From 81ffbc1e31300c056d7ffc32c32948095f80844a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Mon, 28 Oct 2024 23:46:35 +0100 Subject: [PATCH] pl-hooks: Add user directory hook MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../lib/hooks/accounts/use-directory.ts | 31 ++++++ .../pl-hooks/lib/hooks/search/use-search.ts | 95 +++++++++++++++++++ packages/pl-hooks/lib/main.ts | 2 + 3 files changed, 128 insertions(+) create mode 100644 packages/pl-hooks/lib/hooks/accounts/use-directory.ts create mode 100644 packages/pl-hooks/lib/hooks/search/use-search.ts diff --git a/packages/pl-hooks/lib/hooks/accounts/use-directory.ts b/packages/pl-hooks/lib/hooks/accounts/use-directory.ts new file mode 100644 index 000000000..d9363c251 --- /dev/null +++ b/packages/pl-hooks/lib/hooks/accounts/use-directory.ts @@ -0,0 +1,31 @@ +import { useInfiniteQuery } from '@tanstack/react-query'; + +import { importEntities, usePlHooksApiClient, usePlHooksQueryClient } from 'pl-hooks/main'; + +const useDirectory = (order: 'active' | 'new', local: boolean = false) => { + const { client } = usePlHooksApiClient(); + const queryClient = usePlHooksQueryClient(); + + const directoryQuery = useInfiniteQuery({ + queryKey: ['accountsLists', 'directory', order, local], + queryFn: ({ pageParam }) => client.instance.profileDirectory({ + order, + local, + offset: pageParam ? data?.length : 0, + }).then((accounts) => { + importEntities({ accounts }); + return accounts.map(({ id }) => id); + }), + initialPageParam: [''], + getNextPageParam: (page) => page.length ? page : undefined, + }, queryClient); + + const data: Array | undefined = directoryQuery.data?.pages.flat(); + + return { + ...directoryQuery, + data, + }; +}; + +export { useDirectory }; diff --git a/packages/pl-hooks/lib/hooks/search/use-search.ts b/packages/pl-hooks/lib/hooks/search/use-search.ts new file mode 100644 index 000000000..1e2d19458 --- /dev/null +++ b/packages/pl-hooks/lib/hooks/search/use-search.ts @@ -0,0 +1,95 @@ +import { useInfiniteQuery } from '@tanstack/react-query'; + +import { importEntities, usePlHooksApiClient, usePlHooksQueryClient } from 'pl-hooks/main'; + +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 }; diff --git a/packages/pl-hooks/lib/main.ts b/packages/pl-hooks/lib/main.ts index 3dfa63140..8a9a0cb6c 100644 --- a/packages/pl-hooks/lib/main.ts +++ b/packages/pl-hooks/lib/main.ts @@ -4,6 +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/instance/use-instance'; export * from './hooks/instance/use-translation-languages'; export * from './hooks/markers/use-markers'; @@ -11,6 +12,7 @@ export * from './hooks/markers/use-update-marker-mutation'; export * from './hooks/notifications/use-notification'; export * from './hooks/notifications/use-notification-list'; export * from './hooks/polls/use-poll'; +export * from './hooks/search/use-search'; export * from './hooks/statuses/use-status'; export * from './hooks/statuses/use-status-history'; export * from './hooks/statuses/use-status-translation';