pl-hooks: Add useAccountLookup
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
7cc83b3744
commit
dd560efc6c
4 changed files with 37 additions and 4 deletions
|
@ -38,4 +38,4 @@ const useAccount = (accountId?: string, opts: UseAccountOpts = {}) => {
|
|||
return { ...accountQuery, data };
|
||||
};
|
||||
|
||||
export { useAccount };
|
||||
export { useAccount, type UseAccountOpts };
|
||||
|
|
31
packages/pl-hooks/lib/hooks/accounts/useAccountLookup.ts
Normal file
31
packages/pl-hooks/lib/hooks/accounts/useAccountLookup.ts
Normal file
|
@ -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 };
|
|
@ -11,9 +11,10 @@ import type {
|
|||
Status as BaseStatus,
|
||||
} from 'pl-api';
|
||||
|
||||
const importAccount = (account: BaseAccount) => queryClient.setQueryData<BaseAccount>(
|
||||
['accounts', 'entities', account.id], account,
|
||||
);
|
||||
const importAccount = (account: BaseAccount) => {
|
||||
queryClient.setQueryData<string>(['accounts', 'byAcct', account.acct.toLocaleLowerCase()], account.id);
|
||||
return queryClient.setQueryData<BaseAccount>(['accounts', 'entities', account.id], account);
|
||||
};
|
||||
|
||||
const importGroup = (group: BaseGroup) => queryClient.setQueryData<BaseGroup>(
|
||||
['groups', 'entities', group.id], group,
|
||||
|
|
|
@ -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';
|
||||
|
|
Loading…
Reference in a new issue