From fcbfd377040d37941365ad6c9fb9440d1f9b5e33 Mon Sep 17 00:00:00 2001 From: CodinMaster Date: Mon, 23 Aug 2021 15:14:40 +0530 Subject: [PATCH] Allow network switching, while connected --- src/components/Body/index.js | 81 +++++++++++++++++++++++++++++------- 1 file changed, 67 insertions(+), 14 deletions(-) diff --git a/src/components/Body/index.js b/src/components/Body/index.js index cd144c7..a1a62ed 100644 --- a/src/components/Body/index.js +++ b/src/components/Body/index.js @@ -11,8 +11,10 @@ import { Text, Link, VStack, - Spacer, Select, + useToast, + CircularProgress, + Center, } from "@chakra-ui/react"; import WalletConnect from "@walletconnect/client"; import networkInfo from "./networkInfo"; @@ -20,6 +22,7 @@ import networkInfo from "./networkInfo"; function Body() { const { colorMode } = useColorMode(); const bgColor = { light: "white", dark: "gray.700" }; + const toast = useToast(); const [address, setAddress] = useState(""); const [uri, setUri] = useState(""); @@ -27,6 +30,7 @@ function Body() { const [connector, setConnector] = useState(); const [peerMeta, setPeerMeta] = useState(); const [isConnected, setIsConnected] = useState(false); + const [loading, setLoading] = useState(false); useEffect(() => { const session = getCachedSession(); @@ -35,18 +39,23 @@ function Body() { let _connector = new WalletConnect({ session }); if (_connector.peerMeta) { - setConnector(_connector); - setAddress(_connector.accounts[0]); - setUri(_connector.uri); - setPeerMeta(_connector.peerMeta); - setIsConnected(true); + try { + setConnector(_connector); + setAddress(_connector.accounts[0]); + setUri(_connector.uri); + setPeerMeta(_connector.peerMeta); + setIsConnected(true); - const chainId = _connector.chainId.chainID; - for (let i = 0; i < networkInfo.length; i++) { - if (networkInfo[i].chainID == chainId) { - setChainIdIndex(i); - break; + const chainId = _connector.chainId.chainID; + for (let i = 0; i < networkInfo.length; i++) { + if (networkInfo[i].chainID === chainId) { + setChainIdIndex(i); + break; + } } + } catch { + console.log("Corrupt old session. Starting fresh"); + localStorage.removeItem("walletconnect"); } } } @@ -73,6 +82,7 @@ function Body() { }; const initWalletConnect = async () => { + setLoading(true); try { let _connector = new WalletConnect({ uri }); @@ -83,7 +93,15 @@ function Body() { setConnector(_connector); setUri(_connector.uri); } catch (err) { - throw err; + console.error(err); + toast({ + title: "Couldn't Connect", + description: "Refresh DApp and Connect again", + status: "error", + isClosable: true, + duration: 2000, + }); + setLoading(false); } }; @@ -92,6 +110,9 @@ function Body() { if (connector) { connector.on("session_request", (error, payload) => { + if (loading) { + setLoading(false); + } console.log("EVENT", "session_request"); if (error) { @@ -138,7 +159,7 @@ function Body() { throw error; } - // this.resetApp(); + reset(); }); } }; @@ -165,6 +186,7 @@ function Body() { const killSession = () => { console.log("ACTION", "killSession"); + if (connector) { connector.killSession(); @@ -173,6 +195,12 @@ function Body() { } }; + const reset = () => { + setPeerMeta(null); + setIsConnected(false); + localStorage.removeItem("walletconnect"); + }; + return ( @@ -180,6 +208,7 @@ function Body() { setAddress(e.target.value)} bg={bgColor[colorMode]} @@ -206,8 +235,13 @@ function Body() { value={chainIdIndex} onChange={(e) => { setChainIdIndex(e.target.value); + if (connector && connector.connected) { + connector.updateSession({ + chainId: networkInfo[e.target.value].chainID, + accounts: [address], + }); + } }} - isDisabled={isConnected} > {networkInfo.map((network, i) => (