From 1ca7a5cfd180c613093548fdaa4ba7e507a0e081 Mon Sep 17 00:00:00 2001 From: Ben Kremer Date: Wed, 9 Feb 2022 13:01:22 +0100 Subject: [PATCH] feat: adds back `resetApp` --- .../src/contexts/ClientContext.tsx | 60 ++++++++++--------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/dapps/react-dapp-v2/src/contexts/ClientContext.tsx b/dapps/react-dapp-v2/src/contexts/ClientContext.tsx index 56028ab..3ed4f2e 100644 --- a/dapps/react-dapp-v2/src/contexts/ClientContext.tsx +++ b/dapps/react-dapp-v2/src/contexts/ClientContext.tsx @@ -156,41 +156,43 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac } }; - const subscribeToEvents = useCallback( - async (_client: Client) => { - if (typeof _client === "undefined") { - throw new Error("WalletConnect is not initialized"); - } + const resetApp = () => { + setPairings([]); + setSession(undefined); + setBalances({}); + setAccounts([]); + setChains([]); + }; - let _session = {} as SessionTypes.Settled; + const subscribeToEvents = useCallback(async (_client: Client) => { + if (typeof _client === "undefined") { + throw new Error("WalletConnect is not initialized"); + } - if (_client.session.topics.length) { - _session = await _client.session.get(_client.session.topics[0]); - } + let _session = {} as SessionTypes.Settled; - _client.on(CLIENT_EVENTS.pairing.proposal, async (proposal: PairingTypes.Proposal) => { - const { uri } = proposal.signal.params; - console.log("EVENT", "QR Code Modal open"); - QRCodeModal.open(uri, () => { - console.log("EVENT", "QR Code Modal closed"); - }); + if (_client.session.topics.length) { + _session = await _client.session.get(_client.session.topics[0]); + } + + _client.on(CLIENT_EVENTS.pairing.proposal, async (proposal: PairingTypes.Proposal) => { + const { uri } = proposal.signal.params; + console.log("EVENT", "QR Code Modal open"); + QRCodeModal.open(uri, () => { + console.log("EVENT", "QR Code Modal closed"); }); + }); - _client.on(CLIENT_EVENTS.pairing.created, async (proposal: PairingTypes.Settled) => { - if (typeof client === "undefined") return; - setPairings(client.pairing.topics); - }); + _client.on(CLIENT_EVENTS.pairing.created, async (proposal: PairingTypes.Settled) => { + setPairings(_client.pairing.topics); + }); - _client.on(CLIENT_EVENTS.session.deleted, (deletedSession: SessionTypes.Settled) => { - if (deletedSession.topic !== _session?.topic) return; - console.log("EVENT", "session_deleted"); - // TODO: - // this.resetApp(); - window.location.reload(); - }); - }, - [client], - ); + _client.on(CLIENT_EVENTS.session.deleted, (deletedSession: SessionTypes.Settled) => { + if (deletedSession.topic !== _session?.topic) return; + console.log("EVENT", "session_deleted"); + resetApp(); + }); + }, []); const checkPersistedState = useCallback( async (_client: Client) => {