import { useState } from "react"; import { useUserStore, SavedAccount } from "../../stores/user"; import { useUIStore } from "../../stores/ui"; import { LoginModal } from "../shared/LoginModal"; import { shortenPubkey } from "../../lib/utils"; function Avatar({ account, size = 6 }: { account: SavedAccount; size?: number }) { const initial = (account.name || account.npub || "?").charAt(0).toUpperCase(); const cls = `w-${size} h-${size} rounded-sm object-cover shrink-0`; if (account.picture) { return ( { (e.target as HTMLImageElement).style.display = "none"; }} /> ); } return (
{initial}
); } export function AccountSwitcher() { const { accounts, pubkey, switchAccount, removeAccount, logout } = useUserStore(); const { openProfile } = useUIStore(); const [open, setOpen] = useState(false); const [showAddLogin, setShowAddLogin] = useState(false); const current = accounts.find((a) => a.pubkey === pubkey) ?? null; const others = accounts.filter((a) => a.pubkey !== pubkey); const displayName = (a: SavedAccount) => a.name || shortenPubkey(a.npub); const handleSwitch = async (targetPubkey: string) => { setOpen(false); await switchAccount(targetPubkey); }; const handleRemove = (e: React.MouseEvent, targetPubkey: string) => { e.stopPropagation(); removeAccount(targetPubkey); }; const handleAddAccount = () => { setOpen(false); setShowAddLogin(true); }; // Not logged in if (!pubkey || !current) { return ( <>
{accounts.length > 0 && (
{accounts.map((a) => ( ))}
)}
{showAddLogin && setShowAddLogin(false)} />} ); } return ( <>
{/* Expanded account list */} {open && (
{others.map((a) => (
handleSwitch(a.pubkey)} > {displayName(a)}
))}
)} {/* Current account row */}
openProfile(pubkey)} > {displayName(current)}
{open && (
)}
{showAddLogin && setShowAddLogin(false)} />} ); }