refactor: simplifies onSessionConnected
This commit is contained in:
parent
d675606194
commit
067aca31b8
@ -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");
|
||||
|
@ -102,32 +102,17 @@ 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) => {
|
||||
const onSessionConnected = useCallback(async (incomingSession: SessionTypes.Settled) => {
|
||||
setSession(incomingSession);
|
||||
onSessionUpdate(
|
||||
incomingSession.state.accounts,
|
||||
incomingSession.permissions.blockchain.chains,
|
||||
);
|
||||
},
|
||||
[onSessionUpdate],
|
||||
);
|
||||
setChains(incomingSession.permissions.blockchain.chains);
|
||||
setAccounts(incomingSession.state.accounts);
|
||||
await getAccountBalances(incomingSession.state.accounts);
|
||||
}, []);
|
||||
|
||||
const connect = useCallback(
|
||||
async (pairing?: { topic: string }) => {
|
||||
@ -135,10 +120,6 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
|
||||
throw new Error("WalletConnect is not initialized");
|
||||
}
|
||||
console.log("connect", pairing);
|
||||
// TODO:
|
||||
// if (modal === "pairing") {
|
||||
// closeModal();
|
||||
// }
|
||||
try {
|
||||
const supportedNamespaces: string[] = [];
|
||||
chains.forEach(chainId => {
|
||||
|
Loading…
Reference in New Issue
Block a user