diff --git a/packages/pl-fe/src/api/hooks/search/use-search.ts b/packages/pl-fe/src/api/hooks/search/use-search.ts index 56aab4d1d..8d269db03 100644 --- a/packages/pl-fe/src/api/hooks/search/use-search.ts +++ b/packages/pl-fe/src/api/hooks/search/use-search.ts @@ -14,26 +14,22 @@ const useSearchAccounts = ( const client = useClient(); const dispatch = useAppDispatch(); - const searchQuery = useInfiniteQuery({ + return useInfiniteQuery({ queryKey: ['search', 'accounts', query, params], - queryFn: ({ pageParam, signal }) => client.search.search(query!, { + queryFn: ({ pageParam: offset, signal }) => client.search.search(query!, { with_relationships: true, ...params, - offset: pageParam ? data?.length : 0, + offset, type: 'accounts', }, { signal }).then(({ accounts }) => { dispatch(importEntities({ accounts })); return accounts.map(({ id }) => id); }), enabled: !!query?.trim(), - initialPageParam: [''], - getNextPageParam: (page) => page.length ? page : undefined, - select: (data => data.pages.flat()), + initialPageParam: 0, + getNextPageParam: (_, allPages) => allPages.flat().length, + select: (data) => data.pages.flat(), }); - - const data: Array | undefined = searchQuery.data; - - return searchQuery; }; const useSearchStatuses = ( @@ -57,7 +53,7 @@ const useSearchStatuses = ( enabled: !!query?.trim(), initialPageParam: 0, getNextPageParam: (_, allPages) => allPages.flat().length, - select: (data => data.pages.flat()), + select: (data) => data.pages.flat(), }); }; @@ -77,7 +73,7 @@ const useSearchHashtags = ( enabled: !!query?.trim(), initialPageParam: 0, getNextPageParam: (_, allPages) => allPages.flat().length, - select: (data => data.pages.flat()), + select: (data) => data.pages.flat(), }); }; @@ -101,7 +97,7 @@ const useSearchGroups = ( enabled: !!query?.trim(), initialPageParam: 0, getNextPageParam: (_, allPages) => allPages.flat().length, - select: (data => data.pages.flat()), + select: (data) => data.pages.flat(), }); }; diff --git a/packages/pl-hooks/lib/hooks/search/use-search.ts b/packages/pl-hooks/lib/hooks/search/use-search.ts index 6a438a81f..6327839fc 100644 --- a/packages/pl-hooks/lib/hooks/search/use-search.ts +++ b/packages/pl-hooks/lib/hooks/search/use-search.ts @@ -14,27 +14,21 @@ const useSearchAccounts = ( const queryClient = usePlHooksQueryClient(); const { client } = usePlHooksApiClient(); - const searchQuery = useInfiniteQuery({ + return useInfiniteQuery({ queryKey: ['search', 'accounts', query, params], - queryFn: ({ pageParam }) => client.search.search(query!, { + queryFn: ({ pageParam: offset, signal }) => client.search.search(query!, { ...params, - offset: pageParam ? data?.length : 0, + offset, type: 'accounts', - }).then(({ accounts }) => { + }, { signal }).then(({ accounts }) => { importEntities({ accounts }); return accounts.map(({ id }) => id); }), enabled: !!query?.trim(), - initialPageParam: [''], - getNextPageParam: (page) => page.length ? page : undefined, + initialPageParam: 0, + getNextPageParam: (_, allPages) => allPages.flat().length, + select: (data) => data.pages.flat(), }, queryClient); - - const data: Array | undefined = searchQuery.data?.pages.flat(); - - return { - ...searchQuery, - data, - }; }; const useSearchStatuses = ( @@ -44,27 +38,21 @@ const useSearchStatuses = ( const queryClient = usePlHooksQueryClient(); const { client } = usePlHooksApiClient(); - const searchQuery = useInfiniteQuery({ + return useInfiniteQuery({ queryKey: ['search', 'statuses', query, params], - queryFn: ({ pageParam }) => client.search.search(query, { + queryFn: ({ pageParam: offset, signal }) => client.search.search(query, { ...params, - offset: pageParam ? data?.length : 0, + offset, type: 'statuses', - }).then(({ statuses }) => { + }, { signal }).then(({ statuses }) => { importEntities({ statuses }); return statuses.map(({ id }) => id); }), enabled: !!query?.trim(), - initialPageParam: [''], - getNextPageParam: (page) => page.length ? page : undefined, + initialPageParam: 0, + getNextPageParam: (_, allPages) => allPages.flat().length, + select: (data) => data.pages.flat(), }, queryClient); - - const data: Array | undefined = searchQuery.data?.pages.flat(); - - return { - ...searchQuery, - data, - }; }; const useSearchHashtags = ( @@ -74,24 +62,18 @@ const useSearchHashtags = ( const queryClient = usePlHooksQueryClient(); const { client } = usePlHooksApiClient(); - const searchQuery = useInfiniteQuery({ + return useInfiniteQuery({ queryKey: ['search', 'hashtags', query, params], - queryFn: ({ pageParam }) => client.search.search(query, { + queryFn: ({ pageParam: offset, signal }) => client.search.search(query, { ...params, - offset: pageParam ? data?.length : 0, + offset, type: 'hashtags', - }).then(({ hashtags }) => hashtags as Array), + }, { signal }).then(({ hashtags }) => hashtags as Array), enabled: !!query?.trim(), - initialPageParam: [{}], - getNextPageParam: (page) => page.length ? page : undefined, + initialPageParam: 0, + getNextPageParam: (_, allPages) => allPages.flat().length, + select: (data) => data.pages.flat(), }, queryClient); - - const data: Array | undefined = searchQuery.data?.pages.flat(); - - return { - ...searchQuery, - data, - }; }; export { useSearchAccounts, useSearchStatuses, useSearchHashtags }; diff --git a/packages/pl-hooks/lib/hooks/statuses/use-status-quotes.ts b/packages/pl-hooks/lib/hooks/statuses/use-status-quotes.ts index e4cba2440..972a4e3df 100644 --- a/packages/pl-hooks/lib/hooks/statuses/use-status-quotes.ts +++ b/packages/pl-hooks/lib/hooks/statuses/use-status-quotes.ts @@ -10,16 +10,13 @@ const useStatusQuotes = (statusId: string) => { const queryClient = usePlHooksQueryClient(); const { client } = usePlHooksApiClient(); - const statusQuotesQuery = useInfiniteQuery({ + return useInfiniteQuery({ queryKey: ['statusesLists', '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(), }, queryClient); - - const data = statusQuotesQuery.data?.pages.map(page => page.items).flat(); - - return { ...statusQuotesQuery, data }; }; export { useStatusQuotes };