From bd577de506cb1b4a7550cab3f7f06f65a5c921c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Paczy=C5=84ski?= Date: Tue, 12 Dec 2023 13:20:16 +0100 Subject: [PATCH 1/4] Auto update ENS name on `storage` event --- src/shared/utils/names.ts | 1 + src/ui/Nav/AccountInfo.tsx | 8 ++++++-- src/ui/Nav/index.tsx | 27 +++++++++++++++++++++++++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/shared/utils/names.ts b/src/shared/utils/names.ts index d2d6ad90e..d45b28719 100644 --- a/src/shared/utils/names.ts +++ b/src/shared/utils/names.ts @@ -42,6 +42,7 @@ const addCachedName = ({ name, avatar, address, type }: NameWithProvider) => { }) localStorage.setItem(NAMES_CACHE_STRORAGE_KEY, newCache) + window.dispatchEvent(new Event("storage")) } const resolveENSPromise = (address: string) => diff --git a/src/ui/Nav/AccountInfo.tsx b/src/ui/Nav/AccountInfo.tsx index 6ea0ff9bb..37133a261 100644 --- a/src/ui/Nav/AccountInfo.tsx +++ b/src/ui/Nav/AccountInfo.tsx @@ -10,7 +10,11 @@ import { getRealmColor } from "shared/constants" import Avatar from "shared/components/Media/Avatar" import AccountDropdown from "./AccountDropdown" -export default function AccountInfo() { +type AccountInfoProps = { + accountName: string | null +} + +export default function AccountInfo({ accountName }: AccountInfoProps) { const [isDropdownOpen, setIsDropdownOpen] = useState(false) const dropdownTriggerRef = useRef(null) @@ -41,7 +45,7 @@ export default function AccountInfo() { onClick={() => setIsDropdownOpen((prev) => !prev)} ref={dropdownTriggerRef} > - {name} + {accountName ?? name} (null) + const walletAddress = useDappSelector(selectWalletAddress) + + useEffect(() => { + window.addEventListener("storage", () => { + if (!walletAddress) return + + const cachedNames = localStorage.getItem("taho.cachedNames") + if (!cachedNames) return + + const parsedCachedNames: CachedNames = JSON.parse(cachedNames) + const { ens, uns } = parsedCachedNames[walletAddress] + + if (ens) setAccountName(ens.name) + if (uns) setAccountName(uns.name) + }) + }, [walletAddress]) + return ( <> @@ -30,7 +53,7 @@ export default function Nav(): JSX.Element {
{isConnected ? ( - + ) : (