bigbuffet-rw/packages/pl-hooks/lib/hooks/accounts/useAccountLookup.ts
marcin mikołajczak 11a27f9504 pl-hooks: Fixes, new hooks
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
2024-10-22 15:28:57 +02:00

31 lines
1,007 B
TypeScript

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 { client } = usePlHooksApiClient();
const queryClient = usePlHooksQueryClient();
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 };