"use client" import { useEffect, useRef, type RefObject } from "react" import Link from "next/link" import { usePathname } from "next/navigation" import { Camera, ChevronDown, ChevronUp, LogOut, Plus, X } from "lucide-react" import { AccountAvatar } from "@/components/gmail/account-avatar" import { Button } from "@/components/ui/button" import type { ApiMailAccount } from "@/lib/api/types" import { useMailAccounts } from "@/lib/api/hooks/use-mail-queries" import { useChromeIdentity } from "@/lib/hooks/use-chrome-identity" import { buildOidcLoginUrl } from "@/lib/auth/login-url" import { ACCOUNT_SETTINGS_BASE_PATH } from "@/lib/compte-settings/settings-nav" import { useAccountStore, useSignOutAll, } from "@/lib/stores/account-store" interface AccountSwitcherDropdownProps { open: boolean onOpenChange: (open: boolean) => void containerRef: RefObject } function AccountRow({ account, onSelect, }: { account: ApiMailAccount onSelect: () => void }) { return ( ) } export function AccountSwitcherDropdown({ open, onOpenChange, containerRef, }: AccountSwitcherDropdownProps) { const panelRef = useRef(null) const pathname = usePathname() const identity = useChromeIdentity() const otherAccountsExpanded = useAccountStore((s) => s.otherAccountsExpanded) const setActiveAccountId = useAccountStore((s) => s.setActiveAccountId) const toggleOtherAccountsExpanded = useAccountStore( (s) => s.toggleOtherAccountsExpanded, ) const signOutAll = useSignOutAll() const { data: accounts } = useMailAccounts() const mailAccounts = accounts ?? [] const hasMultipleMailAccounts = mailAccounts.length > 1 const firstName = identity?.firstName ?? "" const addAccountHref = buildOidcLoginUrl({ returnTo: pathname || "/mail/inbox", intent: "add_account", }) useEffect(() => { if (!open) return function handleClickOutside(event: MouseEvent) { if ( containerRef.current && !containerRef.current.contains(event.target as Node) ) { onOpenChange(false) } } function handleEscape(event: KeyboardEvent) { if (event.key === "Escape") onOpenChange(false) } document.addEventListener("mousedown", handleClickOutside) document.addEventListener("keydown", handleEscape) return () => { document.removeEventListener("mousedown", handleClickOutside) document.removeEventListener("keydown", handleEscape) } }, [open, onOpenChange, containerRef]) if (!open || !identity) return null const handleSelectAccount = (id: string) => { setActiveAccountId(id) onOpenChange(false) } const handleSignOut = () => { void signOutAll() onOpenChange(false) } return (

{identity.email}

Bonjour {firstName} !

{hasMultipleMailAccounts ? (
{otherAccountsExpanded && (
{mailAccounts.map((account) => ( handleSelectAccount(account.id)} /> ))}
)}
) : null}
·
) }