diff --git a/packages/ensjs/src/hooks.ts b/packages/ensjs/src/hooks.ts index 939e03ee..105e54fb 100644 --- a/packages/ensjs/src/hooks.ts +++ b/packages/ensjs/src/hooks.ts @@ -1 +1,2 @@ export { useNamesForAddress } from './hooks/useNamesForAddress.js' +export { useEnsAvailable } from './hooks/useEnsAvailable.js' diff --git a/packages/ensjs/src/hooks/useEnsAvailable.ts b/packages/ensjs/src/hooks/useEnsAvailable.ts new file mode 100644 index 00000000..ced3c412 --- /dev/null +++ b/packages/ensjs/src/hooks/useEnsAvailable.ts @@ -0,0 +1,44 @@ +import { + QueryClient, + useQuery, + type UseQueryResult, +} from '@tanstack/react-query' +import type { ClientWithEns } from '../contracts/consts.js' +import { getAvailable } from '../public.js' + +export type UseEnsAvailableParams = { + name: string + client: ClientWithEns + queryClient?: QueryClient +} + +// TODO: figure out why not taking from provider +const fallbackQueryClient = new QueryClient() + +/** + * Returns a list of names for an address + * + * Keep in mind that this function is limited to .eth names + * + * @param data - {@link UseEnsAvailableParams} + * @returns - {@link boolean} + */ +export const useEnsAvailable = ( + data: UseEnsAvailableParams, +): UseQueryResult => { + const { name, client, queryClient = fallbackQueryClient } = data + + return useQuery( + { + queryKey: ['ensjs', 'eth-name-available', name], + queryFn: async () => { + const result = await getAvailable(client, { + name, + }) + + return result + }, + }, + queryClient, + ) +}