From 5249b194775dfd70e11e0e330326b7a772da6065 Mon Sep 17 00:00:00 2001 From: Darren Kelly Date: Fri, 27 Oct 2023 10:23:29 +0100 Subject: [PATCH] fix: show message when wallet is not configured --- apps/sample-react-app/src/Hooks/useCounter.ts | 46 ++++++++++++------- .../src/Screens/components/Counter.tsx | 6 +-- packages/wallet-kit/src/signer-manager.ts | 2 + 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/apps/sample-react-app/src/Hooks/useCounter.ts b/apps/sample-react-app/src/Hooks/useCounter.ts index 628d349e..f4f8708b 100644 --- a/apps/sample-react-app/src/Hooks/useCounter.ts +++ b/apps/sample-react-app/src/Hooks/useCounter.ts @@ -31,6 +31,7 @@ interface UseCounter { increment: () => Promise; status: IncrementStatus; address: string; + error: string | null; } export const useCounter = (): UseCounter => { @@ -38,6 +39,7 @@ export const useCounter = (): UseCounter => { const [count, setCount] = useState(0); const [status, setStatus] = useState('idle'); + const [error, setError] = useState(null); const contract = useMemo( () => thor.account('0x8384738c995d49c5b692560ae688fc8b51af1059'), @@ -55,22 +57,34 @@ export const useCounter = (): UseCounter => { }, [setValue]); const increment = useCallback(async (): Promise => { - setStatus('in-wallet'); - - await contract - .method(_increment) - .transact() - .delegate('https://sponsor-testnet.vechain.energy/by/90') - .request(); - - setStatus('pending'); - - await thor.ticker().next(); - - await setValue() - .then(() => setStatus('idle')) - .catch(() => setStatus('error')); + setError(null); + + try { + setStatus('in-wallet'); + + await contract + .method(_increment) + .transact() + .delegate('https://sponsor-testnet.vechain.energy/by/90') + .request(); + + setStatus('pending'); + + await thor.ticker().next(); + + await setValue() + .then(() => setStatus('idle')) + .catch(() => setStatus('error')); + } catch (e) { + setStatus('error'); + if (e instanceof Error) { + setError(e.message); + } else { + setError('Unknown error'); + } + throw e; + } }, [thor, contract, setValue]); - return { count, increment, status, address: contract.address }; + return { count, increment, status, address: contract.address, error }; }; diff --git a/apps/sample-react-app/src/Screens/components/Counter.tsx b/apps/sample-react-app/src/Screens/components/Counter.tsx index a269a039..97b56f0c 100644 --- a/apps/sample-react-app/src/Screens/components/Counter.tsx +++ b/apps/sample-react-app/src/Screens/components/Counter.tsx @@ -19,7 +19,7 @@ import { AddressButton } from '../../Components'; export const Counter = (): JSX.Element => { const { accountState } = useWallet(); - const { count, increment, status, address } = useCounter(); + const { count, increment, status, address, error } = useCounter(); const toast = useToast(); const incrementCounter = useCallback(() => { @@ -65,7 +65,7 @@ export const Counter = (): JSX.Element => { return ( - There was an unexpected error + {error ? error : 'Unknown error'} ); case 'pending': @@ -78,7 +78,7 @@ export const Counter = (): JSX.Element => { case 'idle': return null; } - }, [status]); + }, [error, status]); return ( diff --git a/packages/wallet-kit/src/signer-manager.ts b/packages/wallet-kit/src/signer-manager.ts index 51060f96..1c075c18 100644 --- a/packages/wallet-kit/src/signer-manager.ts +++ b/packages/wallet-kit/src/signer-manager.ts @@ -28,6 +28,8 @@ class SignerManager implements ConnexSigner { if (signer) { await signer.disconnect?.(); } + + this.currentSource = undefined; } async signTx(