diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 49a3422..4304844 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -14,7 +14,7 @@ "@fontsource-variable/sora": "^5.2.8", "@noble/hashes": "^2.4.0", "@qrlwallet/connect": "4.0.0", - "@qrlwallet/connect-ui": "0.3.0", + "@qrlwallet/connect-ui": "0.6.0", "@radix-ui/react-slot": "^1.1.2", "@radix-ui/react-tabs": "^1.1.1", "@theqrl/web3": "^2.0.0", @@ -997,9 +997,9 @@ } }, "node_modules/@qrlwallet/connect-ui": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/@qrlwallet/connect-ui/-/connect-ui-0.3.0.tgz", - "integrity": "sha512-kJdhjcRUyzfrMWAvMm4n6eVchyJ+VY8pqyqgIXlGEMfcAaTJpHIseIf/x/8axB0W0i+EFHA9HFySDFvjyFo3+A==", + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/@qrlwallet/connect-ui/-/connect-ui-0.6.0.tgz", + "integrity": "sha512-6gHOzMCD48+82XNQpkZ5lPA/gacyGE4cYNqjuXkLkLjxddK2EUNDF5D1gRGvA6jo4Vht1l0NLbKiEld8GtdMlg==", "license": "MIT", "dependencies": { "qrcode": "^1.5.4" diff --git a/frontend/package.json b/frontend/package.json index 90e0dd4..835aaed 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -23,7 +23,7 @@ "@fontsource-variable/sora": "^5.2.8", "@noble/hashes": "^2.4.0", "@qrlwallet/connect": "4.0.0", - "@qrlwallet/connect-ui": "0.3.0", + "@qrlwallet/connect-ui": "0.6.0", "@radix-ui/react-slot": "^1.1.2", "@radix-ui/react-tabs": "^1.1.1", "@theqrl/web3": "^2.0.0", diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 29abf59..50abd84 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,4 +1,4 @@ -import { useEffect } from "react"; +import { useCallback, useEffect, useSyncExternalStore } from "react"; import { observer } from "mobx-react-lite"; import { BrowserRouter, Route, Routes } from "react-router"; import { AlertTriangle, X } from "lucide-react"; @@ -8,6 +8,7 @@ import { TxBanner } from "@/components/TxBanner"; import { WalletPickerModal } from "@/components/WalletPickerModal"; import { QrPairModal } from "@/components/QrPairModal"; import { RouteSeo } from "@/components/RouteSeo"; +import { ApprovalHint } from "@/components/ApprovalHint"; import { StakePage } from "@/pages/StakePage"; import { WithdrawalsPage } from "@/pages/WithdrawalsPage"; import { StatsPage } from "@/pages/StatsPage"; @@ -22,11 +23,30 @@ const App = observer(() => { void poolStore.init(); }, [poolStore]); + const subscribeApprovals = useCallback( + (listener: () => void) => poolStore.approvals.subscribe(listener), + [poolStore], + ); + const approvalPending = useSyncExternalStore(subscribeApprovals, () => + poolStore.approvals.pending(), + ); + return (
+ {/* The wallet a request is waiting for, named where the user is + looking: a prompt can sit unseen behind the wallet's toolbar icon + or on a paired device. It only informs, so clicks pass through. */} +
+ +
+ {poolStore.rpcError && (
diff --git a/frontend/src/components/ApprovalHint.tsx b/frontend/src/components/ApprovalHint.tsx new file mode 100644 index 0000000..1e9db30 --- /dev/null +++ b/frontend/src/components/ApprovalHint.tsx @@ -0,0 +1,59 @@ +import { useEffect, useLayoutEffect, useRef } from "react"; +import { + defineQrlApprovalHint, + QrlApprovalHint, + QRL_APPROVAL_HINT_TAG, +} from "@qrlwallet/connect-ui/approval"; +import type { ApprovalWallet } from "@qrlwallet/connect-ui/wallets"; + +interface Props { + /** The wallet the open request went to, or null when it is unknown. */ + wallet: ApprovalWallet | null; + /** A wallet request is open and has not been answered. */ + pending: boolean; + className?: string; +} + +const UNKNOWN_WALLET: ApprovalWallet = { + name: "", + icon: null, + kind: "extension", +}; + +/** + * Thin React wrapper around from @qrlwallet/connect-ui. + * The element shows itself once `pending` has lasted its delay, names the + * wallet to open and never opens or raises anything. + */ +export function ApprovalHint({ wallet, pending, className }: Props) { + const hostRef = useRef(null); + const elRef = useRef(null); + + useLayoutEffect(() => { + defineQrlApprovalHint(); + // createElement works with whichever copy of the element class holds the + // tag; a constructor call only with this one. + const el = document.createElement(QRL_APPROVAL_HINT_TAG); + if (!(el instanceof QrlApprovalHint)) return undefined; + hostRef.current?.append(el); + elRef.current = el; + return () => { + el.remove(); + elRef.current = null; + }; + }, []); + + useEffect(() => { + if (elRef.current) elRef.current.wallet = wallet ?? UNKNOWN_WALLET; + }, [wallet]); + + useEffect(() => { + if (elRef.current) elRef.current.pending = pending; + }, [pending]); + + // The host takes its spacing only while the hint shows, so a hidden hint + // leaves no gap. + return ( +
+ ); +} diff --git a/frontend/src/stores/poolStore.ts b/frontend/src/stores/poolStore.ts index 6d1aefe..3ddc4a3 100644 --- a/frontend/src/stores/poolStore.ts +++ b/frontend/src/stores/poolStore.ts @@ -7,7 +7,13 @@ import { } from "@qrlwallet/connect"; // The dependency-free subpath: this module is loaded in plain Node by the // store tests, and the package index pulls in the DOM pairing modal. -import { groupMyQrlWallet } from "@qrlwallet/connect-ui/wallets"; +import { + approvalWalletFor, + groupMyQrlWallet, + type ApprovalWallet, +} from "@qrlwallet/connect-ui/wallets"; +// Also free of DOM code: counts the wallet requests that wait for the user. +import { createApprovalRequests } from "@qrlwallet/connect-ui/flow"; import { NativeQrlPoolABI } from "@/abi/NativeQrlPool"; import { ACTIVE_NETWORK, type NetworkConfig } from "@/config/networks"; import { getQrlWeb3, type Web3Instance } from "@/utils/web3/web3Lazy"; @@ -94,6 +100,15 @@ export interface OtherWalletRow { /** A row shown in the picker. */ export type DiscoveredWallet = MyQrlWalletRow | OtherWalletRow; +/** + * The relay always is MyQRLWallet on a paired device. One object, so the + * hint is not handed a new wallet on every relay event. + */ +const RELAY_APPROVAL_WALLET: ApprovalWallet = approvalWalletFor({ + ...QRL_CONNECT_PROVIDER_INFO, + uuid: "", +}); + /** Which transport the active provider uses (drives the tx param shape). */ type ProviderKind = "extension" | "relay"; @@ -223,6 +238,17 @@ export class PoolStore { pairingStatus = ""; /** Display name of the connected wallet (e.g. "MyQRLWallet"). */ activeWalletName: string | null = null; + /** + * The wallet a request is waiting for, for the hint that names it: the + * connected wallet, or the one a connection request just went to. + */ + approvalWallet: ApprovalWallet | null = null; + /** + * The wallet requests that wait for the user. Open from the moment a + * request goes to the wallet until the wallet answers; a prompt can sit + * unseen behind the wallet's toolbar icon or on a paired device. + */ + readonly approvals = createApprovalRequests(); account: AccountState | null = null; withdrawals: WithdrawalRequestView[] = []; pendingDeposits: PendingDepositView[] = []; @@ -276,6 +302,7 @@ export class PoolStore { wiredExtensionProviders: false, transactionGeneration: false, onEip6963Announce: false, + approvals: false, } as const; makeAutoObservable(this, overrides); } @@ -390,6 +417,7 @@ export class PoolStore { this.provider = qrl; runInAction(() => { this.activeWalletName = QRL_CONNECT_PROVIDER_INFO.name; + this.approvalWallet = RELAY_APPROVAL_WALLET; this.pairingStatus = qrl.getStatus(); }); } @@ -474,8 +502,12 @@ export class PoolStore { this.provider = qrl; this.providerKind = "relay"; this.relayEstablished = false; + // A new relay connection: a request still open in the wallet before + // belongs to that wallet, and this page no longer waits for it. + this.approvals.reset(); runInAction(() => { this.activeWalletName = QRL_CONNECT_PROVIDER_INFO.name; + this.approvalWallet = RELAY_APPROVAL_WALLET; this.account = null; this.withdrawals = []; this.activity = []; @@ -556,9 +588,16 @@ export class PoolStore { if (!this.connectionAttemptGuard.isCurrent(attemptGeneration)) { throw new Error("Wallet connection attempt changed"); } - return detail.provider.request({ - method: "qrl_requestAccounts", + // A new extension connection starts its own count. + this.approvals.reset(); + runInAction(() => { + this.approvalWallet = approvalWalletFor(detail.info); }); + return this.approvals.track(() => + detail.provider.request({ + method: "qrl_requestAccounts", + }), + ); }, (accounts) => { if (!this.connectionAttemptGuard.isCurrent(attemptGeneration)) { @@ -616,8 +655,12 @@ export class PoolStore { this.provider = qrl; this.providerKind = "relay"; this.relayEstablished = false; + // A new relay connection: a request still open in the wallet before + // belongs to that wallet, and this page no longer waits for it. + this.approvals.reset(); runInAction(() => { this.activeWalletName = QRL_CONNECT_PROVIDER_INFO.name; + this.approvalWallet = RELAY_APPROVAL_WALLET; this.account = null; this.withdrawals = []; this.activity = []; @@ -715,6 +758,13 @@ export class PoolStore { const isNewAddress = !this.account || this.account.address !== address; runInAction(() => { this.activeWalletName = name; + // An extension keeps the announcement it was named from when its + // connection began. + if (kind === "relay") { + this.approvalWallet = RELAY_APPROVAL_WALLET; + } else if (this.approvalWallet?.name !== name) { + this.approvalWallet = { name, icon: null, kind: "extension" }; + } this.walletPickerOpen = false; this.pairingUri = null; this.connectError = null; @@ -785,7 +835,10 @@ export class PoolStore { this.providerKind = null; this.relayUserDisconnected = false; this.relayEstablished = false; + // A request lost with the connection never settles: forget it. + this.approvals.reset(); runInAction(() => { + this.approvalWallet = null; this.account = null; this.withdrawals = []; this.activity = []; @@ -982,7 +1035,9 @@ export class PoolStore { throw new Error("Wallet returned an invalid QRL account cache"); const accounts = cached.length ? cached - : await qrl.request({ method: "qrl_requestAccounts" }); + : await this.approvals.track(() => + qrl.request({ method: "qrl_requestAccounts" }), + ); const address = requireQrlAccount(accounts); if ( this.providerKind !== "relay" || @@ -1497,10 +1552,12 @@ export class PoolStore { "Switch your wallet to the chain configured for this native pool.", ); - const hash = await provider.request({ - method: "qrl_sendTransaction", - params: [txParams], - }); + const hash = await this.approvals.track(() => + provider.request({ + method: "qrl_sendTransaction", + params: [txParams], + }), + ); if (!isCurrent()) return false; txHash = readTransactionHash(hash); runInAction(() => {