Files
crosscode-platform/apps/website/frontend/src/admin/ui.tsx
T
joshuaandClaude Opus 5.5 84c5b0c08e feat(website): the admin, with drafts, preview, publish and restore
Admin API and screens: dashboard and setup checklist, template and
style, pages and blocks, ministries, leadership, media library with
the storage limit, publish and history. Pending changes with who and
when, discard one, one Publish, restore any version (each version
keeps the working copy's rows, never sent to visitors). Preview on
the site's own addresses with a bar (widths, back, publish); shared
preview links for 7 days or until revoked. Roles checked, same-origin
changes only. Publish refreshes the public pages at once
(WEBSITE_CONTENT_CACHE_SECONDS). E2E on a production build: the
admin's flows and axe on every screen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 12:30:29 +02:00

120 lines
5.1 KiB
TypeScript

"use client";
/** The admin's own building blocks: plain, calm, the church's colour for the main action. */
import { useCallback, useEffect, useState } from "react";
import { api, AdminError } from "./api";
export const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" ");
export function Title({ children, sub, actions }: { children: React.ReactNode; sub?: React.ReactNode; actions?: React.ReactNode }) {
return (
<div className="mb-6 flex flex-wrap items-end justify-between gap-4">
<div>
<h1 className="text-2xl font-semibold text-gray-900">{children}</h1>
{sub && <p className="mt-1 text-gray-600">{sub}</p>}
</div>
{actions && <div className="flex flex-wrap gap-2">{actions}</div>}
</div>
);
}
export function Panel({ children, className, title }: { children: React.ReactNode; className?: string; title?: string }) {
return (
<section className={cx("rounded-lg border border-gray-200 bg-white p-5 shadow-sm", className)}>
{title && <h2 className="mb-4 text-lg font-semibold text-gray-900">{title}</h2>}
{children}
</section>
);
}
type BtnProps = React.ButtonHTMLAttributes<HTMLButtonElement> & { kind?: "primary" | "secondary" | "danger" | "link"; busy?: boolean };
export function Btn({ kind = "secondary", busy, className, children, disabled, ...rest }: BtnProps) {
return (
<button
type="button"
{...rest}
disabled={disabled || busy}
className={cx(
"inline-flex items-center justify-center gap-2 rounded-md px-4 py-2 text-sm font-semibold transition-colors disabled:cursor-not-allowed disabled:opacity-50",
kind === "primary" && "bg-brand text-brand-on hover:bg-brand-dark",
kind === "secondary" && "border border-gray-300 bg-white text-gray-800 hover:bg-gray-50",
kind === "danger" && "border border-red-300 bg-white text-red-700 hover:bg-red-50",
kind === "link" && "px-0 py-0 text-brand-text underline-offset-4 hover:underline",
className,
)}
>
{busy ? "Working…" : children}
</button>
);
}
export function Field({ label, hint, error, children }: { label: string; hint?: React.ReactNode; error?: string; children: React.ReactNode }) {
return (
<label className="block">
<span className="block text-sm font-medium text-gray-800">{label}</span>
<span className="mt-1 block">{children}</span>
{hint && !error && <span className="mt-1 block text-xs text-gray-500">{hint}</span>}
{error && <span className="mt-1 block text-xs font-medium text-red-700" role="alert">{error}</span>}
</label>
);
}
export const inputClass = "w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 shadow-sm focus:border-brand focus:outline-none focus:ring-2 focus:ring-brand/30";
export function Notice({ tone = "info", children }: { tone?: "info" | "error" | "success" | "warning"; children: React.ReactNode }) {
const tones = { info: "border-blue-200 bg-blue-50 text-blue-900", error: "border-red-200 bg-red-50 text-red-900", success: "border-green-200 bg-green-50 text-green-900", warning: "border-amber-200 bg-amber-50 text-amber-900" };
return <div role={tone === "error" ? "alert" : "status"} className={cx("rounded-md border px-4 py-3 text-sm", tones[tone])}>{children}</div>;
}
/** Loads `path` from the admin API; `reload` fetches it again. */
export function useAdmin<T>(path: string | null) {
const [data, setData] = useState<T | null>(null);
const [error, setError] = useState<string | null>(null);
const load = useCallback(async () => {
if (!path) return;
try {
setData(await api<T>(path));
setError(null);
} catch (e) {
setError((e as Error).message);
}
}, [path]);
useEffect(() => {
// Fetching on mount is this hook's job.
// eslint-disable-next-line react-hooks/set-state-in-effect
load();
}, [load]);
return { data, error, reload: load, setData };
}
/** Runs an action, keeping its busy state, message and field errors. */
export function useAction() {
const [busy, setBusy] = useState(false);
const [message, setMessage] = useState<{ tone: "error" | "success"; text: string } | null>(null);
const [fields, setFields] = useState<Record<string, string>>({});
const run = useCallback(async <T,>(fn: () => Promise<T>, success?: string): Promise<T | null> => {
setBusy(true);
setMessage(null);
setFields({});
try {
const out = await fn();
if (success) setMessage({ tone: "success", text: success });
return out;
} catch (e) {
setMessage({ tone: "error", text: (e as Error).message });
if (e instanceof AdminError) setFields(e.fields);
return null;
} finally {
setBusy(false);
}
}, []);
return { busy, message, fields, run, setMessage };
}
export function Loading({ error }: { error?: string | null }) {
return error ? <Notice tone="error">{error}</Notice> : <p className="text-gray-500">Loading…</p>;
}
export const when = (iso: string | null) => (iso ? new Intl.DateTimeFormat("en-ZA", { day: "numeric", month: "short", hour: "2-digit", minute: "2-digit" }).format(new Date(iso)) : "");