Skip to content

Commit

Permalink
Introduce useEnsCredentials
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Sep 5, 2024
1 parent 617f985 commit cf8d8b2
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
Expand Up @@ -4,3 +4,4 @@ export { useEnsResolverInterfaces } from './hooks/useEnsResolverInterfaces.js'
export { useDecodedName } from './hooks/useDecodedName.js'
export { useEnsRecordsWrite } from './hooks/useEnsRecordsWrite.js'
export { useEnsExpiry } from './hooks/useEnsExpiry.js'
export { useEnsCredentials } from './hooks/useEnsCredentials.js'
46 changes: 46 additions & 0 deletions packages/react/src/hooks/useEnsCredentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { getTextRecord } from '@ensdomains/ensjs/public'
import type { ParamWithClients } from '../client.js'
import { useQuery, type UseQueryReturnType } from './useQuery.js'

export type UseEnsCredentialsParams = ParamWithClients<{ name: string }>

export type ExternalCredential = {
url: string
}

export type UseEnsCredentialsReturnType = ExternalCredential[]

/**
* Returns credentials from a name
*
* @param params - {@link UseEnsCredentialsParams}
* @returns - {@link UseEnsCredentialsReturnType}
*/
export const useEnsCredentials = (
params: UseEnsCredentialsParams,
): UseQueryReturnType<UseEnsCredentialsReturnType> => {
const { name, client, queryClient } = params

return useQuery(
['ensjs', 'credentials', params.name],
{
queryFn: async () => {
const result = await getTextRecord(client, {
name,
key: 'verifications',
})

if (!result) return []

const credentials = (JSON.parse(result) as string[])
.filter((url) => new URL(url))
.map((url) => ({
url,
}))

return credentials
},
},
queryClient,
)
}

0 comments on commit cf8d8b2

Please sign in to comment.