Skip to content

Commit

Permalink
Do not update wallet name if is the same as cached one
Browse files Browse the repository at this point in the history
  • Loading branch information
xpaczka committed Dec 13, 2023
1 parent d4a4b97 commit d28514c
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/shared/hooks/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
connectArbitrumProviderFallback,
fetchPopulation,
updateConnectedWallet,
selectWalletName,
} from "redux-state"
import {
ARBITRUM_SEPOLIA,
Expand Down Expand Up @@ -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])
}

0 comments on commit d28514c

Please sign in to comment.