NGU-Web/src/components/admin/SectionNav.tsx
Zaldimmar e4a9c96f7e Finish the audit leftovers
- One mediaUrl in media.ts instead of 12 identical aliases and a
  kind map; orgPath builds on hrefs.ts's orgHref
- One Logo component (hides itself or shows a fallback when the file
  is missing) for event cards, org lists and chapter marks
- useHistory is a thin wrapper over useRecord
- adminSchema.ts split into adminSchema/, one file per entity plus
  shared.ts; the registry is unchanged (checked by loading both)
- JS smooth scrolls respect reduced motion (lib/motion.ts)
- Content-Security-Policy in Report-Only, with /api/csp-report logging
  what it would block

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 22:49:07 -05:00

143 lines
5.3 KiB
TypeScript

/* ═══════════════════════════════════════════════════════════════
EDITOR SECTION NAV
The record editor's table of contents. On a wide screen it's a
sticky column left of the form; below lg it's a slim sticky bar
with a "Jump to" menu. Both list the same sections, which
EntityEdit derives from the manifest (field groups, then child
collections with their row counts), so every editor gets one
without declaring anything.
The lit entry is the last section whose top has scrolled past
the line ACTIVE_LINE px below the viewport's top: the one you're
reading, not merely the one peeking in at the bottom. A section
with a save error carries a red dot, so a rejected save points
at where to look.
═══════════════════════════════════════════════════════════════ */
import { useEffect, useState } from "react";
import { scrollBehavior } from "../../lib/motion.ts";
export type EditorSection = {
id: string;
label: string;
count?: number;
hasError?: boolean;
};
const ACTIVE_LINE = 140;
function useActiveSection(sections: EditorSection[]) {
const [active, setActive] = useState<string | null>(sections[0]?.id ?? null);
const ids = sections.map((section) => section.id).join("|");
useEffect(() => {
let frame = 0;
const measure = () => {
frame = 0;
let current: string | null = null;
for (const id of ids.split("|")) {
const el = document.getElementById(id);
if (el && el.getBoundingClientRect().top <= ACTIVE_LINE) current = id;
}
// At the very bottom the last short section may never reach
// the line; it's the one being read.
if (window.innerHeight + window.scrollY >= document.documentElement.scrollHeight - 4) {
current = ids.split("|").pop() ?? current;
}
setActive(current ?? ids.split("|")[0] ?? null);
};
const onScroll = () => {
if (!frame) frame = requestAnimationFrame(measure);
};
measure();
window.addEventListener("scroll", onScroll, { passive: true });
window.addEventListener("resize", onScroll);
return () => {
window.removeEventListener("scroll", onScroll);
window.removeEventListener("resize", onScroll);
if (frame) cancelAnimationFrame(frame);
};
}, [ids]);
return active;
}
function jump(id: string) {
document.getElementById(id)?.scrollIntoView({ behavior: scrollBehavior(), block: "start" });
}
function Label({ section }: { section: EditorSection }) {
return (
<>
<span className="truncate">{section.label}</span>
{section.count != null && (
<span className="text-xs text-muted/70">({section.count})</span>
)}
{section.hasError && (
<span aria-label="needs attention" className="h-1.5 w-1.5 shrink-0 rounded-full bg-danger" />
)}
</>
);
}
/* The desktop column. Place it in the grid's first track, in a cell
that is itself sticky (see EntityEdit). */
export function SectionNav({ sections }: { sections: EditorSection[] }) {
const active = useActiveSection(sections);
return (
<nav aria-label="Sections" className="hidden max-h-[calc(100vh-8rem)] overflow-y-auto lg:block">
<p className="mb-2 px-3 text-xs font-semibold uppercase tracking-wider text-muted/70">
On this page
</p>
<ul className="space-y-0.5 border-l border-brand/15">
{sections.map((section) => {
const lit = section.id === active;
return (
<li key={section.id}>
<button
type="button"
onClick={() => jump(section.id)}
aria-current={lit ? "location" : undefined}
className={`-ml-px flex w-full items-center gap-1.5 border-l-2 px-3 py-1.5 text-left text-sm transition-colors ${
lit
? "border-brand font-semibold text-link"
: "border-transparent text-muted hover:border-brand/40 hover:text-link"
}`}
>
<Label section={section} />
</button>
</li>
);
})}
</ul>
</nav>
);
}
/* The phone and tablet bar. Sits above the form, sticks to the top. */
export function SectionJump({ sections }: { sections: EditorSection[] }) {
const active = useActiveSection(sections);
return (
<div className="sticky top-0 z-20 -mx-6 mt-4 mb-2 border-b border-brand/15 bg-tint-soft/95 px-6 py-2 backdrop-blur lg:hidden">
<label className="flex items-center gap-3 text-sm text-muted">
<span className="shrink-0">Jump to</span>
<select
value={active ?? ""}
onChange={(e) => jump(e.target.value)}
className="w-full rounded-lg border border-muted/25 bg-surface px-3 py-1.5 text-sm text-body outline-none focus:border-brand"
>
{sections.map((section) => (
<option key={section.id} value={section.id}>
{section.label}
{section.count != null ? ` (${section.count})` : ""}
{section.hasError ? ", needs attention" : ""}
</option>
))}
</select>
</label>
</div>
);
}