From 4871ba1843190e4d3107dedcbf8bf00542757e77 Mon Sep 17 00:00:00 2001 From: Chid Gilovitz Date: Tue, 10 Oct 2023 14:58:47 +0800 Subject: [PATCH] Fix Portal sync watched accounts bug (#1120) * fix: talisman hostname check for portal url * fix: account connection UI ignores talisman portal --- apps/extension/src/core/page.ts | 21 +++++++++++++++---- .../src/ui/apps/popup/pages/Connect.tsx | 5 ++--- .../src/ui/domains/Site/ConnectedAccounts.tsx | 16 +++++++------- .../src/ui/hooks/useAccountsForSite.ts | 8 +++++++ .../src/ui/hooks/useAuthorisedSiteById.tsx | 4 ++-- 5 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 apps/extension/src/ui/hooks/useAccountsForSite.ts diff --git a/apps/extension/src/core/page.ts b/apps/extension/src/core/page.ts index 13aa096fc9..b0ca449ed8 100644 --- a/apps/extension/src/core/page.ts +++ b/apps/extension/src/core/page.ts @@ -39,10 +39,23 @@ const enable = async (origin: string): Promise => { return new TalismanInjected(messageService.sendMessage) as Injected } -export const isTalismanHostname = (hostname: string | undefined) => - hostname === TALISMAN_WEB_APP_DOMAIN || - (DEBUG && hostname?.endsWith(".talisman.pages.dev")) || - (DEBUG && ["localhost", "127.0.0.1"].includes(hostname ?? "")) +export const isTalismanUrl = (url: string | undefined) => { + if (!url) return false + try { + const hostname = new URL(url).hostname + return isTalismanHostname(hostname) + } catch (e) { + return false + } +} + +export const isTalismanHostname = (hostname: string | undefined) => { + return ( + hostname === TALISMAN_WEB_APP_DOMAIN || + (DEBUG && hostname?.endsWith(".talisman.pages.dev")) || + (DEBUG && ["localhost", "127.0.0.1"].includes(hostname ?? "")) + ) +} function inject() { // inject substrate wallet provider diff --git a/apps/extension/src/ui/apps/popup/pages/Connect.tsx b/apps/extension/src/ui/apps/popup/pages/Connect.tsx index 69ec1df87a..0047a45eb4 100644 --- a/apps/extension/src/ui/apps/popup/pages/Connect.tsx +++ b/apps/extension/src/ui/apps/popup/pages/Connect.tsx @@ -1,6 +1,6 @@ import { ProviderType } from "@core/domains/sitesAuthorised/types" import { KnownRequestIdOnly } from "@core/libs/requests/types" -import { isTalismanHostname } from "@core/page" +import { isTalismanUrl } from "@core/page" import { AppPill } from "@talisman/components/AppPill" import { notify } from "@talisman/components/Notifications" import useSet from "@talisman/hooks/useSet" @@ -61,7 +61,7 @@ export const Connect: FC<{ className?: string }> = ({ className }) => { const authRequest = useRequest(id) const { popupOpenEvent } = useAnalytics() const accountsReady = useAccountsSubscribe() // hack to prevent no accounts drawer flashing - const allAccounts = useAccounts(isTalismanHostname(authRequest?.url) ? "all" : "owned") + const allAccounts = useAccounts(isTalismanUrl(authRequest?.url) ? "all" : "owned") const { items: connected, toggle, set, clear } = useSet() const ethereum = !!authRequest?.request?.ethereum @@ -158,7 +158,6 @@ export const Connect: FC<{ className?: string }> = ({ className }) => { - {evmAccounts.map(([acc, isConnected], idx) => ( + {activeAccounts.map(([acc, isConnected], idx) => ( {!!idx && } = ({ site }) => { const AccountSeparator = () =>
const EthAccounts: FC<{ site: AuthorizedSite | null }> = ({ site }) => { - const accounts = useAccounts("owned") - const evmAccounts = useMemo( + const accounts = useAccountsForSite(site) + const activeAccounts = useMemo( () => accounts .filter((acc) => acc.type === "ethereum") @@ -102,7 +102,7 @@ const EthAccounts: FC<{ site: AuthorizedSite | null }> = ({ site }) => { return ( <> - {evmAccounts.map(([acc, isConnected], idx) => ( + {activeAccounts.map(([acc, isConnected], idx) => ( {!!idx && } { + return useAccounts(isTalismanUrl(site?.url) ? "all" : "owned") +} diff --git a/apps/extension/src/ui/hooks/useAuthorisedSiteById.tsx b/apps/extension/src/ui/hooks/useAuthorisedSiteById.tsx index b2f6176894..0972d0871b 100644 --- a/apps/extension/src/ui/hooks/useAuthorisedSiteById.tsx +++ b/apps/extension/src/ui/hooks/useAuthorisedSiteById.tsx @@ -5,7 +5,7 @@ import { AuthorizedSiteId, ProviderType, } from "@core/domains/sitesAuthorised/types" -import { isTalismanHostname } from "@core/page" +import { isTalismanUrl } from "@core/page" import { api } from "@ui/api" import { useCallback, useEffect, useMemo, useState } from "react" @@ -16,7 +16,7 @@ const useAuthorisedSiteById = (id: AuthorizedSiteId, type: ProviderType) => { const sites = useAuthorisedSites() const availableAddresses = useAccountAddresses( type === "ethereum", - isTalismanHostname(sites[id]?.url) ? "all" : "owned" + isTalismanUrl(sites[id]?.url) ? "all" : "owned" ) const connected = useMemo(() => {