Skip to content

Commit

Permalink
Introduce useRecords hook (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans authored Apr 29, 2023
1 parent 8584e17 commit dfbb850
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/react/hooks/useRecords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { useChainId, useEnsResolver, useQuery } from 'wagmi';

type _wagmiEnsResolverArguments = typeof useEnsResolver extends (
arguments_: infer U
) => any
? U
: never;

export type EnsRecordsConfig = {
// ENS name (eg. "luc.eth")
name: string;
// Records (eg. ["com.github", "avatar", "com.discord"])
records: string[];
// Wether to format/normalize the records
format: boolean;
} & _wagmiEnsResolverArguments;

const queryKey = ({
chainId,
name,
scopeKey,
}: Pick<EnsRecordsConfig, 'chainId' | 'name' | 'scopeKey'>) => {
return [
{
entity: 'ensRecords',
chainId,
name,
scopeKey,
persist: false,
},
] as const;
};

/**
* Gets the user's records for a given ENS.
* CURRENTLY INCOMPLETE IMPLEMENTATION
* Based on: https://github.com/wagmi-dev/wagmi/blob/main/packages/react/src/hooks/ens/useEnsResolver.ts
*/
export const useRecords = ({
records,
...resolverConfig
}: EnsRecordsConfig): any => {
const {
name,
chainId: _chainId,
enabled,
scopeKey,
suspense,
onError,
onSettled,
onSuccess,
cacheTime,
} = resolverConfig;

const chainId = useChainId({ chainId: _chainId });
const { data, ...resolverOutput } = useEnsResolver({
...resolverConfig,
});

return useQuery(
queryKey({ chainId, name, scopeKey }),
async ({ queryKey: [{ chainId, name }] }) => {
return await Promise.all(
records.map(async (record) => data?.getText(record))
);
},
{
cacheTime: cacheTime || 3600,
enabled: Boolean(enabled && chainId && name),
suspense,
onError,
onSettled,
onSuccess,
}
);
};
1 change: 1 addition & 0 deletions src/react/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './hooks/useEnsMultichainAddress';
export * from './hooks/useRecords';

0 comments on commit dfbb850

Please sign in to comment.