"use client"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { useEffect, useId, useRef, useState } from "react"; import { LogoutButton } from "./LogoutButton"; /** * The account menu under the user's name (top right): profile, invoices, * redeeming a code, logging out. Closes on a click outside, Escape, or * moving to another page. */ export function UserMenu({ name, invoices }: { name: string; invoices: boolean }) { const [open, setOpen] = useState(false); const box = useRef(null); const button = useRef(null); const menuId = useId(); const pathname = usePathname(); useEffect(() => setOpen(false), [pathname]); useEffect(() => { if (!open) return; const outside = (e: PointerEvent) => { if (box.current && !box.current.contains(e.target as Node)) setOpen(false); }; document.addEventListener("pointerdown", outside); return () => document.removeEventListener("pointerdown", outside); }, [open]); function keydown(e: React.KeyboardEvent) { if (e.key === "Escape" && open) { setOpen(false); button.current?.focus(); } } const items = [ { href: "/profil", label: "Profil" }, ...(invoices ? [{ href: "/szamlak", label: "Számláim" }] : []), { href: "/bevaltas", label: "Kód beváltása" }, ]; return (
{open && ( )}
); }