|
1 | 1 | import { WalletType } from '@bgd-labs/frontend-web3-utils'; |
2 | 2 | import { Box } from '@mui/system'; |
3 | | -import React from 'react'; |
| 3 | +import React, { useState } from 'react'; |
4 | 4 | import { Field, Form } from 'react-final-form'; |
| 5 | +import { isAddress } from 'viem'; |
5 | 6 |
|
6 | 7 | import { useStore } from '../../../store/ZustandStoreProvider'; |
7 | 8 | import { BigButton, Input } from '../../../ui'; |
8 | 9 | import { texts } from '../../../ui/utils/texts'; |
9 | 10 | import { appConfig } from '../../../utils/appConfig'; |
| 11 | +import { isEnsName } from '../../utils/ensHelpers'; |
10 | 12 |
|
11 | 13 | export function ImpersonatedForm() { |
12 | 14 | const impersonated = useStore((store) => store.impersonated); |
13 | 15 | const setImpersonated = useStore((store) => store.setImpersonated); |
14 | 16 | const connectWallet = useStore((store) => store.connectWallet); |
| 17 | + const fetchAddressByEnsName = useStore( |
| 18 | + (store) => store.fetchAddressByEnsName, |
| 19 | + ); |
| 20 | + |
| 21 | + const [isResolving, setIsResolving] = useState(false); |
| 22 | + const [error, setError] = useState<string | undefined>(); |
15 | 23 |
|
16 | 24 | const handleFormSubmit = async ({ |
17 | 25 | impersonatedAddress, |
18 | 26 | }: { |
19 | 27 | impersonatedAddress: string; |
20 | 28 | }) => { |
21 | | - setImpersonated(impersonatedAddress); |
22 | | - await connectWallet(WalletType.Impersonated, appConfig.govCoreChainId); |
| 29 | + setError(undefined); |
| 30 | + setIsResolving(true); |
| 31 | + |
| 32 | + try { |
| 33 | + let resolvedAddress = impersonatedAddress; |
| 34 | + |
| 35 | + if (isEnsName(impersonatedAddress)) { |
| 36 | + const resolved = await fetchAddressByEnsName(impersonatedAddress); |
| 37 | + if (!resolved) { |
| 38 | + setError('Invalid ENS name or address not found'); |
| 39 | + setIsResolving(false); |
| 40 | + return; |
| 41 | + } |
| 42 | + resolvedAddress = resolved; |
| 43 | + } else if (!isAddress(impersonatedAddress)) { |
| 44 | + setError('Invalid address format'); |
| 45 | + setIsResolving(false); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + setImpersonated(resolvedAddress); |
| 50 | + await connectWallet(WalletType.Impersonated, appConfig.govCoreChainId); |
| 51 | + } catch (err) { |
| 52 | + setError('Failed to resolve address'); |
| 53 | + } finally { |
| 54 | + setIsResolving(false); |
| 55 | + } |
23 | 56 | }; |
24 | 57 |
|
25 | 58 | return ( |
@@ -59,14 +92,32 @@ export function ImpersonatedForm() { |
59 | 92 | )} |
60 | 93 | </Field> |
61 | 94 |
|
| 95 | + {error && ( |
| 96 | + <Box |
| 97 | + sx={{ |
| 98 | + color: '$error', |
| 99 | + fontSize: 12, |
| 100 | + mt: 8, |
| 101 | + textAlign: 'center', |
| 102 | + }}> |
| 103 | + {error} |
| 104 | + </Box> |
| 105 | + )} |
| 106 | + |
62 | 107 | <Box |
63 | 108 | sx={{ |
64 | 109 | display: 'flex', |
65 | 110 | alignItems: 'center', |
66 | 111 | justifyContent: 'center', |
67 | 112 | }}> |
68 | | - <BigButton alwaysWithBorders type="submit" css={{ mt: 60 }}> |
69 | | - {texts.walletConnect.impersonatedButtonTitle} |
| 113 | + <BigButton |
| 114 | + alwaysWithBorders |
| 115 | + type="submit" |
| 116 | + css={{ mt: 60 }} |
| 117 | + disabled={isResolving}> |
| 118 | + {isResolving |
| 119 | + ? 'Resolving...' |
| 120 | + : texts.walletConnect.impersonatedButtonTitle} |
70 | 121 | </BigButton> |
71 | 122 | </Box> |
72 | 123 | </Box> |
|
0 commit comments