Skip to content

Commit

Permalink
Introduce useDecodedName
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Sep 4, 2024
1 parent b8b0149 commit 69a5ff2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/react/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { useNamesForAddress } from './hooks/useNamesForAddress.js'
export { useEnsAvailable } from './hooks/useEnsAvailable.js'
export { useEnsResolverInterfaces } from './hooks/useEnsResolverInterfaces.js'
export { useDecodedName } from './hooks/useDecodedName.js'
46 changes: 46 additions & 0 deletions packages/react/src/hooks/useDecodedName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useQuery, type UseQueryResult } from '@tanstack/react-query'
import {
getDecodedName,
type GetDecodedNameReturnType,
} from '@ensdomains/ensjs/subgraph'
import type { ParamWithClients } from '../client.js'
import { fallbackQueryClient } from '../query.js'

export type UseDecodedNameParams = ParamWithClients<{
name: string
allowIncomplete?: boolean
}>

/**
* Decode names returned using the subgraph
*
* Performs network request only if the name needs to be decoded, otherwise transparent
*
* @param data - {@link UseDecodedNameParams}
* @returns - {@link GetDecodedNameReturnType}
*/
export const useDecodedName = (
data: UseDecodedNameParams,
): UseQueryResult<GetDecodedNameReturnType> => {
const {
name,
allowIncomplete,
client,
queryClient = fallbackQueryClient,
} = data

return useQuery(
{
queryKey: ['ensjs', 'decoded-subgraph-name', name],
queryFn: async () => {
const result = await getDecodedName(client, {
name,
allowIncomplete,
})

return result
},
},
queryClient,
)
}

0 comments on commit 69a5ff2

Please sign in to comment.