-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
50 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { useNames } from './hooks/useNames.js' | ||
export { useNamesForAddress } from './hooks/useNamesForAddress.js' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} |