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';