From 9f8b9a783c07110b6818be7217e04a37e0b8b6c3 Mon Sep 17 00:00:00 2001 From: mkljczk Date: Sat, 30 Nov 2024 20:30:37 +0100 Subject: [PATCH] pl-hooks: improvements Signed-off-by: mkljczk --- .../use-directory.ts | 18 ++++++------------ packages/pl-hooks/lib/main.ts | 2 +- 2 files changed, 7 insertions(+), 13 deletions(-) rename packages/pl-hooks/lib/hooks/{accounts => account-lists}/use-directory.ts (64%) diff --git a/packages/pl-hooks/lib/hooks/accounts/use-directory.ts b/packages/pl-hooks/lib/hooks/account-lists/use-directory.ts similarity index 64% rename from packages/pl-hooks/lib/hooks/accounts/use-directory.ts rename to packages/pl-hooks/lib/hooks/account-lists/use-directory.ts index e6b5e2321..e9e1dc926 100644 --- a/packages/pl-hooks/lib/hooks/accounts/use-directory.ts +++ b/packages/pl-hooks/lib/hooks/account-lists/use-directory.ts @@ -8,26 +8,20 @@ const useDirectory = (order: 'active' | 'new', local: boolean = false) => { const { client } = usePlHooksApiClient(); const queryClient = usePlHooksQueryClient(); - const directoryQuery = useInfiniteQuery({ + return useInfiniteQuery({ queryKey: ['accountsLists', 'directory', order, local], - queryFn: ({ pageParam }) => client.instance.profileDirectory({ + queryFn: ({ pageParam: offset }) => client.instance.profileDirectory({ order, local, - offset: pageParam ? data?.length : 0, + offset, }).then((accounts) => { importEntities({ accounts }); return accounts.map(({ id }) => id); }), - initialPageParam: [''], - getNextPageParam: (page) => page.length ? page : undefined, + initialPageParam: 0, + getNextPageParam: (_, allPages) => allPages.at(-1)?.length === 0 ? undefined : allPages.flat().length, + select: (data) => data?.pages.flat(), }, queryClient); - - const data: Array | undefined = directoryQuery.data?.pages.flat(); - - return { - ...directoryQuery, - data, - }; }; export { useDirectory }; diff --git a/packages/pl-hooks/lib/main.ts b/packages/pl-hooks/lib/main.ts index 8a9a0cb6c..b460f92bb 100644 --- a/packages/pl-hooks/lib/main.ts +++ b/packages/pl-hooks/lib/main.ts @@ -4,7 +4,7 @@ export * from './contexts/query-client'; export * from './hooks/accounts/use-account'; export * from './hooks/accounts/use-account-lookup'; export * from './hooks/accounts/use-account-relationship'; -export * from './hooks/accounts/use-directory'; +export * from './hooks/account-lists/use-directory'; export * from './hooks/instance/use-instance'; export * from './hooks/instance/use-translation-languages'; export * from './hooks/markers/use-markers';