Skip to content

Commit

Permalink
Add a session_delete handler
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Sep 21, 2023
1 parent f05fc0b commit 1074de3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
10 changes: 6 additions & 4 deletions src/components/walletconnect/ProposalForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ const ProposalForm = ({ proposal, onApprove, onReject }: ProposalFormProps) => {
const requiredChains = requiredNamespaces[EIP155].chains ?? []
const optionalChains = optionalNamespaces[EIP155].chains ?? []

const chainIds = requiredChains
.concat(optionalChains)
.map((chain) => chain.split(':').pop())
.filter((chainId) => configs.some((chain) => chain.chainId === chainId))
const chainIds = configs
.filter((chain) => {
const eipChainId = `${EIP155}:${chain.chainId}`
return requiredChains.includes(eipChainId) || optionalChains.includes(eipChainId)
})
.map((chain) => chain.chainId)

return (
<div className={css.container}>
Expand Down
18 changes: 11 additions & 7 deletions src/components/walletconnect/SessionManager/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ const SessionManager = () => {
const { safe, safeAddress } = useSafeInfo()
const { chainId } = safe
const { walletConnect, error: walletConnectError } = useContext(WalletConnectContext)
const [sessions, setSessions] = useState<Record<string, SessionTypes.Struct>>(walletConnect.getActiveSessions() || {})
const [sessions, setSessions] = useState<Record<string, SessionTypes.Struct>>(walletConnect.getActiveSessions())
const [proposal, setProposal] = useState<Web3WalletTypes.SessionProposal>()
const [error, setError] = useState<Error>()

// 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])

Expand All @@ -37,9 +42,8 @@ const SessionManager = () => {
}

setProposal(undefined)
// Update sessions
setSessions(walletConnect.getActiveSessions() || {})
}, [proposal, walletConnect])
setSessions(walletConnect.getActiveSessions())
}, [proposal, walletConnect, chainId, safeAddress])

// On session reject
const onReject = useCallback(async () => {
Expand All @@ -59,7 +63,7 @@ const SessionManager = () => {
const onDisconnect = async (session: SessionTypes.Struct) => {
try {
await walletConnect.disconnectSession(session)
setSessions(walletConnect.getActiveSessions() || {})
setSessions(walletConnect.getActiveSessions())
} catch (error) {
setError(asError(error))
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/walletconnect/WalletConnectContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createContext, useEffect, useState } from 'react'
import { type ReactNode, createContext, useEffect, useState } from 'react'

import useSafeInfo from '@/hooks/useSafeInfo'
import useSafeWalletProvider from '@/services/safe-wallet-provider/useSafeWalletProvider'
Expand All @@ -15,7 +15,7 @@ export const WalletConnectContext = createContext<{
error: null,
})

export const WalletConnectProvider = ({ children }: { children: JSX.Element }) => {
export const WalletConnectProvider = ({ children }: { children: ReactNode }) => {
const {
safe: { chainId },
safeAddress,
Expand Down
13 changes: 12 additions & 1 deletion src/services/walletconnect/WalletConnectWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
*/
Expand Down

0 comments on commit 1074de3

Please sign in to comment.