Add a section nav to the admin editor and fold repeater rows
Every record editor gets a sticky "On this page" column listing its field groups and collections (with row counts), lighting the one in view and marking any with a save error. Below lg it becomes a sticky "Jump to" menu. Editor pages widen to fit it through a small layout context; other admin pages keep their width. Each collection now sits in its own card, and repeater rows fold to their title line. Rows start folded, with Expand all and Collapse all; a new row, or one the server rejected, opens itself. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
eaff23839f
commit
8186aff62f
5 changed files with 402 additions and 67 deletions
141
src/components/admin/SectionNav.tsx
Normal file
141
src/components/admin/SectionNav.tsx
Normal file
|
|
@ -0,0 +1,141 @@
|
||||||
|
/* ═══════════════════════════════════════════════════════════════
|
||||||
|
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<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: "smooth", block: "start" });
|
||||||
|
}
|
||||||
|
|
||||||
|
function Label({ section }: { section: EditorSection }) {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<span className="truncate">{section.label}</span>
|
||||||
|
{section.count != null && (
|
||||||
|
<span className="text-xs text-[#4a6b72]/70">({section.count})</span>
|
||||||
|
)}
|
||||||
|
{section.hasError && (
|
||||||
|
<span aria-label="needs attention" className="h-1.5 w-1.5 shrink-0 rounded-full bg-[#b3261e]" />
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The desktop column. Place it in the grid's first track. */
|
||||||
|
export function SectionNav({ sections }: { sections: EditorSection[] }) {
|
||||||
|
const active = useActiveSection(sections);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<nav aria-label="Sections" className="sticky top-6 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-[#4a6b72]/70">
|
||||||
|
On this page
|
||||||
|
</p>
|
||||||
|
<ul className="space-y-0.5 border-l border-[#138ba0]/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-[#138ba0] font-semibold text-[#138ba0]"
|
||||||
|
: "border-transparent text-[#4a6b72] hover:border-[#138ba0]/40 hover:text-[#138ba0]"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<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 mb-2 border-b border-[#138ba0]/15 bg-[#f6fbfc]/95 px-6 py-2 backdrop-blur lg:hidden">
|
||||||
|
<label className="flex items-center gap-3 text-sm text-[#4a6b72]">
|
||||||
|
<span className="shrink-0">Jump to</span>
|
||||||
|
<select
|
||||||
|
value={active ?? ""}
|
||||||
|
onChange={(e) => jump(e.target.value)}
|
||||||
|
className="w-full rounded-lg border border-[#4a6b72]/25 bg-white px-3 py-1.5 text-sm text-[#26454c] outline-none focus:border-[#138ba0]"
|
||||||
|
>
|
||||||
|
{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>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -7,7 +7,15 @@
|
||||||
|
|
||||||
Ordering is array position (the server writes sort_order from
|
Ordering is array position (the server writes sort_order from
|
||||||
the index), so moving a row is a splice, not a number to
|
the index), so moving a row is a splice, not a number to
|
||||||
hand-edit. Rows can be dragged by the handle or moved with the
|
hand-edit.
|
||||||
|
|
||||||
|
Rows fold to their title line. They start folded, so a long
|
||||||
|
collection (the front page's slides and paths) reads as a list
|
||||||
|
rather than a wall of fields; a row just added starts open, and
|
||||||
|
one the server rejected opens itself so the error can't hide.
|
||||||
|
Which rows are open is a set of indices, remapped by every move,
|
||||||
|
insert and removal here, so it survives a save (which replaces
|
||||||
|
every row object but keeps their order). Rows can be dragged by the handle or moved with the
|
||||||
arrow buttons; the arrows are the keyboard path and stay whether
|
arrow buttons; the arrows are the keyboard path and stay whether
|
||||||
or not a pointer is in use.
|
or not a pointer is in use.
|
||||||
|
|
||||||
|
|
@ -246,9 +254,42 @@ export function FieldGrid({ children }) {
|
||||||
|
|
||||||
/* ── Repeater ────────────────────────────────────────────────── */
|
/* ── Repeater ────────────────────────────────────────────────── */
|
||||||
|
|
||||||
export function Repeater({ spec, rows, options, errors, errorPrefix, onChange }) {
|
/* Where each old index ends up after a change to the list, or -1
|
||||||
|
when that row is gone. Used to carry the open set across it. */
|
||||||
|
function remapOpen(open: Set<number>, where: (index: number) => number) {
|
||||||
|
const next = new Set<number>();
|
||||||
|
for (const index of open) {
|
||||||
|
const moved = where(index);
|
||||||
|
if (moved >= 0) next.add(moved);
|
||||||
|
}
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function Repeater({
|
||||||
|
spec,
|
||||||
|
rows,
|
||||||
|
options,
|
||||||
|
errors,
|
||||||
|
errorPrefix,
|
||||||
|
onChange,
|
||||||
|
className = "mt-8",
|
||||||
|
}: any) {
|
||||||
const list = rows ?? [];
|
const list = rows ?? [];
|
||||||
|
|
||||||
|
const [open, setOpen] = useState<Set<number>>(() => new Set());
|
||||||
|
const hasError = (index) =>
|
||||||
|
Object.keys(errors ?? {}).some(
|
||||||
|
(key) => errors[key] && key.startsWith(`${errorPrefix}${index}.`),
|
||||||
|
);
|
||||||
|
const isOpen = (index) => open.has(index) || hasError(index);
|
||||||
|
const toggle = (index) =>
|
||||||
|
setOpen((prev) => {
|
||||||
|
const next = new Set(prev);
|
||||||
|
if (next.has(index)) next.delete(index);
|
||||||
|
else next.add(index);
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
|
||||||
// Which row is in flight, and which one it's currently over.
|
// Which row is in flight, and which one it's currently over.
|
||||||
// Both are per-Repeater, which is what keeps a drag inside a
|
// Both are per-Repeater, which is what keeps a drag inside a
|
||||||
// nested collection from being accepted by the outer one.
|
// nested collection from being accepted by the outer one.
|
||||||
|
|
@ -264,6 +305,9 @@ export function Repeater({ spec, rows, options, errors, errorPrefix, onChange })
|
||||||
if (target < 0 || target >= list.length) return;
|
if (target < 0 || target >= list.length) return;
|
||||||
const next = [...list];
|
const next = [...list];
|
||||||
[next[index], next[target]] = [next[target], next[index]];
|
[next[index], next[target]] = [next[target], next[index]];
|
||||||
|
setOpen((prev) =>
|
||||||
|
remapOpen(prev, (i) => (i === index ? target : i === target ? index : i)),
|
||||||
|
);
|
||||||
onChange(next);
|
onChange(next);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -272,22 +316,63 @@ export function Repeater({ spec, rows, options, errors, errorPrefix, onChange })
|
||||||
const next = [...list];
|
const next = [...list];
|
||||||
const [moved] = next.splice(from, 1);
|
const [moved] = next.splice(from, 1);
|
||||||
next.splice(to, 0, moved);
|
next.splice(to, 0, moved);
|
||||||
|
setOpen((prev) =>
|
||||||
|
remapOpen(prev, (i) => {
|
||||||
|
if (i === from) return to;
|
||||||
|
if (from < to && i > from && i <= to) return i - 1;
|
||||||
|
if (from > to && i >= to && i < from) return i + 1;
|
||||||
|
return i;
|
||||||
|
}),
|
||||||
|
);
|
||||||
onChange(next);
|
onChange(next);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const add = () => {
|
||||||
|
setOpen((prev) => new Set(prev).add(list.length));
|
||||||
|
onChange([...list, { ...spec.blank }]);
|
||||||
|
};
|
||||||
|
|
||||||
|
const removeAt = (index) => {
|
||||||
|
setOpen((prev) => remapOpen(prev, (i) => (i === index ? -1 : i > index ? i - 1 : i)));
|
||||||
|
onChange(list.filter((_, i) => i !== index));
|
||||||
|
};
|
||||||
|
|
||||||
const endDrag = () => {
|
const endDrag = () => {
|
||||||
setDragIndex(null);
|
setDragIndex(null);
|
||||||
setOverIndex(null);
|
setOverIndex(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="mt-8">
|
<section className={className}>
|
||||||
<div className="flex items-center justify-between">
|
<div className="flex flex-wrap items-center gap-3">
|
||||||
<h3 className="text-base font-semibold text-[#26454c]">{spec.label}</h3>
|
<h3 className="text-base font-semibold text-[#26454c]">
|
||||||
|
{spec.label}
|
||||||
|
{list.length > 0 && (
|
||||||
|
<span className="ml-2 text-sm font-normal text-[#4a6b72]/80">{list.length}</span>
|
||||||
|
)}
|
||||||
|
</h3>
|
||||||
|
{list.length > 1 && (
|
||||||
|
<div className="flex gap-3 text-xs text-[#4a6b72]">
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => onChange([...list, { ...spec.blank }])}
|
onClick={() => setOpen(new Set(list.map((_, i) => i)))}
|
||||||
className="rounded-full border border-[#138ba0] px-3 py-1 text-sm font-medium text-[#138ba0] transition-colors hover:bg-[#eef9fb]"
|
className="hover:text-[#138ba0] hover:underline"
|
||||||
|
>
|
||||||
|
Expand all
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setOpen(new Set())}
|
||||||
|
className="hover:text-[#138ba0] hover:underline"
|
||||||
|
>
|
||||||
|
Collapse all
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={add}
|
||||||
|
className="ml-auto rounded-full border border-[#138ba0] px-3 py-1 text-sm font-medium text-[#138ba0] transition-colors hover:bg-[#eef9fb]"
|
||||||
>
|
>
|
||||||
{spec.addLabel ?? "Add"}
|
{spec.addLabel ?? "Add"}
|
||||||
</button>
|
</button>
|
||||||
|
|
@ -301,6 +386,7 @@ export function Repeater({ spec, rows, options, errors, errorPrefix, onChange })
|
||||||
{list.map((row, index) => {
|
{list.map((row, index) => {
|
||||||
const dragging = dragIndex === index;
|
const dragging = dragIndex === index;
|
||||||
const over = overIndex === index && dragIndex !== null && !dragging;
|
const over = overIndex === index && dragIndex !== null && !dragging;
|
||||||
|
const expanded = isOpen(index);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
|
|
@ -325,14 +411,14 @@ export function Repeater({ spec, rows, options, errors, errorPrefix, onChange })
|
||||||
endDrag();
|
endDrag();
|
||||||
}}
|
}}
|
||||||
className={
|
className={
|
||||||
"rounded-xl border bg-[#f6fbfc] p-4 transition-colors " +
|
`rounded-xl border bg-[#f6fbfc] ${expanded ? "p-4" : "px-4 py-2.5"} transition-colors ` +
|
||||||
(over
|
(over
|
||||||
? "border-[#138ba0] ring-2 ring-[#138ba0]/25 "
|
? "border-[#138ba0] ring-2 ring-[#138ba0]/25 "
|
||||||
: "border-[#4a6b72]/20 ") +
|
: "border-[#4a6b72]/20 ") +
|
||||||
(dragging ? "opacity-50" : "")
|
(dragging ? "opacity-50" : "")
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<div className="mb-3 flex items-center gap-2">
|
<div className={`flex items-center gap-2 ${expanded ? "mb-3" : ""}`}>
|
||||||
<span
|
<span
|
||||||
draggable
|
draggable
|
||||||
onDragStart={(e) => {
|
onDragStart={(e) => {
|
||||||
|
|
@ -353,11 +439,29 @@ export function Repeater({ spec, rows, options, errors, errorPrefix, onChange })
|
||||||
⠿
|
⠿
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
<span className="text-sm font-medium text-[#26454c]">
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => toggle(index)}
|
||||||
|
aria-expanded={expanded}
|
||||||
|
className="flex min-w-0 flex-1 items-center gap-2 text-left text-sm font-medium text-[#26454c] hover:text-[#138ba0]"
|
||||||
|
>
|
||||||
|
<span
|
||||||
|
aria-hidden="true"
|
||||||
|
className={`inline-block text-xs text-[#4a6b72] transition-transform ${
|
||||||
|
expanded ? "rotate-90" : ""
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
▶
|
||||||
|
</span>
|
||||||
|
<span className="truncate">
|
||||||
{spec.title ? spec.title(row, options) : `#${index + 1}`}
|
{spec.title ? spec.title(row, options) : `#${index + 1}`}
|
||||||
</span>
|
</span>
|
||||||
|
{!expanded && hasError(index) && (
|
||||||
|
<span className="text-xs text-[#b3261e]">needs attention</span>
|
||||||
|
)}
|
||||||
|
</button>
|
||||||
|
|
||||||
<div className="ml-auto flex gap-1">
|
<div className="ml-auto flex shrink-0 gap-1">
|
||||||
<IconButton
|
<IconButton
|
||||||
label="Move up"
|
label="Move up"
|
||||||
onClick={() => move(index, -1)}
|
onClick={() => move(index, -1)}
|
||||||
|
|
@ -375,13 +479,15 @@ export function Repeater({ spec, rows, options, errors, errorPrefix, onChange })
|
||||||
<IconButton
|
<IconButton
|
||||||
label="Remove"
|
label="Remove"
|
||||||
danger
|
danger
|
||||||
onClick={() => onChange(list.filter((_, i) => i !== index))}
|
onClick={() => removeAt(index)}
|
||||||
>
|
>
|
||||||
✕
|
✕
|
||||||
</IconButton>
|
</IconButton>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{expanded && (
|
||||||
|
<>
|
||||||
<FieldGrid>
|
<FieldGrid>
|
||||||
{spec.fields.map((field) => (
|
{spec.fields.map((field) => (
|
||||||
<Field
|
<Field
|
||||||
|
|
@ -397,17 +503,20 @@ export function Repeater({ spec, rows, options, errors, errorPrefix, onChange })
|
||||||
</FieldGrid>
|
</FieldGrid>
|
||||||
|
|
||||||
{(spec.children ?? []).map((nested) => (
|
{(spec.children ?? []).map((nested) => (
|
||||||
<div key={nested.key} className="mt-4 border-t border-[#4a6b72]/15 pt-2">
|
<div key={nested.key} className="mt-4 border-t border-[#4a6b72]/15 pt-4">
|
||||||
<Repeater
|
<Repeater
|
||||||
spec={nested}
|
spec={nested}
|
||||||
rows={row[nested.key]}
|
rows={row[nested.key]}
|
||||||
options={options}
|
options={options}
|
||||||
errors={errors}
|
errors={errors}
|
||||||
errorPrefix={`${errorPrefix}${index}.${nested.key}.`}
|
errorPrefix={`${errorPrefix}${index}.${nested.key}.`}
|
||||||
|
className=""
|
||||||
onChange={(value) => update(index, { ...row, [nested.key]: value })}
|
onChange={(value) => update(index, { ...row, [nested.key]: value })}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
|
|
||||||
26
src/lib/adminWidth.tsx
Normal file
26
src/lib/adminWidth.tsx
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
/* ═══════════════════════════════════════════════════════════════
|
||||||
|
ADMIN PAGE WIDTH
|
||||||
|
|
||||||
|
The admin column is max-w-5xl. The record editor wants more: its
|
||||||
|
section nav sits to the left of a form that keeps that width.
|
||||||
|
Like the title (adminTitle.tsx), the page asks and the layout
|
||||||
|
decides, so AdminLayout stays the one place that sets <main>'s
|
||||||
|
width.
|
||||||
|
|
||||||
|
Clears on unmount, so leaving the editor puts the list pages
|
||||||
|
back to their usual width.
|
||||||
|
═══════════════════════════════════════════════════════════════ */
|
||||||
|
|
||||||
|
import { createContext, useContext, useEffect } from "react";
|
||||||
|
|
||||||
|
export const AdminWidthContext = createContext<((wide: boolean) => void) | null>(null);
|
||||||
|
|
||||||
|
export function useWideAdminPage() {
|
||||||
|
const setWide = useContext(AdminWidthContext);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!setWide) return undefined;
|
||||||
|
setWide(true);
|
||||||
|
return () => setWide(false);
|
||||||
|
}, [setWide]);
|
||||||
|
}
|
||||||
|
|
@ -20,6 +20,7 @@ import { useCallback, useEffect, useMemo, useState } from "react";
|
||||||
import { Link, NavLink, Outlet, useLocation, useNavigate } from "react-router-dom";
|
import { Link, NavLink, Outlet, useLocation, useNavigate } from "react-router-dom";
|
||||||
import { useAuth } from "../../lib/auth.tsx";
|
import { useAuth } from "../../lib/auth.tsx";
|
||||||
import { AdminTitleContext } from "../../lib/adminTitle.tsx";
|
import { AdminTitleContext } from "../../lib/adminTitle.tsx";
|
||||||
|
import { AdminWidthContext } from "../../lib/adminWidth.tsx";
|
||||||
import { SITE_VERSION } from "../../lib/version.ts";
|
import { SITE_VERSION } from "../../lib/version.ts";
|
||||||
import { ROLE_LABELS, isSuper } from "../../lib/roles.ts";
|
import { ROLE_LABELS, isSuper } from "../../lib/roles.ts";
|
||||||
import nguLogo from "../../assets/NGU_Logo.svg";
|
import nguLogo from "../../assets/NGU_Logo.svg";
|
||||||
|
|
@ -60,6 +61,10 @@ export default function AdminLayout() {
|
||||||
const stableSet = useCallback((value) => setDetail(value), []);
|
const stableSet = useCallback((value) => setDetail(value), []);
|
||||||
const titleContext = useMemo(() => ({ setDetail: stableSet }), [stableSet]);
|
const titleContext = useMemo(() => ({ setDetail: stableSet }), [stableSet]);
|
||||||
|
|
||||||
|
// Whether the page below asked for extra room (the record editor,
|
||||||
|
// for its section nav). setWide is a state setter, so it's stable.
|
||||||
|
const [wide, setWide] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Sections only exist in the CMS. Home and Panel already say
|
// Sections only exist in the CMS. Home and Panel already say
|
||||||
// what they are in the area title; "Panel | NGU Admin Panel"
|
// what they are in the area title; "Panel | NGU Admin Panel"
|
||||||
|
|
@ -159,9 +164,11 @@ export default function AdminLayout() {
|
||||||
{/* flex-1 rather than a fixed height: the footer sits at the
|
{/* flex-1 rather than a fixed height: the footer sits at the
|
||||||
bottom of a short page and below the content of a long one,
|
bottom of a short page and below the content of a long one,
|
||||||
without ever floating over it. */}
|
without ever floating over it. */}
|
||||||
<main className="mx-auto w-full max-w-5xl flex-1 px-6 py-10">
|
<main className={`mx-auto w-full flex-1 px-6 py-10 ${wide ? "max-w-[82rem]" : "max-w-5xl"}`}>
|
||||||
<AdminTitleContext.Provider value={titleContext}>
|
<AdminTitleContext.Provider value={titleContext}>
|
||||||
|
<AdminWidthContext.Provider value={setWide}>
|
||||||
<Outlet />
|
<Outlet />
|
||||||
|
</AdminWidthContext.Provider>
|
||||||
</AdminTitleContext.Provider>
|
</AdminTitleContext.Provider>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,12 @@
|
||||||
None of this is protection. The server refuses the request; this
|
None of this is protection. The server refuses the request; this
|
||||||
only decides whether to draw a button that would be refused.
|
only decides whether to draw a button that would be refused.
|
||||||
|
|
||||||
|
The page is laid out as sections: the URL ID, each field group,
|
||||||
|
then each child collection in a card of its own. SectionNav lists
|
||||||
|
them (a sticky column on wide screens, a "Jump to" bar below lg),
|
||||||
|
derived here from the manifest, so no descriptor declares a nav.
|
||||||
|
The editor asks the layout for extra width to fit it.
|
||||||
|
|
||||||
slugFrom may name one field or several. Most ids are unique
|
slugFrom may name one field or several. Most ids are unique
|
||||||
because the name is: two organizations aren't both called
|
because the name is: two organizations aren't both called
|
||||||
Northwest. Team ids are the exception: teams.id is a global
|
Northwest. Team ids are the exception: teams.id is a global
|
||||||
|
|
@ -46,9 +52,21 @@ import { useNavigate, useParams } from "react-router-dom";
|
||||||
import { get, post, patch, del, ApiError } from "../../lib/api.ts";
|
import { get, post, patch, del, ApiError } from "../../lib/api.ts";
|
||||||
import { isUnauthorized, useAuth } from "../../lib/auth.tsx";
|
import { isUnauthorized, useAuth } from "../../lib/auth.tsx";
|
||||||
import { useAdminDetail } from "../../lib/adminTitle.tsx";
|
import { useAdminDetail } from "../../lib/adminTitle.tsx";
|
||||||
|
import { useWideAdminPage } from "../../lib/adminWidth.tsx";
|
||||||
import { ADMIN_ENTITIES, slugify } from "../../lib/adminSchema.ts";
|
import { ADMIN_ENTITIES, slugify } from "../../lib/adminSchema.ts";
|
||||||
import { atLeast } from "../../lib/roles.ts";
|
import { atLeast } from "../../lib/roles.ts";
|
||||||
import { Field, FieldGrid, Repeater, getPath, setPath } from "../../components/admin/fields.tsx";
|
import { Field, FieldGrid, Repeater, getPath, setPath } from "../../components/admin/fields.tsx";
|
||||||
|
import {
|
||||||
|
SectionJump,
|
||||||
|
SectionNav,
|
||||||
|
type EditorSection,
|
||||||
|
} from "../../components/admin/SectionNav.tsx";
|
||||||
|
|
||||||
|
/* One card per section. scroll-mt clears the sticky "Jump to" bar
|
||||||
|
below lg, and leaves a little air above the card on desktop. */
|
||||||
|
const CARD = "mt-6 scroll-mt-16 rounded-2xl border border-[#138ba0]/20 bg-white p-5 lg:scroll-mt-6";
|
||||||
|
|
||||||
|
const sectionId = (name) => `section-${slugify(name)}`;
|
||||||
|
|
||||||
/* A foreign key refusing to budge is the most common way a save or
|
/* A foreign key refusing to budge is the most common way a save or
|
||||||
delete fails here, and SQLite's own wording explains nothing to
|
delete fails here, and SQLite's own wording explains nothing to
|
||||||
|
|
@ -66,6 +84,8 @@ export default function EntityEdit() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { user } = useAuth();
|
const { user } = useAuth();
|
||||||
|
|
||||||
|
useWideAdminPage();
|
||||||
|
|
||||||
const isNew = id === "new";
|
const isNew = id === "new";
|
||||||
const canWrite = atLeast(user, "editor");
|
const canWrite = atLeast(user, "editor");
|
||||||
const canDelete = atLeast(user, "admin");
|
const canDelete = atLeast(user, "admin");
|
||||||
|
|
@ -302,6 +322,32 @@ export default function EntityEdit() {
|
||||||
|
|
||||||
const visible = (when) => !when || getPath(form, when.path) === when.value;
|
const visible = (when) => !when || getPath(form, when.path) === when.value;
|
||||||
|
|
||||||
|
const groups = manifest.groups.filter((group) => visible(group.when));
|
||||||
|
const shownChildren = children.filter((child) => visible(child.when));
|
||||||
|
const childHasError = (key) =>
|
||||||
|
Object.keys(errors).some((path) => errors[path] && path.startsWith(`${key}.`));
|
||||||
|
|
||||||
|
// A singleton's only header box is "Last saved", and a new
|
||||||
|
// auto-id record has no box at all: neither is worth a nav entry.
|
||||||
|
const idSection = !singleton && (!autoId || !isNew);
|
||||||
|
|
||||||
|
const sections: EditorSection[] = [
|
||||||
|
...(idSection
|
||||||
|
? [{ id: "section-url-id", label: manifest.idLabel, hasError: Boolean(errors.id) }]
|
||||||
|
: []),
|
||||||
|
...groups.map((group) => ({
|
||||||
|
id: sectionId(group.legend),
|
||||||
|
label: group.legend,
|
||||||
|
hasError: group.fields.some((field) => errors[field.path]),
|
||||||
|
})),
|
||||||
|
...shownChildren.map((child) => ({
|
||||||
|
id: sectionId(`${child.key}-list`),
|
||||||
|
label: child.label,
|
||||||
|
count: (form[child.key] ?? []).length,
|
||||||
|
hasError: childHasError(child.key),
|
||||||
|
})),
|
||||||
|
];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="pb-24">
|
<div className="pb-24">
|
||||||
{!singleton && (
|
{!singleton && (
|
||||||
|
|
@ -318,18 +364,29 @@ export default function EntityEdit() {
|
||||||
{isNew ? `New ${manifest.singular}` : heading}
|
{isNew ? `New ${manifest.singular}` : heading}
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
|
<div className="mt-4">
|
||||||
|
<SectionJump sections={sections} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="lg:grid lg:grid-cols-[13rem_minmax(0,1fr)] lg:items-start lg:gap-10">
|
||||||
|
<div className="lg:pt-6">
|
||||||
|
<SectionNav sections={sections} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="min-w-0">
|
||||||
|
|
||||||
{/* Slug. An auto-id entity has nothing to ask for on create, and
|
{/* Slug. An auto-id entity has nothing to ask for on create, and
|
||||||
nothing editable afterwards, so it gets a plain line rather
|
nothing editable afterwards, so it gets a plain line rather
|
||||||
than a disabled box pretending to be a field. */}
|
than a disabled box pretending to be a field. */}
|
||||||
{singleton ? (
|
{singleton ? (
|
||||||
updatedAt && (
|
updatedAt && (
|
||||||
<div className="mt-6 rounded-2xl border border-[#138ba0]/20 bg-white p-5">
|
<div className={CARD}>
|
||||||
<p className="text-sm text-[#4a6b72]">Last saved {updatedAt}</p>
|
<p className="text-sm text-[#4a6b72]">Last saved {updatedAt}</p>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
) : autoId ? (
|
) : autoId ? (
|
||||||
!isNew && (
|
!isNew && (
|
||||||
<div className="mt-6 rounded-2xl border border-[#138ba0]/20 bg-white p-5">
|
<div id="section-url-id" className={CARD}>
|
||||||
<p className="text-sm text-[#4a6b72]">
|
<p className="text-sm text-[#4a6b72]">
|
||||||
{manifest.idLabel} #{form.id}
|
{manifest.idLabel} #{form.id}
|
||||||
{form.updated_at && <> · last saved {form.updated_at}</>}
|
{form.updated_at && <> · last saved {form.updated_at}</>}
|
||||||
|
|
@ -337,7 +394,7 @@ export default function EntityEdit() {
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
) : (
|
) : (
|
||||||
<div className="mt-6 rounded-2xl border border-[#138ba0]/20 bg-white p-5">
|
<div id="section-url-id" className={CARD}>
|
||||||
<Field
|
<Field
|
||||||
field={{
|
field={{
|
||||||
path: "id",
|
path: "id",
|
||||||
|
|
@ -368,11 +425,8 @@ export default function EntityEdit() {
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Field groups */}
|
{/* Field groups */}
|
||||||
{manifest.groups.filter((group) => visible(group.when)).map((group) => (
|
{groups.map((group) => (
|
||||||
<section
|
<section key={group.legend} id={sectionId(group.legend)} className={CARD}>
|
||||||
key={group.legend}
|
|
||||||
className="mt-6 rounded-2xl border border-[#138ba0]/20 bg-white p-5"
|
|
||||||
>
|
|
||||||
<h2 className="text-lg font-semibold text-[#26454c]">{group.legend}</h2>
|
<h2 className="text-lg font-semibold text-[#26454c]">{group.legend}</h2>
|
||||||
{group.note && <p className="mt-1 text-sm text-[#4a6b72]">{group.note}</p>}
|
{group.note && <p className="mt-1 text-sm text-[#4a6b72]">{group.note}</p>}
|
||||||
<div className="mt-4">
|
<div className="mt-4">
|
||||||
|
|
@ -393,25 +447,23 @@ export default function EntityEdit() {
|
||||||
</section>
|
</section>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
{/* Child collections. Awards own none, so the panel would be
|
{/* Child collections, a card each, so the front page's slides,
|
||||||
an empty white box; skip it rather than render it. */}
|
stats and paths read as separate things. */}
|
||||||
{children.some((child) => visible(child.when)) && (
|
{shownChildren.map((child) => (
|
||||||
<div className="mt-6 rounded-2xl border border-[#138ba0]/20 bg-white p-5">
|
<div key={child.key} id={sectionId(`${child.key}-list`)} className={CARD}>
|
||||||
{children
|
|
||||||
.filter((child) => visible(child.when))
|
|
||||||
.map((child) => (
|
|
||||||
<Repeater
|
<Repeater
|
||||||
key={child.key}
|
|
||||||
spec={child}
|
spec={child}
|
||||||
rows={form[child.key]}
|
rows={form[child.key]}
|
||||||
options={options}
|
options={options}
|
||||||
errors={errors}
|
errors={errors}
|
||||||
errorPrefix={`${child.key}.`}
|
errorPrefix={`${child.key}.`}
|
||||||
|
className=""
|
||||||
onChange={(rows) => setForm((prev) => ({ ...prev, [child.key]: rows }))}
|
onChange={(rows) => setForm((prev) => ({ ...prev, [child.key]: rows }))}
|
||||||
/>
|
/>
|
||||||
|
</div>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
)}
|
</div>
|
||||||
|
|
||||||
{/* Sticky action bar */}
|
{/* Sticky action bar */}
|
||||||
<div className="fixed inset-x-0 bottom-0 border-t border-[#138ba0]/20 bg-white/95 backdrop-blur">
|
<div className="fixed inset-x-0 bottom-0 border-t border-[#138ba0]/20 bg-white/95 backdrop-blur">
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue