Skip to content

Commit

Permalink
refresh when new account is added to improve UX
Browse files Browse the repository at this point in the history
  • Loading branch information
zkokelj committed Dec 9, 2024
1 parent 99c6f58 commit a2b89f0
Showing 1 changed file with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ export const WalletConnectionProvider = ({
setVersion(await fetchVersion());
} catch (error) {
showToast(
ToastType.DESTRUCTIVE,
error instanceof Error ? error.message : "Error initializing wallet connection. Please refresh the page."
);
ToastType.DESTRUCTIVE,
error instanceof Error ? error.message : "Error initializing wallet connection. Please refresh the page."
);
} finally {
setLoading(false);
}
Expand Down Expand Up @@ -189,16 +189,26 @@ export const WalletConnectionProvider = ({
setProvider(providerInstance);
initialize(providerInstance);

ethereum.on("accountsChanged", fetchUserAccounts);
const handleAccountsChanged = async (accounts: string[]) => {
if (accounts.length === 0) {
setAccounts(null);
setWalletConnected(false);
setToken("");
} else {
window.location.reload();
}
};

ethereum.on("accountsChanged", handleAccountsChanged);

return () => {
if (ethereum && ethereum.removeListener) {
ethereum.removeListener("accountsChanged", handleAccountsChanged);
}
};
} else {
setLoading(false);
}

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

Expand Down

0 comments on commit a2b89f0

Please sign in to comment.