Skip to content

Commit

Permalink
feat: add better handling for ens errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alx-khramov committed Jun 25, 2024
1 parent eddf917 commit 1dacde7
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
13 changes: 11 additions & 2 deletions features/rewards/components/addressInput/AddressInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import { isValidAnyAddress } from 'features/rewards/utils';
import { AddressInputProps } from './types';

export const AddressInput: FC<AddressInputProps> = (props) => {
const { inputValue, isAddressResolving, handleInputChange, address } = props;
const {
inputValue,
isAddressResolving,
handleInputChange,
address,
addressError,
} = props;

return (
<Input
Expand All @@ -23,7 +29,10 @@ export const AddressInput: FC<AddressInputProps> = (props) => {
}
rightDecorator={address ? <CopyAddressUrl address={inputValue} /> : null}
spellCheck="false"
error={inputValue.length > 0 && !isValidAnyAddress(inputValue)}
error={
(inputValue.length > 0 && !isValidAnyAddress(inputValue)) ||
addressError
}
/>
);
};
1 change: 1 addition & 0 deletions features/rewards/components/addressInput/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export type AddressInputProps = {
isAddressResolving: boolean;
handleInputChange: (value: string) => void;
address: string;
addressError: string;
};
10 changes: 8 additions & 2 deletions features/rewards/features/top-card/top-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ const INPUT_DESC_TEXT =
'Current balance may differ from last balance in the table due to rounding.';

export const TopCard: FC = () => {
const { address, isAddressResolving, inputValue, setInputValue } =
useRewardsHistory();
const {
address,
addressError,
isAddressResolving,
inputValue,
setInputValue,
} = useRewardsHistory();

return (
<Block color="accent" style={{ padding: 0 }}>
<InputWrapper data-testid="inputSection" color="accent">
<ThemeProvider theme={themeDark}>
<AddressInput
address={address}
addressError={addressError}
inputValue={inputValue}
handleInputChange={setInputValue}
isAddressResolving={isAddressResolving}
Expand Down
23 changes: 20 additions & 3 deletions features/rewards/hooks/useGetCurrentAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useCurrentStaticRpcProvider } from 'shared/hooks/use-current-static-rpc

type UseGetCurrentAddress = () => {
address: string;
addressError: string;
inputValue: string;
isAddressResolving: boolean;
setInputValue: (value: string) => void;
Expand All @@ -21,6 +22,7 @@ export const useGetCurrentAddress: UseGetCurrentAddress = () => {
}, []);
const [isAddressResolving, setIsAddressResolving] = useState(false);
const [address, setAddress] = useState('');
const [addressError, setAddressError] = useState('');

const { account } = useSDK();
const { staticRpcProvider } = useCurrentStaticRpcProvider();
Expand All @@ -29,12 +31,26 @@ export const useGetCurrentAddress: UseGetCurrentAddress = () => {
const getEnsAddress = useCallback(
async (value: string) => {
setAddress('');
let result: string | null = null;
let error: string | null = null;

setIsAddressResolving(true);
const result = await resolveEns(value, staticRpcProvider);
setIsAddressResolving(false);
try {
result = await resolveEns(value, staticRpcProvider);
} catch (e) {
console.error(e);
error = 'An error happened during ENS name resolving';
} finally {
setIsAddressResolving(false);
}

if (result) setAddress(result);
if (result) {
setAddress(result);
} else if (error) {
setAddressError(error);
} else {
setAddressError("The ENS name entered couldn't be found");
}
},
[staticRpcProvider],
);
Expand Down Expand Up @@ -76,6 +92,7 @@ export const useGetCurrentAddress: UseGetCurrentAddress = () => {

return {
address,
addressError,
inputValue,
isAddressResolving,
setInputValue,
Expand Down
12 changes: 10 additions & 2 deletions providers/rewardsHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type RewardsHistoryValue = {
page: number;
skip: number;
address: string;
addressError: string;
inputValue: string;
isOnlyRewards: boolean;
isUseArchiveExchangeRate: boolean;
Expand Down Expand Up @@ -63,8 +64,13 @@ const RewardsHistoryProvider: FC<PropsWithChildren> = (props) => {
const skip = page * PAGE_ITEMS;
const limit = PAGE_ITEMS;

const { address, inputValue, setInputValue, isAddressResolving } =
useGetCurrentAddress();
const {
address,
addressError,
inputValue,
setInputValue,
isAddressResolving,
} = useGetCurrentAddress();

const { data, error, loading, initialLoading, isLagging } =
useRewardsDataLoad({
Expand Down Expand Up @@ -100,12 +106,14 @@ const RewardsHistoryProvider: FC<PropsWithChildren> = (props) => {
setCurrency,
isAddressResolving,
address,
addressError,
inputValue,
setInputValue,
isLagging,
}),
[
address,
addressError,
currencyObject,
data,
error,
Expand Down

0 comments on commit 1dacde7

Please sign in to comment.