From 3303802ef47db21f9981d03ef134e8d48eb5f814 Mon Sep 17 00:00:00 2001 From: katspaugh Date: Thu, 21 Sep 2023 20:50:52 +0200 Subject: [PATCH] Add a session_delete handler --- .../walletconnect/SessionManager/index.tsx | 15 +++++++++------ src/services/walletconnect/WalletConnectWallet.ts | 13 ++++++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/components/walletconnect/SessionManager/index.tsx b/src/components/walletconnect/SessionManager/index.tsx index d7010d7902..ed9a059b1b 100644 --- a/src/components/walletconnect/SessionManager/index.tsx +++ b/src/components/walletconnect/SessionManager/index.tsx @@ -14,14 +14,19 @@ const SessionManager = () => { const { safe, safeAddress } = useSafeInfo() const { chainId } = safe const { walletConnect, error: walletConnectError } = useContext(WalletConnectContext) - const [sessions, setSessions] = useState>(walletConnect.getActiveSessions() || {}) + const [sessions, setSessions] = useState>(walletConnect.getActiveSessions()) const [proposal, setProposal] = useState() const [error, setError] = useState() // Subscribe to session proposals useEffect(() => { - return walletConnect.addOnSessionPropose(async (event) => { - setProposal(event) + return walletConnect.onSessionPropose(setProposal) + }, [walletConnect]) + + // Subscribe to session deletes + useEffect(() => { + return walletConnect.onSessionDelete(() => { + setSessions(walletConnect.getActiveSessions()) }) }, [walletConnect]) @@ -37,8 +42,7 @@ const SessionManager = () => { } setProposal(undefined) - // Update sessions - setSessions(walletConnect.getActiveSessions() || {}) + setSessions(walletConnect.getActiveSessions()) }, [proposal, walletConnect]) // On session reject @@ -59,7 +63,6 @@ const SessionManager = () => { const onDisconnect = async (session: SessionTypes.Struct) => { try { await walletConnect.disconnectSession(session) - setSessions(walletConnect.getActiveSessions() || {}) } catch (error) { setError(asError(error)) } diff --git a/src/services/walletconnect/WalletConnectWallet.ts b/src/services/walletconnect/WalletConnectWallet.ts index 94831fb8df..5222ae8cb3 100644 --- a/src/services/walletconnect/WalletConnectWallet.ts +++ b/src/services/walletconnect/WalletConnectWallet.ts @@ -144,7 +144,7 @@ class WalletConnectWallet { /** * Subscribe to session proposals */ - public addOnSessionPropose(onSessionPropose: (e: Web3WalletTypes.SessionProposal) => void) { + public onSessionPropose(onSessionPropose: (e: Web3WalletTypes.SessionProposal) => void) { // Subscribe to the session proposal event this.web3Wallet?.on('session_proposal', onSessionPropose) @@ -154,6 +154,17 @@ class WalletConnectWallet { } } + /** + * Subscribe to session delete + */ + public onSessionDelete(handler: () => void) { + this.web3Wallet?.on('session_delete', handler) + + return () => { + this.web3Wallet?.off('session_delete', handler) + } + } + /** * Disconnect a session */