From d28514c52181397e52602f113588d6fc62b5434b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Paczy=C5=84ski?= Date: Wed, 13 Dec 2023 10:54:26 +0100 Subject: [PATCH] Do not update wallet name if is the same as cached one --- src/shared/hooks/wallets.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/shared/hooks/wallets.ts b/src/shared/hooks/wallets.ts index 87d74405..7f205777 100644 --- a/src/shared/hooks/wallets.ts +++ b/src/shared/hooks/wallets.ts @@ -14,6 +14,7 @@ import { connectArbitrumProviderFallback, fetchPopulation, updateConnectedWallet, + selectWalletName, } from "redux-state" import { ARBITRUM_SEPOLIA, @@ -278,33 +279,33 @@ export function useWalletChange() { } export function useCachedWalletName() { - const walletAddress = useDappSelector(selectWalletAddress) + const address = useDappSelector(selectWalletAddress) + const walletName = useDappSelector(selectWalletName) const dispatch = useDappDispatch() useEffect(() => { const handleCachedNamesUpdate = () => { - if (!walletAddress) return + if (!address) return const cachedNames = localStorage.getItem(LOCAL_STORAGE_CACHED_NAMES) if (!cachedNames) return const parsedCachedNames: CachedNames = JSON.parse(cachedNames) - const { ens, uns } = parsedCachedNames[walletAddress] + const { ens, uns } = parsedCachedNames[address] - if (uns) - dispatch( - updateConnectedWallet({ address: walletAddress, name: uns.name }) - ) + if (ens || uns) { + // If cached name and redux wallet name are the same do not dispatch wallet update action + if (walletName === ens?.name || walletName === uns?.name) return - if (ens) dispatch( - updateConnectedWallet({ address: walletAddress, name: ens.name }) + updateConnectedWallet({ address, name: ens?.name ?? uns?.name }) ) + } } handleCachedNamesUpdate() window.addEventListener("storage", handleCachedNamesUpdate) return () => window.removeEventListener("storage", handleCachedNamesUpdate) - }, [walletAddress, dispatch]) + }, [address, walletName, dispatch]) }