import { useInfiniteQuery } from '@tanstack/react-query'; import { usePlHooksApiClient } from 'pl-hooks/contexts/api-client'; import { usePlHooksQueryClient } from 'pl-hooks/contexts/query-client'; import { importEntities } from 'pl-hooks/importer'; import type { SearchParams, Tag } from 'pl-api'; import type { PaginationParams } from 'pl-api/dist/params/common'; const useSearchAccounts = ( query: string, params?: Omit, ) => { const queryClient = usePlHooksQueryClient(); const { client } = usePlHooksApiClient(); const searchQuery = useInfiniteQuery({ queryKey: ['search', 'accounts', query, params], queryFn: ({ pageParam }) => client.search.search(query!, { ...params, offset: pageParam ? data?.length : 0, type: 'accounts', }).then(({ accounts }) => { importEntities({ accounts }); return accounts.map(({ id }) => id); }), enabled: !!query?.trim(), initialPageParam: [''], getNextPageParam: (page) => page.length ? page : undefined, }, queryClient); const data: Array | undefined = searchQuery.data?.pages.flat(); return { ...searchQuery, data, }; }; const useSearchStatuses = ( query: string, params?: Omit, ) => { const queryClient = usePlHooksQueryClient(); const { client } = usePlHooksApiClient(); const searchQuery = useInfiniteQuery({ queryKey: ['search', 'statuses', query, params], queryFn: ({ pageParam }) => client.search.search(query, { ...params, offset: pageParam ? data?.length : 0, type: 'statuses', }).then(({ statuses }) => { importEntities({ statuses }); return statuses.map(({ id }) => id); }), enabled: !!query?.trim(), initialPageParam: [''], getNextPageParam: (page) => page.length ? page : undefined, }, queryClient); const data: Array | undefined = searchQuery.data?.pages.flat(); return { ...searchQuery, data, }; }; const useSearchHashtags = ( query: string, params?: Omit, ) => { const queryClient = usePlHooksQueryClient(); const { client } = usePlHooksApiClient(); const searchQuery = useInfiniteQuery({ queryKey: ['search', 'hashtags', query, params], queryFn: ({ pageParam }) => client.search.search(query, { ...params, offset: pageParam ? data?.length : 0, type: 'hashtags', }).then(({ hashtags }) => hashtags as Array), enabled: !!query?.trim(), initialPageParam: [{}], getNextPageParam: (page) => page.length ? page : undefined, }, queryClient); const data: Array | undefined = searchQuery.data?.pages.flat(); return { ...searchQuery, data, }; }; export { useSearchAccounts, useSearchStatuses, useSearchHashtags };