Skip to content

Commit

Permalink
remove listener for account change
Browse files Browse the repository at this point in the history
  • Loading branch information
Jennievon committed Dec 11, 2023
1 parent 4eb837e commit dcddaa3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import {
TwitterLogoIcon,
DiscordLogoIcon,
} from "@radix-ui/react-icons";
import { useWalletConnection } from "../providers/wallet-provider";

export default function Footer() {
const { version } = useWalletConnection();

return (
<div className="border-t px-2">
<div className="flex h-16 items-center px-4">
Expand All @@ -33,6 +36,11 @@ export default function Footer() {
<DiscordLogoIcon />
</a>
</div>
<div className="flex-1 flex items-center justify-center">
<h3 className="text-sm text-muted-foreground">
Version: {version || "Unknown"}
</h3>
</div>
<div className="flex items-center space-x-4">
<Link
href="/docs/privacy"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import { socialLinks } from "../../../lib/constants";
import { Skeleton } from "../../ui/skeleton";

const Connected = () => {
const { accounts, connectAccount, version, revokeAccounts } =
useWalletConnection();
const { accounts, connectAccount, revokeAccounts } = useWalletConnection();

return (
<>
Expand Down Expand Up @@ -84,12 +83,6 @@ const Connected = () => {
)}
</TableBody>
</Table>

<div className="mt-8">
<h3 className="text-sm text-muted-foreground">
Version: {version || "Unknown"}
</h3>
</div>
</>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,40 +43,22 @@ export const WalletConnectionProvider = ({
const [accounts, setAccounts] = useState<Account[] | null>(null);
const [provider, setProvider] = useState({} as ethers.providers.Web3Provider);

useEffect(() => {
initialize();
ethereum.on("accountsChanged", handleAccountsChanged);

return () => {
ethereum.removeListener("accountsChanged", handleAccountsChanged);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const handleAccountsChanged = async () => {
if (!ethereum) {
return;
}
const status = await ethService.isUserConnectedToTenChain(token);
await fetchUserAccounts();
setWalletConnected(status);
};

const initialize = async () => {
if (!ethereum) {
showToast(
ToastType.DESTRUCTIVE,
"Please install Metamask to connect your wallet."
);
return;
}
try {
if (!ethereum) {
showToast(
ToastType.DESTRUCTIVE,
"Please install Metamask to connect your wallet."
);
return;
}
const providerInstance = new ethers.providers.Web3Provider(ethereum);
setProvider(providerInstance);
await ethService.checkIfMetamaskIsLoaded(providerInstance);

const fetchedToken = await getToken(providerInstance);
setToken(fetchedToken);

const status = await ethService.isUserConnectedToTenChain(fetchedToken);
setWalletConnected(status);

Expand Down Expand Up @@ -179,6 +161,11 @@ export const WalletConnectionProvider = ({
}
};

useEffect(() => {
initialize();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const walletConnectionContextValue: WalletConnectionContextType = {
walletConnected,
accounts,
Expand Down

0 comments on commit dcddaa3

Please sign in to comment.