Skip to content

Commit

Permalink
Introduce useEnsAvailable
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Sep 4, 2024
1 parent df7f7f8 commit 62914db
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/ensjs/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { useNamesForAddress } from './hooks/useNamesForAddress.js'
export { useEnsAvailable } from './hooks/useEnsAvailable.js'
44 changes: 44 additions & 0 deletions packages/ensjs/src/hooks/useEnsAvailable.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> => {
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,
)
}

0 comments on commit 62914db

Please sign in to comment.