diff --git a/components/Accounts.tsx b/components/Accounts.tsx index ee3c88b..d27bc5b 100644 --- a/components/Accounts.tsx +++ b/components/Accounts.tsx @@ -12,6 +12,7 @@ import HDPathDialog from './HDPathDialog'; import AccountDetails from './AccountDetails'; import { useAccounts } from '../context/AccountsContext'; import { web3wallet } from '../utils/wallet-connect/WalletConnectUtils'; +import { usePrevious } from '../hooks/usePrevious'; const Accounts = ({ network, @@ -22,7 +23,8 @@ const Accounts = ({ useNavigation>(); const { accounts, setAccounts } = useAccounts(); - + const prevEthAccountsRef = usePrevious(accounts.ethAccounts); + const prevCosmosAccountsRef = usePrevious(accounts.cosmosAccounts); const [expanded, setExpanded] = useState(false); const [isAccountCreating, setIsAccountCreating] = useState(false); const [hdDialog, setHdDialog] = useState(false); @@ -52,30 +54,40 @@ const Accounts = ({ useEffect(() => { const updateSessions = async () => { - const sessions = web3wallet.getActiveSessions(); - for (const sessionId in sessions) { - const session = sessions[sessionId]; + const sessions = web3wallet?.getActiveSessions() || {}; + + for (const topic in sessions) { + const session = sessions[topic]; const requiredNamespaces = session.requiredNamespaces; const namespaces = session.namespaces; - if (namespaces.hasOwnProperty('eip155')) { + if ( + namespaces.hasOwnProperty('eip155') && + prevEthAccountsRef !== accounts.ethAccounts + ) { requiredNamespaces.eip155.chains?.forEach(chainId => { namespaces.eip155.accounts = accounts.ethAccounts.map( ethAccount => `${chainId}:${ethAccount.address}`, ); }); + await web3wallet.updateSession({ topic, namespaces }); } - if (namespaces.hasOwnProperty('cosmos')) { + + if ( + namespaces.hasOwnProperty('cosmos') && + prevCosmosAccountsRef !== accounts.cosmosAccounts + ) { requiredNamespaces?.cosmos.chains?.forEach(chainId => { namespaces.cosmos.accounts = accounts.cosmosAccounts.map( cosmosAccount => `${chainId}:${cosmosAccount.address}`, ); }); + await web3wallet.updateSession({ topic, namespaces }); } - await web3wallet.updateSession({ topic: sessionId, namespaces }); } }; updateSessions(); + // eslint-disable-next-line react-hooks/exhaustive-deps }, [accounts]); const addAccountHandler = async () => { diff --git a/components/SignRequest.tsx b/components/SignRequest.tsx index 1c53a16..bf5f7fe 100644 --- a/components/SignRequest.tsx +++ b/components/SignRequest.tsx @@ -39,7 +39,9 @@ const SignRequest = ({ route }: SignRequestProps) => { useNavigation>(); const isCosmosSignDirect = useMemo(() => { - const requestParams = route.params!.requestEvent.params.request; + const requestParams = route.params!.requestEvent + ? route.params!.requestEvent.params.request + : {}; return requestParams.method === 'cosmos_signDirect'; }, [route.params]); diff --git a/components/WalletConnect.tsx b/components/WalletConnect.tsx index 90585c1..9201a8c 100644 --- a/components/WalletConnect.tsx +++ b/components/WalletConnect.tsx @@ -16,7 +16,7 @@ export default function WalletConnect() { topic: sessionId, reason: getSdkError('USER_DISCONNECTED'), }); - const sessions = web3wallet.getActiveSessions(); + const sessions = web3wallet?.getActiveSessions() || {}; setActiveSessions(sessions); return; }; @@ -34,31 +34,29 @@ export default function WalletConnect() { Active Sessions - {Object.entries(activeSessions).map( - ([sessionId, session], index) => ( - ( - - )} - right={() => ( - disconnect(sessionId)} - style={{ display: 'flex', justifyContent: 'center' }}> - - - )} - /> - ), - )} + {Object.entries(activeSessions).map(([sessionId, session]) => ( + ( + + )} + right={() => ( + disconnect(sessionId)} + style={{ display: 'flex', justifyContent: 'center' }}> + + + )} + /> + ))} ) : ( diff --git a/hooks/usePrevious.ts b/hooks/usePrevious.ts new file mode 100644 index 0000000..42a8bfb --- /dev/null +++ b/hooks/usePrevious.ts @@ -0,0 +1,9 @@ +import { useEffect, useRef } from 'react'; + +export function usePrevious(value: T): T | undefined { + const ref = useRef(value); + useEffect(() => { + ref.current = value; + }, [value]); + return ref.current; +} diff --git a/index.js b/index.js index 37d8a67..1b0e7b3 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,7 @@ export default function Main() { }, }; return ( - +