Files
crosscode-platform/apps/website/frontend/src/app/admin/publish/page.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

113 lines
6.8 KiB
TypeScript

"use client";
/**
* Publish and history (WEBSITE-1.0-PLAN §4): every pending change (what, who, when), Discard for
* any one, one Publish for the whole site; links to share the preview; every published version,
* any of which can be restored into the working copy.
*/
import { useState } from "react";
import { api, canEdit } from "@/admin/api";
import { Btn, Field, inputClass, Loading, Notice, Panel, Title, useAction, useAdmin, when } from "@/admin/ui";
type Change = { key: string; kind: "added" | "changed" | "removed"; what: string; label: string; by: string | null; at: string | null };
type Version = { number: number; publishedAt: string; by: string | null; note: string | null };
type Link = { id: string; createdAt: string; expiresAt: string; by: string | null; active: boolean };
const WHAT: Record<string, string> = { design: "Template and style", page: "Page", ministry: "Ministry", leader: "Team" };
const KIND: Record<string, string> = { added: "Added", changed: "Changed", removed: "Removed" };
export default function Publish() {
const me = useAdmin<{ user: { role: string } }>("/me");
const pending = useAdmin<Change[]>("/changes");
const versions = useAdmin<Version[]>("/versions");
const editor = !!me.data && canEdit(me.data.user.role);
const links = useAdmin<Link[]>(editor ? "/preview-links" : null);
const [note, setNote] = useState("");
const [newLink, setNewLink] = useState<string | null>(null);
const { busy, message, run } = useAction();
if (!pending.data || !versions.data) return <Loading error={pending.error ?? versions.error} />;
const first = versions.data.length === 0;
const reloadAll = async () => { await Promise.all([pending.reload(), versions.reload(), links.reload()]); };
return (
<>
<Title sub={first ? "Until you publish, visitors see Coming soon." : "Visitors see the last version published."}
actions={<>
{/* eslint-disable-next-line @next/next/no-html-link-for-pages -- a route handler that sets the preview cookie: a full page load, not a client navigation */}
<a href="/preview/start" className="rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-semibold hover:bg-gray-50">Preview the whole site</a>
</>}>
Publish and history
</Title>
{message && <div className="mb-4"><Notice tone={message.tone}>{message.text}</Notice></div>}
<div className="space-y-6">
<Panel title={pending.data.length ? `${pending.data.length} ${pending.data.length === 1 ? "change" : "changes"} not published` : "Nothing waiting to be published"}>
{pending.data.length > 0 && (
<ul className="mb-4 divide-y divide-gray-200">
{pending.data.map((c) => (
<li key={c.key} className="flex flex-wrap items-center justify-between gap-3 py-2">
<div>
<p><span className="font-medium">{KIND[c.kind]}:</span> {WHAT[c.what]} {c.what !== "design" && <>“{c.label}”</>}</p>
<p className="text-xs text-gray-500">{[c.by, when(c.at)].filter(Boolean).join(", ")}</p>
</div>
{editor && <Btn busy={busy} onClick={() => run(async () => { await api("/discard", { body: { key: c.key } }); await reloadAll(); }, "Discarded: back to the published version.")}>Discard</Btn>}
</li>
))}
</ul>
)}
{editor ? (
(pending.data.length > 0 || first) && (
<div className="space-y-3">
<Field label="A note for the history (optional)"><input className={inputClass} value={note} maxLength={500} onChange={(e) => setNote(e.target.value)} placeholder="E.g. Christmas service times" /></Field>
<Btn kind="primary" busy={busy} onClick={() => run(async () => {
const r = await api<{ number: number }>("/publish", { body: { note } });
// Visitors get it at once, not after the 30-second cache.
await fetch("/revalidate", { method: "POST" }).catch(() => {});
setNote("");
await reloadAll();
return r;
}, first ? "Published: your site is live." : "Published: visitors see it now.")}>{first ? "Publish your site" : "Publish all changes"}</Btn>
</div>
)
) : <p className="text-sm text-gray-600">An editor publishes your changes.</p>}
</Panel>
{editor && (
<Panel title="Share the preview">
<p className="mb-3 text-sm text-gray-600">A link for someone without an account (your pastor, say) to see the whole site as it would look published. It works for 7 days, or until you stop it, and can&apos;t change anything.</p>
<Btn busy={busy} onClick={() => run(async () => { const l = await api<{ url: string }>("/preview-links", { body: {} }); setNewLink(l.url); await links.reload(); })}>Make a preview link</Btn>
{newLink && (
<div className="mt-3"><Notice tone="success">Copy this link now (it isn&apos;t shown again): <code className="break-all font-mono">{newLink}</code></Notice></div>
)}
{links.data && links.data.length > 0 && (
<ul className="mt-4 divide-y divide-gray-200 text-sm">
{links.data.map((l) => (
<li key={l.id} className="flex items-center justify-between gap-3 py-2">
<span>Made {when(l.createdAt)}{l.by && ` by ${l.by}`} · {l.active ? `works until ${when(l.expiresAt)}` : "stopped"}</span>
{l.active && <Btn kind="danger" onClick={() => run(async () => { await api(`/preview-links/${l.id}`, { method: "DELETE" }); await links.reload(); }, "The link stopped working.")}>Stop it</Btn>}
</li>
))}
</ul>
)}
</Panel>
)}
<Panel title="History">
{versions.data.length === 0 ? <p className="text-gray-600">Not published yet.</p> : (
<ul className="divide-y divide-gray-200">
{versions.data.map((v, i) => (
<li key={v.number} className="flex flex-wrap items-center justify-between gap-3 py-2">
<div>
<p className="font-medium">Version {v.number}{i === 0 && " (live)"}{v.note && <span className="font-normal text-gray-600"> · {v.note}</span>}</p>
<p className="text-xs text-gray-500">{when(v.publishedAt)}{v.by && `, ${v.by}`}</p>
</div>
{editor && i > 0 && <Btn onClick={() => run(async () => { await api(`/versions/${v.number}/restore`, { body: {} }); await reloadAll(); }, `Version ${v.number} is back in your drafts: preview it, then publish.`)}>Restore</Btn>}
</li>
))}
</ul>
)}
</Panel>
</div>
</>
);
}