From 067aca31b873ae4c7c9ed4781039f5ba6a9e9ae1 Mon Sep 17 00:00:00 2001 From: Ben Kremer Date: Tue, 8 Feb 2022 11:15:05 +0100 Subject: [PATCH] refactor: simplifies `onSessionConnected` --- dapps/react-dapp-v2/src/HooksApp.tsx | 9 ++++- .../src/contexts/ClientContext.tsx | 33 ++++--------------- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/dapps/react-dapp-v2/src/HooksApp.tsx b/dapps/react-dapp-v2/src/HooksApp.tsx index 626604b..79be62d 100644 --- a/dapps/react-dapp-v2/src/HooksApp.tsx +++ b/dapps/react-dapp-v2/src/HooksApp.tsx @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React, { useEffect, useState } from "react"; import { version } from "@walletconnect/client/package.json"; // import { formatDirectSignDoc, stringifySignDocValues } from "cosmos-wallet"; @@ -60,6 +60,13 @@ export default function App() { rpcResult, } = useJsonRpc(); + useEffect(() => { + // Close the pairing modal after a session is established. + if (session && modal === "pairing") { + closeModal(); + } + }, [session, modal]); + const onConnect = () => { if (typeof client === "undefined") { throw new Error("WalletConnect is not initialized"); diff --git a/dapps/react-dapp-v2/src/contexts/ClientContext.tsx b/dapps/react-dapp-v2/src/contexts/ClientContext.tsx index d1d7b00..8a1146e 100644 --- a/dapps/react-dapp-v2/src/contexts/ClientContext.tsx +++ b/dapps/react-dapp-v2/src/contexts/ClientContext.tsx @@ -102,43 +102,24 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac if (typeof session !== "undefined") return; // populates existing session to state (assume only the top one) if (_client.session.topics.length) { - const session = await _client.session.get(_client.session.topics[0]); - const chains = session.state.accounts.map(account => - account.split(":").slice(0, -1).join(":"), - ); - setAccounts(session.state.accounts); - setChains(chains); - onSessionConnected(session); + const _session = await _client.session.get(_client.session.topics[0]); + onSessionConnected(_session); } }; - const onSessionUpdate = useCallback(async (accounts: string[], chains: string[]) => { - setChains(chains); - setAccounts(accounts); - await getAccountBalances(accounts); + const onSessionConnected = useCallback(async (incomingSession: SessionTypes.Settled) => { + setSession(incomingSession); + setChains(incomingSession.permissions.blockchain.chains); + setAccounts(incomingSession.state.accounts); + await getAccountBalances(incomingSession.state.accounts); }, []); - const onSessionConnected = useCallback( - async (incomingSession: SessionTypes.Settled) => { - setSession(incomingSession); - onSessionUpdate( - incomingSession.state.accounts, - incomingSession.permissions.blockchain.chains, - ); - }, - [onSessionUpdate], - ); - const connect = useCallback( async (pairing?: { topic: string }) => { if (typeof client === "undefined") { throw new Error("WalletConnect is not initialized"); } console.log("connect", pairing); - // TODO: - // if (modal === "pairing") { - // closeModal(); - // } try { const supportedNamespaces: string[] = []; chains.forEach(chainId => {