/* ═══════════════════════════════════════════════════════════════ ADMIN: ENTITY LIST One component for organizations, events and people. The :entity route param picks the manifest; nothing here knows what a group or a retreat is. The "New X" link follows the same rule as EntityEdit's save button: atLeast(user, "editor"), matching requireRole("editor") on POST /api/admin/:entity. Drawing it is not permission (the server decides); it only avoids offering a click that 403s, and avoids hiding one that wouldn't. ═══════════════════════════════════════════════════════════════ */ import { useCallback, useEffect, useState } from "react"; import { Link, Navigate, useNavigate, useParams, useSearchParams } from "react-router-dom"; import { get, upload, ApiError } from "../../lib/api.ts"; import { isUnauthorized, useAuth } from "../../lib/auth.tsx"; import { ADMIN_ENTITIES, optionsPath } from "../../lib/adminSchema.ts"; import { atLeast } from "../../lib/roles.ts"; export default function EntityList() { const { entity: entityKey } = useParams(); const manifest = ADMIN_ENTITIES[entityKey ?? ""]; const navigate = useNavigate(); const { user } = useAuth(); const [params, setParams] = useSearchParams(); const [rows, setRows] = useState([]); const [total, setTotal] = useState(0); const [loadingMore, setLoadingMore] = useState(false); const [options, setOptions] = useState({}); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [query, setQuery] = useState(params.get("q") ?? ""); // Minimum rank, never equality. POST /api/admin/:entity is gated // at "editor", so anyone from editor upward may create, and the // equality test this replaced hid the button from superadmins as // well as editors, which is the failure mode an == against a // ladder always produces once a rank is added above it. const canWrite = atLeast(user, manifest?.writeRole ?? "editor"); // The cell that carries the row's link: the primary column, else the first. const columns = manifest?.list.columns ?? []; const linkKey = (columns.find((c) => c.primary) ?? columns[0])?.key; const load = useCallback(async () => { if (!manifest || manifest.singleton) return; setLoading(true); setError(null); try { const optsPath = optionsPath(manifest.list.filters); const [list, opts] = await Promise.all([ get(`/admin/${manifest.key}?${params}`, { ttl: 0 }), optsPath ? get(optsPath, { ttl: 60_000 }) : { options: {} }, ]); setRows(list.rows); setTotal(list.total); setOptions(opts.options); } catch (err) { if (isUnauthorized(err)) return navigate("/admin/login", { replace: true }); setError(err instanceof ApiError ? err.message : "Couldn't reach the server."); } finally { setLoading(false); } }, [manifest, params, navigate]); useEffect(() => { load(); }, [load]); // The server sends a page at a time; the next one goes under it. async function loadMore() { setLoadingMore(true); try { const next = new URLSearchParams(params); next.set("offset", String(rows.length)); const list = await get(`/admin/${manifest.key}?${next}`, { ttl: 0 }); setRows((current) => [...current, ...list.rows]); setTotal(list.total); } catch (err) { setError(err instanceof ApiError ? err.message : "Couldn't reach the server."); } finally { setLoadingMore(false); } } if (!manifest) { return

No such thing to edit.

; } // One row, so no list: the tab opens the row. if (manifest.singleton) { return ; } function setParam(key, value) { const next = new URLSearchParams(params); if (value) next.set(key, value); else next.delete(key); setParams(next, { replace: true }); } function labelFor(filter) { if (filter.optionsFrom) { return (options[filter.optionsFrom] ?? []).map((o) => [o.id, o.label]); } return filter.options.map((o) => (Array.isArray(o) ? o : [o, o])); } return (

{manifest.label}

{canWrite && manifest.importer && } {canWrite && ( New {manifest.singular} )}
{/* Filters */}
{manifest.list.filters.map((filter) => ( ))}
setQuery(e.target.value)} onKeyDown={(e) => e.key === "Enter" && setParam("q", query.trim())} className="border border-muted/25 bg-surface px-4 py-1.5 text-sm text-body outline-none focus:border-brand" />
{error && (

{error}

)} {/* Rows */}
{manifest.list.columns.map((column) => ( ))} {/* The row click is for the mouse; the link in the primary cell is what a keyboard or screen reader uses. */} {rows.map((row) => ( navigate(`/admin/${manifest.key}/${row.id}`)} > {manifest.list.columns.map((column) => ( ))} ))}
{column.label} Slug
{column.key === linkKey ? ( e.stopPropagation()} className="hover:underline" > {row[column.key] || row.id} ) : column.widget === "bool" ? ( row[column.key] ? ( "Yes" ) : ( "-" ) ) : ( row[column.key] || "-" )} {row.id}
{!loading && rows.length === 0 && (

Nothing matches.

)} {loading &&

Loading…

}
{!loading && rows.length < total && (

{rows.length} of {total}

)}
); } /* A manifest's `importer`: pick a file, the server turns it into a new record, and its editor opens with a note of what came over (EntityEdit shows it). Blog posts take Word documents. */ function ImportButton({ manifest }: { manifest: any }) { const navigate = useNavigate(); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); async function send(file: File) { setBusy(true); setError(null); const form = new FormData(); form.set("file", file); try { const result = await upload(manifest.importer.endpoint, form); navigate(`/admin/${manifest.key}/${result.id}`, { state: { imported: result.summary } }); } catch (err) { setError(err instanceof ApiError ? err.message : "Couldn't reach the server."); setBusy(false); } } return (
{error && ( {error} )}
); }