From dc58ed12669d9fa52e57df5de06e53be3ee64c2a Mon Sep 17 00:00:00 2001 From: Ben Kremer Date: Fri, 4 Feb 2022 12:26:22 +0100 Subject: [PATCH] fix: ensure `session` can be referenced when creating event handlers --- dapps/react-dapp-v2/src/HooksApp.tsx | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/dapps/react-dapp-v2/src/HooksApp.tsx b/dapps/react-dapp-v2/src/HooksApp.tsx index 9130e15..d1cb012 100644 --- a/dapps/react-dapp-v2/src/HooksApp.tsx +++ b/dapps/react-dapp-v2/src/HooksApp.tsx @@ -26,7 +26,6 @@ import { DEFAULT_PROJECT_ID, DEFAULT_RELAY_URL, DEFAULT_TEST_CHAINS, - DEFAULT_CHAINS, } from "./constants"; import { apiGetAccountAssets, @@ -142,9 +141,8 @@ export default function App() { const openRequestModal = () => setModal("request"); const init = async () => { - setLoading(true); - try { + setLoading(true); await loadChainData(); const _client = await Client.init({ @@ -153,7 +151,7 @@ export default function App() { projectId: DEFAULT_PROJECT_ID, }); setClient(_client); - subscribeToEvents(_client); + await subscribeToEvents(_client); await checkPersistedState(_client); } catch (err) { throw err; @@ -166,11 +164,17 @@ export default function App() { init(); }, []); - const subscribeToEvents = (_client: Client) => { + const subscribeToEvents = async (_client: Client) => { if (typeof _client === "undefined") { throw new Error("WalletConnect is not initialized"); } + let _session = {} as SessionTypes.Settled; + + 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; setUri(uri); @@ -186,10 +190,11 @@ export default function App() { }); _client.on(CLIENT_EVENTS.session.deleted, (deletedSession: SessionTypes.Settled) => { - if (deletedSession.topic !== session?.topic) return; + if (deletedSession.topic !== _session?.topic) return; console.log("EVENT", "session_deleted"); // TODO: // this.resetApp(); + window.location.reload(); }); };