import type { WalletSummary } from './shared'; import { useSearchParams } from 'react-router'; import { WALLET_PARAM } from './shared'; import { useWallets } from './useWalletData'; // Which wallet is open lives in `?wallet=`, read independently by every panel — never passed between // them over a channel. See docs/navigation-audit.md. // // With no param, the sidecar's own `isActive` wallet wins, then the first in the list. That fallback is a // display default, not a selection: the URL stays bare so a shared /wallet link means "whatever is active // right now" rather than pinning whichever wallet the sender happened to have open. export type SelectedWallet = { walletId: number | null; wallet: WalletSummary | null; wallets: WalletSummary[]; /** True when the id came from the URL rather than the active-wallet fallback. */ isPinned: boolean; isLoading: boolean; }; export function useSelectedWallet(): SelectedWallet { const [params] = useSearchParams(); const { wallets, isLoading } = useWallets(); const raw = params.get(WALLET_PARAM); const pinnedId = raw && /^\d+$/.test(raw) ? Number(raw) : null; const pinned = pinnedId == null ? null : (wallets.find((w) => w.id === pinnedId) ?? null); const fallback = wallets.find((w) => w.isActive) ?? wallets[0] ?? null; const wallet = pinned ?? fallback; return { walletId: wallet?.id ?? null, wallet, wallets, isPinned: pinned != null, isLoading, }; }