Files
crosscode-platform/apps/website/frontend/src/app/admin/publish/page.tsx
T
joshuaandClaude Opus 5.5 dcc22a5c16 Accounts 1.8.0, Website 1.1.0: admins redesigned as Cross Events' dashboard and settings
- Website: an icon sidebar in groups (Your site, Content, Go live, Team), a
  dashboard with cards and a tile per part of the admin, steps to the first
  publish across the top of every admin page, and a live preview beside the
  page editor and Template and style (computer or phone width, folds away,
  reloads on save). A framed request (Sec-Fetch-Dest: iframe) with an admin
  session reads the drafts, so no preview cookie is set. /preview/start
  takes ?to=. The page editor has Content and Page details tabs.
- Accounts: /admin is an overview (cards, "Needs you", a tile per section);
  People moved to /admin/people; the sidebar shows the part you're in,
  grouped, with a count on Deletion requests; People redesigned, with a
  confirm before Deactivate; Email and WhatsApp split into two cards.
- Both: darker grey labels for contrast; lucide-react in the Website.
- e2e: the live preview shows drafts while the site stays live; axe on the
  page editor.
- docs: VERSIONS, changelogs, WHATS-NEW.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 19:57:22 +02:00

114 lines
6.9 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";
import { History, ListChecks, Send, Share2 } from "lucide-react";
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 icon={Send} 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 icon={ListChecks} 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 icon={Share2} 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 icon={History} 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>
</>
);
}