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

Improve ENS support (accept all TLDs) #563

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 23 additions & 15 deletions packages/nextjs/components/scaffold-eth/Input/AddressInput.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
import { useCallback, useEffect, useState } from "react";
import { blo } from "blo";
import { isAddress } from "viem";
import { Address } from "viem";
import { useDebounce } from "usehooks-ts";
import { Address, isAddress } from "viem";
import { useEnsAddress, useEnsAvatar, useEnsName } from "wagmi";
import { CommonInputProps, InputBase } from "~~/components/scaffold-eth";

// ToDo: move this function to an utility file
const isENS = (address = "") => address.endsWith(".eth") || address.endsWith(".xyz");
import { CommonInputProps, InputBase, isENS } from "~~/components/scaffold-eth";

/**
* Address input with ENS name resolution
*/
export const AddressInput = ({ value, name, placeholder, onChange, disabled }: CommonInputProps<Address | string>) => {
// Debounce the input to keep clean RPC calls when resolving ENS names
// If the input is an address, we don't need to debounce it
const _debouncedValue = useDebounce(value, 500);
technophile-04 marked this conversation as resolved.
Show resolved Hide resolved
const debouncedValue = isAddress(value) ? value : _debouncedValue;
const isDebouncedValueLive = debouncedValue === value;

// If the user changes the input after an ENS name is already resolved, we want to remove the stale result
const settledValue = isDebouncedValueLive ? debouncedValue : undefined;

const { data: ensAddress, isLoading: isEnsAddressLoading } = useEnsAddress({
name: value,
enabled: isENS(value),
name: settledValue,
enabled: isENS(debouncedValue),
chainId: 1,
cacheTime: 30_000,
});

const [enteredEnsName, setEnteredEnsName] = useState<string>();
const { data: ensName, isLoading: isEnsNameLoading } = useEnsName({
address: value,
enabled: isAddress(value),
address: settledValue,
enabled: isAddress(debouncedValue),
chainId: 1,
cacheTime: 30_000,
});
Expand All @@ -39,9 +45,9 @@ export const AddressInput = ({ value, name, placeholder, onChange, disabled }: C
if (!ensAddress) return;

// ENS resolved successfully
setEnteredEnsName(value);
setEnteredEnsName(debouncedValue);
onChange(ensAddress);
}, [ensAddress, onChange, value]);
}, [ensAddress, onChange, debouncedValue]);

const handleChange = useCallback(
(newValue: Address) => {
Expand Down Expand Up @@ -75,9 +81,11 @@ export const AddressInput = ({ value, name, placeholder, onChange, disabled }: C
)
}
suffix={
// Don't want to use nextJS Image here (and adding remote patterns for the URL)
// eslint-disable-next-line @next/next/no-img-element
value && <img alt="" className="!rounded-full" src={blo(value as `0x${string}`)} width="35" height="35" />
debouncedValue && (
// Don't want to use nextJS Image here (and adding remote patterns for the URL)
// eslint-disable-next-line @next/next/no-img-element
<img alt="" className="!rounded-full" src={blo(debouncedValue as `0x${string}`)} width="35" height="35" />
)
gskril marked this conversation as resolved.
Show resolved Hide resolved
}
/>
);
Expand Down
4 changes: 4 additions & 0 deletions packages/nextjs/components/scaffold-eth/Input/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,7 @@ export const isValidInteger = (dataType: IntegerVariant, value: bigint | string,
}
return true;
};

// Treat any dot-separated string as a potential ENS name
const ensRegex = /.+\..+/;
export const isENS = (address = "") => ensRegex.test(address);