"use client"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useEffect, useRef, useState } from "react"; import { api } from "@/lib/api"; import { useSession } from "@/lib/session"; import { useTheme, type ThemePref } from "@/lib/theme"; const APPEARANCE: { value: ThemePref; label: string }[] = [ { value: "light", label: "Light" }, { value: "dark", label: "Dark" }, { value: "system", label: "System" }, ]; /* * Everything here is about YOU rather than about the account: your settings, * your password, how you want the app to look, and leaving. None of it is a * destination worth a slot in the primary nav, which is why Settings moved off * the bar and into this menu. */ export function AccountMenu({ staff = false }: { staff?: boolean }) { const { session } = useSession(); const [open, setOpen] = useState(false); const [pref, setPref] = useTheme(); const wrap = useRef(null); const router = useRouter(); const qc = useQueryClient(); useEffect(() => { if (!open) return; const onDown = (e: MouseEvent) => { if (wrap.current && !wrap.current.contains(e.target as Node)) setOpen(false); }; const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") setOpen(false); }; document.addEventListener("mousedown", onDown); document.addEventListener("keydown", onKey); return () => { document.removeEventListener("mousedown", onDown); document.removeEventListener("keydown", onKey); }; }, [open]); const signOut = useMutation({ mutationFn: api.logout, // Clear the cache before leaving: a cached account response outliving // the session would show the next person who signs in on this browser // the previous account's name for a beat. onSettled: () => { qc.clear(); router.replace("/login"); }, }); const email = session?.email ?? ""; const initials = email .split("@")[0] .split(/[.\-_]/) .slice(0, 2) .map((p) => p[0]?.toUpperCase() ?? "") .join("") || "?"; return (
{open && (
{email} {staff ? "Vantage staff" : (session?.account_role ?? "member")}
{!staff && ( setOpen(false)} className="px-3 py-2 text-[0.86rem] text-ink hover:bg-accent-wash" > Settings )}
Appearance
{APPEARANCE.map((a) => ( ))}
)}
); }