Skip to content

Commit

Permalink
Upgrade Hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Sep 4, 2024
1 parent 09e6483 commit df7f7f8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/ensjs/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { useNames } from './hooks/useNames.js'
export { useNamesForAddress } from './hooks/useNamesForAddress.js'
29 changes: 0 additions & 29 deletions packages/ensjs/src/hooks/useNames.ts

This file was deleted.

49 changes: 49 additions & 0 deletions packages/ensjs/src/hooks/useNamesForAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
QueryClient,
useQuery,
type UseQueryResult,
} from '@tanstack/react-query'
import type { Address } from 'viem'
import {
getNamesForAddress,
type GetNamesForAddressReturnType,
} from '../subgraph.js'
import type { ClientWithEns } from '../contracts/consts.js'

export type UseNamesParams = {
address: Address
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 list will be loaded from the subgraph, and only include watchable names.
* Read more about enumeration and watchability here: https://docs.ens.domains/web/enumerate
*
* @param data - {@link UseNamesParams}
* @returns - {@link GetNamesForAddressReturnType}
*/
export const useNamesForAddress = (
data: UseNamesParams,
): UseQueryResult<GetNamesForAddressReturnType> => {
const { address, client, queryClient = fallbackQueryClient } = data

return useQuery(
{
queryKey: ['ensjs', 'names-for-address', address],
queryFn: async () => {
const result = await getNamesForAddress(client, {
address,
})

return result
},
},
queryClient,
)
}

0 comments on commit df7f7f8

Please sign in to comment.