"use client"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useEffect, useRef, useState } from "react"; import { formatBirthDate } from "@/lib/astro"; import type { ChartListItem } from "@/lib/types"; /** * The chart page title as a menu of the user's charts. Switching keeps the * open tab (#matrix etc.), so two people's charts are easy to compare. */ export function ChartSwitcher({ title, currentId, own, persons, canAdd, }: { title: string; currentId: string; own: ChartListItem | null; persons: ChartListItem[]; canAdd: boolean; }) { const router = useRouter(); const [open, setOpen] = useState(false); const root = useRef(null); useEffect(() => { if (!open) return; const close = (e: MouseEvent | KeyboardEvent) => { if (e instanceof KeyboardEvent ? e.key === "Escape" : !root.current?.contains(e.target as Node)) setOpen(false); }; document.addEventListener("mousedown", close); document.addEventListener("keydown", close); return () => { document.removeEventListener("mousedown", close); document.removeEventListener("keydown", close); }; }, [open]); function go(e: React.MouseEvent, id: string) { e.preventDefault(); setOpen(false); router.push(`/kepletek/${id}${window.location.hash}`); } const items = [...(own ? [{ chart: own, name: "Én" }] : []), ...persons.map((p) => ({ chart: p, name: p.label }))]; return (

{open && (
{canAdd && + Új személy} Összes képlet
)}
); }