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
|
|
@ -20,6 +20,7 @@ import { useCallback, useEffect, useMemo, useState } from "react";
|
|||
import { Link, NavLink, Outlet, useLocation, useNavigate } from "react-router-dom";
|
||||
import { useAuth } from "../../lib/auth.tsx";
|
||||
import { AdminTitleContext } from "../../lib/adminTitle.tsx";
|
||||
import { AdminWidthContext } from "../../lib/adminWidth.tsx";
|
||||
import { SITE_VERSION } from "../../lib/version.ts";
|
||||
import { ROLE_LABELS, isSuper } from "../../lib/roles.ts";
|
||||
import nguLogo from "../../assets/NGU_Logo.svg";
|
||||
|
|
@ -60,6 +61,10 @@ export default function AdminLayout() {
|
|||
const stableSet = useCallback((value) => setDetail(value), []);
|
||||
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(() => {
|
||||
// Sections only exist in the CMS. Home and Panel already say
|
||||
// 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
|
||||
bottom of a short page and below the content of a long one,
|
||||
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}>
|
||||
<Outlet />
|
||||
<AdminWidthContext.Provider value={setWide}>
|
||||
<Outlet />
|
||||
</AdminWidthContext.Provider>
|
||||
</AdminTitleContext.Provider>
|
||||
</main>
|
||||
|
||||
|
|
|
|||
|
|
@ -33,6 +33,12 @@
|
|||
None of this is protection. The server refuses the request; this
|
||||
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
|
||||
because the name is: two organizations aren't both called
|
||||
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 { isUnauthorized, useAuth } from "../../lib/auth.tsx";
|
||||
import { useAdminDetail } from "../../lib/adminTitle.tsx";
|
||||
import { useWideAdminPage } from "../../lib/adminWidth.tsx";
|
||||
import { ADMIN_ENTITIES, slugify } from "../../lib/adminSchema.ts";
|
||||
import { atLeast } from "../../lib/roles.ts";
|
||||
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
|
||||
delete fails here, and SQLite's own wording explains nothing to
|
||||
|
|
@ -66,6 +84,8 @@ export default function EntityEdit() {
|
|||
const navigate = useNavigate();
|
||||
const { user } = useAuth();
|
||||
|
||||
useWideAdminPage();
|
||||
|
||||
const isNew = id === "new";
|
||||
const canWrite = atLeast(user, "editor");
|
||||
const canDelete = atLeast(user, "admin");
|
||||
|
|
@ -302,6 +322,32 @@ export default function EntityEdit() {
|
|||
|
||||
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 (
|
||||
<div className="pb-24">
|
||||
{!singleton && (
|
||||
|
|
@ -318,18 +364,29 @@ export default function EntityEdit() {
|
|||
{isNew ? `New ${manifest.singular}` : heading}
|
||||
</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
|
||||
nothing editable afterwards, so it gets a plain line rather
|
||||
than a disabled box pretending to be a field. */}
|
||||
{singleton ? (
|
||||
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>
|
||||
</div>
|
||||
)
|
||||
) : autoId ? (
|
||||
!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]">
|
||||
{manifest.idLabel} #{form.id}
|
||||
{form.updated_at && <> · last saved {form.updated_at}</>}
|
||||
|
|
@ -337,7 +394,7 @@ export default function EntityEdit() {
|
|||
</div>
|
||||
)
|
||||
) : (
|
||||
<div className="mt-6 rounded-2xl border border-[#138ba0]/20 bg-white p-5">
|
||||
<div id="section-url-id" className={CARD}>
|
||||
<Field
|
||||
field={{
|
||||
path: "id",
|
||||
|
|
@ -368,11 +425,8 @@ export default function EntityEdit() {
|
|||
)}
|
||||
|
||||
{/* Field groups */}
|
||||
{manifest.groups.filter((group) => visible(group.when)).map((group) => (
|
||||
<section
|
||||
key={group.legend}
|
||||
className="mt-6 rounded-2xl border border-[#138ba0]/20 bg-white p-5"
|
||||
>
|
||||
{groups.map((group) => (
|
||||
<section key={group.legend} id={sectionId(group.legend)} className={CARD}>
|
||||
<h2 className="text-lg font-semibold text-[#26454c]">{group.legend}</h2>
|
||||
{group.note && <p className="mt-1 text-sm text-[#4a6b72]">{group.note}</p>}
|
||||
<div className="mt-4">
|
||||
|
|
@ -393,25 +447,23 @@ export default function EntityEdit() {
|
|||
</section>
|
||||
))}
|
||||
|
||||
{/* Child collections. Awards own none, so the panel would be
|
||||
an empty white box; skip it rather than render it. */}
|
||||
{children.some((child) => visible(child.when)) && (
|
||||
<div className="mt-6 rounded-2xl border border-[#138ba0]/20 bg-white p-5">
|
||||
{children
|
||||
.filter((child) => visible(child.when))
|
||||
.map((child) => (
|
||||
<Repeater
|
||||
key={child.key}
|
||||
spec={child}
|
||||
rows={form[child.key]}
|
||||
options={options}
|
||||
errors={errors}
|
||||
errorPrefix={`${child.key}.`}
|
||||
onChange={(rows) => setForm((prev) => ({ ...prev, [child.key]: rows }))}
|
||||
/>
|
||||
))}
|
||||
{/* Child collections, a card each, so the front page's slides,
|
||||
stats and paths read as separate things. */}
|
||||
{shownChildren.map((child) => (
|
||||
<div key={child.key} id={sectionId(`${child.key}-list`)} className={CARD}>
|
||||
<Repeater
|
||||
spec={child}
|
||||
rows={form[child.key]}
|
||||
options={options}
|
||||
errors={errors}
|
||||
errorPrefix={`${child.key}.`}
|
||||
className=""
|
||||
onChange={(rows) => setForm((prev) => ({ ...prev, [child.key]: rows }))}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Sticky action bar */}
|
||||
<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