2026-11-20 17:15:37 +00:00
|
|
|
"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,
|
2026-11-24 07:01:40 +00:00
|
|
|
* redeeming a code, giving a gift, feedback (a dot: a new reply), logging out. Closes on a click outside, Escape, or
|
2026-11-20 17:15:37 +00:00
|
|
|
* moving to another page.
|
|
|
|
|
*/
|
2026-11-24 07:01:40 +00:00
|
|
|
export function UserMenu({ name, invoices, replies = 0 }: { name: string; invoices: boolean; replies?: number }) {
|
2026-11-20 17:15:37 +00:00
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
|
const box = useRef<HTMLDivElement>(null);
|
|
|
|
|
const button = useRef<HTMLButtonElement>(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" },
|
2026-11-23 20:28:11 +00:00
|
|
|
{ href: "/ajandek", label: "Ajándékozás" },
|
2026-11-24 07:01:40 +00:00
|
|
|
// Where the user is goes with the message (for a bug).
|
|
|
|
|
{ href: `/visszajelzes?honnan=${encodeURIComponent(pathname)}`, match: "/visszajelzes", label: "Visszajelzés", dot: replies > 0 },
|
2026-11-20 17:15:37 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="user-menu" ref={box} onKeyDown={keydown}>
|
|
|
|
|
<button
|
|
|
|
|
ref={button}
|
|
|
|
|
type="button"
|
|
|
|
|
className="who-name"
|
|
|
|
|
aria-expanded={open}
|
|
|
|
|
aria-controls={menuId}
|
|
|
|
|
aria-haspopup="menu"
|
|
|
|
|
onClick={() => setOpen((o) => !o)}
|
|
|
|
|
>
|
|
|
|
|
<svg viewBox="0 0 24 24" aria-hidden="true" className="who-icon">
|
|
|
|
|
<circle cx="12" cy="8" r="4" />
|
|
|
|
|
<path d="M4 20c1.5-4 4.5-6 8-6s6.5 2 8 6" />
|
|
|
|
|
</svg>
|
|
|
|
|
<span className="who-name-text">{name}</span>
|
2026-11-24 07:01:40 +00:00
|
|
|
{replies > 0 && <span className="menu-dot" aria-label="új válasz a visszajelzésedre" />}
|
2026-11-20 17:15:37 +00:00
|
|
|
<svg viewBox="0 0 24 24" aria-hidden="true" className="who-chevron">
|
|
|
|
|
<path d="M6 9l6 6 6-6" />
|
|
|
|
|
</svg>
|
|
|
|
|
</button>
|
|
|
|
|
{open && (
|
|
|
|
|
<div id={menuId} className="user-menu-list" role="menu" aria-label="Fiók">
|
|
|
|
|
{items.map((item) => (
|
|
|
|
|
<Link
|
|
|
|
|
key={item.href}
|
|
|
|
|
href={item.href}
|
|
|
|
|
role="menuitem"
|
|
|
|
|
className="user-menu-item"
|
2026-11-24 07:01:40 +00:00
|
|
|
aria-current={pathname.startsWith("match" in item && item.match ? item.match : item.href) ? "page" : undefined}
|
2026-11-20 17:15:37 +00:00
|
|
|
>
|
|
|
|
|
{item.label}
|
2026-11-24 07:01:40 +00:00
|
|
|
{"dot" in item && item.dot && (
|
|
|
|
|
<span className="menu-dot" aria-label="új válasz">
|
|
|
|
|
{" "}
|
|
|
|
|
</span>
|
|
|
|
|
)}
|
2026-11-20 17:15:37 +00:00
|
|
|
</Link>
|
|
|
|
|
))}
|
|
|
|
|
<LogoutButton className="user-menu-item user-menu-logout" role="menuitem" />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|