Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add Opts for useEns hook #178

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [3.9.0] - 2024-12-20
## [3.9.0] - 2025-01-07

- Improve how the ENS address information is provided if available through the UI. The new approach considerably improves efficiency in terms of on-chain reads.

Expand Down
18 changes: 12 additions & 6 deletions packages/services/src/ens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const getENSInfo = (address: string, chainId: number): GetENSInfoResult => {
});
};

const useCachedEntry = (address: string) => {
const useCachedEntry = (address: string, enabled: boolean) => {
const { chainId } = useWallet();
const cachedData = useContext(ENSDataContext);
const [entry, setEntry] = useState<Partial<ENSData>>({
Expand All @@ -75,8 +75,7 @@ const useCachedEntry = (address: string) => {

useEffect(() => {
const isValidAddress = ethers.utils.isAddress(address ?? '');

if (!isValidAddress) {
if (!isValidAddress || !enabled) {
const entry = { address, hasEns: false, resolving: false };
setEntry(entry);
return;
Expand Down Expand Up @@ -114,7 +113,7 @@ const useCachedEntry = (address: string) => {
setEntry(newEntry);
});
}
}, [cachedData, address, chainId]);
}, [cachedData, address, chainId, enabled]);

return entry;
};
Expand Down Expand Up @@ -151,13 +150,20 @@ export interface ENSEntry {
resolving: boolean;
}

type Opts = { enabled: boolean };

/**
* This hook provides a easy way to display ENS information about a ETH address.
* When opts params are omitted the fetching is enabled in case of cache miss. Otherwise,
* it will just cache and return the address passed down.
* @param address ETH address to be resolved
* @param opts Option to fine grained control on fetching ENS info.
* @returns ENSEntry with the address, and name if address can be resolved to a name
*/
export const useENS = (address: string): ENSEntry => {
const cachedEnsEntry = useCachedEntry(address);
export const useENS = (address: string, opts?: Opts): ENSEntry => {
const isEnabled = opts === undefined || opts.enabled;
const cachedEnsEntry = useCachedEntry(address, isEnabled);

return {
address: cachedEnsEntry?.address ?? address,
resolving: cachedEnsEntry?.resolving ?? false,
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/src/components/Address.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ const Address: FC<AddressProps> = (props) => {
} = props;

// resolve ENS entry from address
const addressEnsInfo = useENS(address);
const withEns = ens === true;
const addressEnsInfo = useENS(address, { enabled: withEns });
const ensEntry = ens ? addressEnsInfo : null;

const { hasCopied, onCopy } = useClipboard(address);
Expand Down
Loading