Files
sorsfonalak/services/frontend/src/components/ChartSwitcher.tsx
T
fecoandClaude Opus 5.5 fef1605848
CI / test (push) Failing after 43s
CI / build (push) Skipped
CI / deploy (push) Skipped
Persons: other people's charts with package slots, list page, switcher
- charts.kind own/person; a new person uses a 'person' unit (not refunded
  on delete); person packages and an unlimited entitlement; admins unlimited
- a person's birth data can be corrected within an hour (admins any time)
- /kepletek list with tiles, /kepletek/uj, correction page, mock person
  packages, chart page title switches between charts keeping the tab

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-17 11:03:55 +00:00

94 lines
2.7 KiB
TypeScript

"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<HTMLDivElement>(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 (
<div className="switcher" ref={root}>
<h1>
<button
type="button"
className="switcher-button"
aria-haspopup="true"
aria-expanded={open}
onClick={() => setOpen((o) => !o)}
>
{title}
<span className="switcher-caret" aria-hidden="true">
▾
</span>
<span className="sr-only">: másik képlet választása</span>
</button>
</h1>
{open && (
<div className="switcher-menu">
<ul>
{items.map(({ chart, name }) => (
<li key={chart.id}>
<a
href={`/kepletek/${chart.id}`}
aria-current={chart.id === currentId ? "page" : undefined}
onClick={(e) => go(e, chart.id)}
>
<strong>{name}</strong>
<span className="muted">{formatBirthDate(chart.birth.date)}</span>
</a>
</li>
))}
</ul>
<div className="switcher-foot">
{canAdd && <Link href="/kepletek/uj">+ Új személy</Link>}
<Link href="/kepletek">Összes képlet</Link>
</div>
</div>
)}
</div>
);
}