diff --git a/packages/pl-fe/src/api/hooks/account-lists/use-event-participation-requests.ts b/packages/pl-fe/src/api/hooks/account-lists/use-event-participation-requests.ts index 94d138482..072758d7e 100644 --- a/packages/pl-fe/src/api/hooks/account-lists/use-event-participation-requests.ts +++ b/packages/pl-fe/src/api/hooks/account-lists/use-event-participation-requests.ts @@ -2,7 +2,8 @@ import { type InfiniteData, useInfiniteQuery, useMutation } from '@tanstack/react-query'; import { importEntities } from 'pl-fe/actions/importer'; -import { minifyList } from 'pl-fe/api/normalizers/minify-list'; +import { makePaginatedResponseQuery } from 'pl-fe/api/utils/make-paginated-response-query'; +import { minifyList } from 'pl-fe/api/utils/minify-list'; import { useClient } from 'pl-fe/hooks/use-client'; import { queryClient } from 'pl-fe/queries/client'; import { store } from 'pl-fe/store'; @@ -24,17 +25,10 @@ const removeRequest = (statusId: string, accountId: string) => pages: data.pages.map(({ items, ...page }) => ({ ...page, items: items.filter(({ account_id }) => account_id !== accountId) })), } : undefined); -const useEventParticipationRequests = (statusId: string) => { - const client = useClient(); - - return useInfiniteQuery({ - queryKey: ['accountsLists', 'eventParticipationRequests', statusId], - queryFn: ({ pageParam }) => pageParam.next?.() || client.events.getEventParticipationRequests(statusId).then(minifyRequestList), - initialPageParam: { previous: null, next: null, items: [], partial: false } as MinifiedRequestList, - getNextPageParam: (page) => page.next ? page : undefined, - select: (data) => data.pages.map(page => page.items).flat(), - }); -}; +const useEventParticipationRequests = makePaginatedResponseQuery( + (statusId: string) => ['accountsLists', 'eventParticipationRequests', statusId], + (client, params) => client.events.getEventParticipationRequests(...params).then(minifyRequestList), +); const useAcceptEventParticipationRequestMutation = (statusId: string, accountId: string) => { const client = useClient(); diff --git a/packages/pl-fe/src/api/hooks/account-lists/use-event-participations.ts b/packages/pl-fe/src/api/hooks/account-lists/use-event-participations.ts index 33d63dc1b..18adb8850 100644 --- a/packages/pl-fe/src/api/hooks/account-lists/use-event-participations.ts +++ b/packages/pl-fe/src/api/hooks/account-lists/use-event-participations.ts @@ -1,21 +1,9 @@ +import { makePaginatedResponseQuery } from 'pl-fe/api/utils/make-paginated-response-query'; +import { minifyAccountList } from 'pl-fe/api/utils/minify-list'; -import { useInfiniteQuery } from '@tanstack/react-query'; - -import { minifyAccountList } from 'pl-fe/api/normalizers/minify-list'; -import { useClient } from 'pl-fe/hooks/use-client'; - -import type { PaginatedResponse } from 'pl-api'; - -const useEventParticipations = (statusId: string) => { - const client = useClient(); - - return useInfiniteQuery({ - queryKey: ['accountsLists', 'eventParticipations', statusId], - queryFn: ({ pageParam }) => pageParam.next?.() || client.events.getEventParticipations(statusId).then(minifyAccountList), - initialPageParam: { previous: null, next: null, items: [], partial: false } as PaginatedResponse, - getNextPageParam: (page) => page.next ? page : undefined, - select: (data) => data.pages.map(page => page.items).flat(), - }); -}; +const useEventParticipations = makePaginatedResponseQuery( + (statusId: string) => ['accountsLists', 'eventParticipations', statusId], + (client, params) => client.events.getEventParticipations(...params).then(minifyAccountList), +); export { useEventParticipations }; diff --git a/packages/pl-fe/src/api/hooks/account-lists/use-follow-requests.ts b/packages/pl-fe/src/api/hooks/account-lists/use-follow-requests.ts index f3554d496..679664a95 100644 --- a/packages/pl-fe/src/api/hooks/account-lists/use-follow-requests.ts +++ b/packages/pl-fe/src/api/hooks/account-lists/use-follow-requests.ts @@ -1,7 +1,7 @@ +import { useMutation, type InfiniteData } from '@tanstack/react-query'; -import { useInfiniteQuery, useMutation, type InfiniteData } from '@tanstack/react-query'; - -import { minifyAccountList } from 'pl-fe/api/normalizers/minify-list'; +import { makePaginatedResponseQuery } from 'pl-fe/api/utils/make-paginated-response-query'; +import { minifyAccountList } from 'pl-fe/api/utils/minify-list'; import { useClient } from 'pl-fe/hooks/use-client'; import { queryClient } from 'pl-fe/queries/client'; @@ -23,17 +23,11 @@ const removeFollowRequest = (accountId: string) => pages: data.pages.map(({ items, ...page }) => ({ ...page, items: items.filter((id) => id !== accountId) })), } : undefined); -const makeUseFollowRequests = (select: ((data: InfiniteData, PaginatedResponse>) => T)) => () => { - const client = useClient(); - - return useInfiniteQuery({ - queryKey: ['accountsLists', 'followRequests'], - queryFn: ({ pageParam }) => pageParam.next?.() || client.myAccount.getFollowRequests().then(minifyAccountList), - initialPageParam: { previous: null, next: null, items: [], partial: false } as PaginatedResponse, - getNextPageParam: (page) => page.next ? page : undefined, - select, - }); -}; +const makeUseFollowRequests = (select: ((data: InfiniteData>) => T)) => makePaginatedResponseQuery( + () => ['accountsLists', 'followRequests'], + (client) => client.myAccount.getFollowRequests().then(minifyAccountList), + select, +); const useFollowRequests = makeUseFollowRequests((data) => data.pages.map(page => page.items).flat()); diff --git a/packages/pl-fe/src/api/hooks/account-lists/use-status-interactions.ts b/packages/pl-fe/src/api/hooks/account-lists/use-status-interactions.ts index 524413547..a0111b77e 100644 --- a/packages/pl-fe/src/api/hooks/account-lists/use-status-interactions.ts +++ b/packages/pl-fe/src/api/hooks/account-lists/use-status-interactions.ts @@ -1,33 +1,25 @@ -import { useInfiniteQuery, useQuery } from '@tanstack/react-query'; +import { useQuery } from '@tanstack/react-query'; import { importEntities } from 'pl-fe/actions/importer'; -import { minifyAccountList } from 'pl-fe/api/normalizers/minify-list'; +import { makePaginatedResponseQuery } from 'pl-fe/api/utils/make-paginated-response-query'; +import { minifyAccountList } from 'pl-fe/api/utils/minify-list'; import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch'; import { useClient } from 'pl-fe/hooks/use-client'; -import type { PaginatedResponse } from 'pl-api'; - -const useStatusInteractions = (statusId: string, method: 'getDislikedBy' | 'getFavouritedBy' | 'getRebloggedBy') => { - const client = useClient(); - - const queryKey = { - getDislikedBy: 'statusDislikes', - getFavouritedBy: 'statusFavourites', - getRebloggedBy: 'statusReblogs', - }[method]; - - return useInfiniteQuery({ - queryKey: ['accountsLists', queryKey, statusId], - queryFn: ({ pageParam }) => pageParam.next?.() || client.statuses[method](statusId).then(minifyAccountList), - initialPageParam: { previous: null, next: null, items: [], partial: false } as PaginatedResponse, - getNextPageParam: (page) => page.next ? page : undefined, - select: (data) => data.pages.map(page => page.items).flat(), - }); +const queryKey = { + getDislikedBy: 'statusDislikes', + getFavouritedBy: 'statusFavourites', + getRebloggedBy: 'statusReblogs', }; -const useStatusDislikes = (statusId: string) => useStatusInteractions(statusId, 'getDislikedBy'); -const useStatusFavourites = (statusId: string) => useStatusInteractions(statusId, 'getFavouritedBy'); -const useStatusReblogs = (statusId: string) => useStatusInteractions(statusId, 'getRebloggedBy'); +const makeUseStatusInteractions = (method: 'getDislikedBy' | 'getFavouritedBy' | 'getRebloggedBy') => makePaginatedResponseQuery( + (statusId: string) => ['accountsLists', queryKey[method], statusId], + (client, params) => client.statuses[method](...params).then(minifyAccountList), +); + +const useStatusDislikes = makeUseStatusInteractions('getDislikedBy'); +const useStatusFavourites = makeUseStatusInteractions('getFavouritedBy'); +const useStatusReblogs = makeUseStatusInteractions('getRebloggedBy'); const useStatusReactions = (statusId: string, emoji?: string) => { const client = useClient(); diff --git a/packages/pl-fe/src/api/hooks/statuses/use-status-quotes.ts b/packages/pl-fe/src/api/hooks/statuses/use-status-quotes.ts index cb214dd13..4af683f96 100644 --- a/packages/pl-fe/src/api/hooks/statuses/use-status-quotes.ts +++ b/packages/pl-fe/src/api/hooks/statuses/use-status-quotes.ts @@ -1,20 +1,9 @@ -import { useInfiniteQuery } from '@tanstack/react-query'; +import { makePaginatedResponseQuery } from 'pl-fe/api/utils/make-paginated-response-query'; +import { minifyStatusList } from 'pl-fe/api/utils/minify-list'; -import { minifyStatusList } from 'pl-fe/api/normalizers/minify-list'; -import { useClient } from 'pl-fe/hooks/use-client'; - -import type { PaginatedResponse } from 'pl-api'; - -const useStatusQuotes = (statusId: string) => { - const client = useClient(); - - return useInfiniteQuery({ - queryKey: ['statusLists', 'quotes', statusId], - queryFn: ({ pageParam }) => pageParam.next?.() || client.statuses.getStatusQuotes(statusId).then(minifyStatusList), - initialPageParam: { previous: null, next: null, items: [], partial: false } as PaginatedResponse, - getNextPageParam: (page) => page.next ? page : undefined, - select: (data) => data.pages.map(page => page.items).flat(), - }); -}; +const useStatusQuotes = makePaginatedResponseQuery( + (statusId: string) => ['statusLists', 'quotes', statusId], + (client, params) => client.statuses.getStatusQuotes(...params).then(minifyStatusList), +); export { useStatusQuotes }; diff --git a/packages/pl-fe/src/api/utils/make-paginated-response-query.ts b/packages/pl-fe/src/api/utils/make-paginated-response-query.ts new file mode 100644 index 000000000..26152c99c --- /dev/null +++ b/packages/pl-fe/src/api/utils/make-paginated-response-query.ts @@ -0,0 +1,23 @@ +import { type InfiniteData, useInfiniteQuery } from '@tanstack/react-query'; + +import { useClient } from 'pl-fe/hooks/use-client'; + +import type { PaginatedResponse, PlApiClient } from 'pl-api'; + +const makePaginatedResponseQuery = , T2, T3 = Array>( + queryKey: (...params: T1) => string[], + queryFn: (client: PlApiClient, params: T1) => Promise>, + select?: (data: InfiniteData>) => T3, +) => (...params: T1) => { + const client = useClient(); + + return useInfiniteQuery({ + queryKey: queryKey(...params), + queryFn: ({ pageParam }) => pageParam.next?.() || queryFn(client, params), + initialPageParam: { previous: null, next: null, items: [], partial: false } as Awaited>, + getNextPageParam: (page) => page.next ? page : undefined, + select: select ?? ((data) => data.pages.map(page => page.items).flat() as T3), + }); + }; + +export { makePaginatedResponseQuery }; diff --git a/packages/pl-fe/src/api/normalizers/minify-list.ts b/packages/pl-fe/src/api/utils/minify-list.ts similarity index 100% rename from packages/pl-fe/src/api/normalizers/minify-list.ts rename to packages/pl-fe/src/api/utils/minify-list.ts