Reconnect keplr on account change

This commit is contained in:
abefernan
2023-05-19 13:41:52 +02:00
parent c7f7244948
commit 6c360af79f
+13 -3
View File
@@ -5,7 +5,7 @@ import { SigningStargateClient } from "@cosmjs/stargate";
import { assert } from "@cosmjs/utils";
import TransportWebUSB from "@ledgerhq/hw-transport-webusb";
import axios from "axios";
import { useState } from "react";
import { useCallback, useLayoutEffect, useState } from "react";
import { useAppContext } from "../../context/AppContext";
import { getConnectError } from "../../lib/errorHelpers";
import { DbSignature, DbTransaction, WalletAccount } from "../../types";
@@ -41,7 +41,7 @@ const TransactionSigning = (props: Props) => {
const [ledgerSigner, setLedgerSigner] = useState({});
const [loading, setLoading] = useState<LoadingStates>({});
const connectKeplr = async () => {
const connectKeplr = useCallback(async () => {
try {
setLoading((oldLoading) => ({ ...oldLoading, keplr: true }));
assert(state.chain.chainId, "chainId missing");
@@ -76,7 +76,17 @@ const TransactionSigning = (props: Props) => {
} finally {
setLoading((newLoading) => ({ ...newLoading, keplr: false }));
}
};
}, [memberPubkeys, props.signatures, state.chain.chainId]);
useLayoutEffect(() => {
const accountChangeKey = "keplr_keystorechange";
if (walletType === "Keplr") {
window.addEventListener(accountChangeKey, connectKeplr);
} else {
window.removeEventListener(accountChangeKey, connectKeplr);
}
}, [connectKeplr, walletType]);
const connectLedger = async () => {
try {