Skip to content

Commit

Permalink
added wallet key change event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
Teja2045 committed Oct 26, 2023
1 parent 5ccf740 commit 1e98f52
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
34 changes: 32 additions & 2 deletions frontend/src/components/ConnectWalletButton.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use client";
import React, { useState } from "react";
import React, { useEffect, useState } from "react";
import { connectWalletV1 } from "../services/walletService";
import { networks } from "../utils/chainsInfo";
import Image from "next/image";
import Walletpage from "./popups/WalletPage";
import { isConnected } from "staking/utils/localStorage";
import { getWalletName, isConnected } from "staking/utils/localStorage";

export const ConnectWalletButton = ({
children,
Expand All @@ -25,6 +25,36 @@ export const ConnectWalletButton = ({
setConnected,
});
};

useEffect(() => {
const walletName = getWalletName();
const accountChangeListener = () => {
setTimeout(
() =>
connectWalletV1({
mainnets: networks,
testnets: [],
walletName: walletName,
setConnected,
}),
1000
);
window.location.reload()
};

window.addEventListener(
`${walletName}_keystorechange`,
accountChangeListener
);

return () => {
window.removeEventListener(
`${walletName}_keystorechange`,
accountChangeListener
);
};
}, []);

return connected ? (
<>{children}</>
) : (
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/utils/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export function setWalletName(walletName: string) {
localStorage.setItem(KEY_WALLET_NAME, walletName);
}

export function getWalletName(): string {
return localStorage.getItem(KEY_WALLET_NAME) || "";
}

export function removeWalletName() {
localStorage.removeItem(KEY_WALLET_NAME);
}
Expand Down

0 comments on commit 1e98f52

Please sign in to comment.