/* ═══════════════════════════════════════════════════════════════ 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"; export type EditorSection = { id: string; label: string; count?: number; hasError?: boolean; }; const ACTIVE_LINE = 140; function useActiveSection(sections: EditorSection[]) { const [active, setActive] = useState(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: "smooth", block: "start" }); } function Label({ section }: { section: EditorSection }) { return ( <> {section.label} {section.count != null && ( ({section.count}) )} {section.hasError && ( )} ); } /* The desktop column. Place it in the grid's first track. */ export function SectionNav({ sections }: { sections: EditorSection[] }) { const active = useActiveSection(sections); return ( ); } /* The phone and tablet bar. Sits above the form, sticks to the top. */ export function SectionJump({ sections }: { sections: EditorSection[] }) { const active = useActiveSection(sections); return (
); }