refactor: simplifies onSessionConnected

This commit is contained in:
Ben Kremer 2022-02-08 11:15:05 +01:00
parent d675606194
commit 067aca31b8
2 changed files with 15 additions and 27 deletions

View File

@ -1,4 +1,4 @@
import React, { useState } from "react"; import React, { useEffect, useState } from "react";
import { version } from "@walletconnect/client/package.json"; import { version } from "@walletconnect/client/package.json";
// import { formatDirectSignDoc, stringifySignDocValues } from "cosmos-wallet"; // import { formatDirectSignDoc, stringifySignDocValues } from "cosmos-wallet";
@ -60,6 +60,13 @@ export default function App() {
rpcResult, rpcResult,
} = useJsonRpc(); } = useJsonRpc();
useEffect(() => {
// Close the pairing modal after a session is established.
if (session && modal === "pairing") {
closeModal();
}
}, [session, modal]);
const onConnect = () => { const onConnect = () => {
if (typeof client === "undefined") { if (typeof client === "undefined") {
throw new Error("WalletConnect is not initialized"); throw new Error("WalletConnect is not initialized");

View File

@ -102,43 +102,24 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
if (typeof session !== "undefined") return; if (typeof session !== "undefined") return;
// populates existing session to state (assume only the top one) // populates existing session to state (assume only the top one)
if (_client.session.topics.length) { if (_client.session.topics.length) {
const session = await _client.session.get(_client.session.topics[0]); const _session = await _client.session.get(_client.session.topics[0]);
const chains = session.state.accounts.map(account => onSessionConnected(_session);
account.split(":").slice(0, -1).join(":"),
);
setAccounts(session.state.accounts);
setChains(chains);
onSessionConnected(session);
} }
}; };
const onSessionUpdate = useCallback(async (accounts: string[], chains: string[]) => { const onSessionConnected = useCallback(async (incomingSession: SessionTypes.Settled) => {
setChains(chains); setSession(incomingSession);
setAccounts(accounts); setChains(incomingSession.permissions.blockchain.chains);
await getAccountBalances(accounts); 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( const connect = useCallback(
async (pairing?: { topic: string }) => { async (pairing?: { topic: string }) => {
if (typeof client === "undefined") { if (typeof client === "undefined") {
throw new Error("WalletConnect is not initialized"); throw new Error("WalletConnect is not initialized");
} }
console.log("connect", pairing); console.log("connect", pairing);
// TODO:
// if (modal === "pairing") {
// closeModal();
// }
try { try {
const supportedNamespaces: string[] = []; const supportedNamespaces: string[] = [];
chains.forEach(chainId => { chains.forEach(chainId => {