pl-hooks: Add useAccountLookup

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-10-20 13:55:00 +02:00
parent 7cc83b3744
commit dd560efc6c
4 changed files with 37 additions and 4 deletions

View file

@ -38,4 +38,4 @@ const useAccount = (accountId?: string, opts: UseAccountOpts = {}) => {
return { ...accountQuery, data };
};
export { useAccount };
export { useAccount, type UseAccountOpts };

View 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 };

View file

@ -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,

View file

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