From dd560efc6c49cb0e8404064fd098c48d8b2bbb32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sun, 20 Oct 2024 13:55:00 +0200 Subject: [PATCH] pl-hooks: Add useAccountLookup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../pl-hooks/lib/hooks/accounts/useAccount.ts | 2 +- .../lib/hooks/accounts/useAccountLookup.ts | 31 +++++++++++++++++++ packages/pl-hooks/lib/importer.ts | 7 +++-- packages/pl-hooks/lib/main.ts | 1 + 4 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 packages/pl-hooks/lib/hooks/accounts/useAccountLookup.ts diff --git a/packages/pl-hooks/lib/hooks/accounts/useAccount.ts b/packages/pl-hooks/lib/hooks/accounts/useAccount.ts index bd199c911e..660c3e8a1f 100644 --- a/packages/pl-hooks/lib/hooks/accounts/useAccount.ts +++ b/packages/pl-hooks/lib/hooks/accounts/useAccount.ts @@ -38,4 +38,4 @@ const useAccount = (accountId?: string, opts: UseAccountOpts = {}) => { return { ...accountQuery, data }; }; -export { useAccount }; +export { useAccount, type UseAccountOpts }; diff --git a/packages/pl-hooks/lib/hooks/accounts/useAccountLookup.ts b/packages/pl-hooks/lib/hooks/accounts/useAccountLookup.ts new file mode 100644 index 0000000000..719ce86e40 --- /dev/null +++ b/packages/pl-hooks/lib/hooks/accounts/useAccountLookup.ts @@ -0,0 +1,31 @@ +import { useQuery } 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 { useAccount, type UseAccountOpts } from './useAccount'; + +const useAccountLookup = (acct?: string, opts: UseAccountOpts = {}) => { + const queryClient = usePlHooksQueryClient(); + const { client } = usePlHooksApiClient(); + const { features } = client; + + const accountIdQuery = useQuery({ + queryKey: ['accounts', 'byAcct', acct?.toLocaleLowerCase()], + queryFn: () => ( + features.accountByUsername && !features.accountLookup + ? client.accounts.getAccount(acct!) + : client.accounts.lookupAccount(acct!) + ).then((account) => { + importEntities({ accounts: [account] }); + + return account.id; + }), + enabled: !!acct, + }, queryClient); + + return useAccount(accountIdQuery.data, opts); +}; + +export { useAccountLookup }; diff --git a/packages/pl-hooks/lib/importer.ts b/packages/pl-hooks/lib/importer.ts index f5e628aba1..5795d5b3ac 100644 --- a/packages/pl-hooks/lib/importer.ts +++ b/packages/pl-hooks/lib/importer.ts @@ -11,9 +11,10 @@ import type { Status as BaseStatus, } from 'pl-api'; -const importAccount = (account: BaseAccount) => queryClient.setQueryData( - ['accounts', 'entities', account.id], account, -); +const importAccount = (account: BaseAccount) => { + queryClient.setQueryData(['accounts', 'byAcct', account.acct.toLocaleLowerCase()], account.id); + return queryClient.setQueryData(['accounts', 'entities', account.id], account); +}; const importGroup = (group: BaseGroup) => queryClient.setQueryData( ['groups', 'entities', group.id], group, diff --git a/packages/pl-hooks/lib/main.ts b/packages/pl-hooks/lib/main.ts index c35d0e772a..9a763be5be 100644 --- a/packages/pl-hooks/lib/main.ts +++ b/packages/pl-hooks/lib/main.ts @@ -2,6 +2,7 @@ export * from './contexts/api-client'; export * from './contexts/query-client'; export * from './hooks/accounts/useAccount'; +export * from './hooks/accounts/useAccountLookup'; export * from './hooks/accounts/useAccountRelationship'; export * from './hooks/markers/useMarkers'; export * from './hooks/markers/useUpdateMarkerMutation';