diff --git a/src/actions/compose.ts b/src/actions/compose.ts index fa56040bc..55a1bfb4a 100644 --- a/src/actions/compose.ts +++ b/src/actions/compose.ts @@ -581,14 +581,14 @@ const clearComposeSuggestions = (composeId: string) => { }; const fetchComposeSuggestionsAccounts = throttle((dispatch, getState, composeId, token) => { - const signal = cancelFetchComposeSuggestions.signal; - if (cancelFetchComposeSuggestions) { cancelFetchComposeSuggestions.abort(); cancelFetchComposeSuggestions = new AbortController(); } - return getClient(getState()).accounts.searchAccounts(token.slice(1), { resolve: false, limit: 10 }, { signal }) + const signal = cancelFetchComposeSuggestions.signal; + + return getClient(getState).accounts.searchAccounts(token.slice(1), { resolve: false, limit: 10 }, { signal }) .then(response => { dispatch(importFetchedAccounts(response)); dispatch(readyComposeSuggestionsAccounts(composeId, token, response)); diff --git a/src/queries/search.ts b/src/queries/search.ts index 88686ce83..204c258dd 100644 --- a/src/queries/search.ts +++ b/src/queries/search.ts @@ -1,37 +1,39 @@ import { keepPreviousData, useInfiniteQuery } from '@tanstack/react-query'; import { useClient } from 'soapbox/hooks'; +import { flattenPages } from 'soapbox/utils/queries'; -import type { Account } from 'pl-api'; +import type { Account, PaginatedResponse } from 'pl-api'; const useAccountSearch = (q: string) => { const client = useClient(); - const getAccountSearch = async(q: string): Promise => { + const getAccountSearch = async(q: string, pageParam?: Pick, 'next'>): Promise> => { + if (pageParam?.next) return pageParam.next(); + const response = await client.accounts.searchAccounts(q, { limit: 10, following: true, - offset: data?.length, + offset: 0, }); - return response; + return { + previous: null, + next: null, + items: response, + partial: false, + }; }; const queryInfo = useInfiniteQuery({ queryKey: ['search', 'accounts', q], - queryFn: () => getAccountSearch(q), + queryFn: ({ pageParam }) => getAccountSearch(q, pageParam), placeholderData: keepPreviousData, - initialPageParam: {}, - getNextPageParam: () => { - if (queryInfo.data?.pages[queryInfo.data.pages.length - 1].length !== 10) { - return {}; - } - - return undefined; - }, + initialPageParam: { next: null as (() => Promise>) | null }, + getNextPageParam: (config) => config.next ? config : undefined, }); - const data = queryInfo.data?.pages.flat(); + const data = flattenPages(queryInfo.data); return { ...queryInfo,