"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 = { design: "Template and style", page: "Page", ministry: "Ministry", leader: "Team" }; const KIND: Record = { added: "Added", changed: "Changed", removed: "Removed" }; export default function Publish() { const me = useAdmin<{ user: { role: string } }>("/me"); const pending = useAdmin("/changes"); const versions = useAdmin("/versions"); const editor = !!me.data && canEdit(me.data.user.role); const links = useAdmin(editor ? "/preview-links" : null); const [note, setNote] = useState(""); const [newLink, setNewLink] = useState(null); const { busy, message, run } = useAction(); if (!pending.data || !versions.data) return ; const first = versions.data.length === 0; const reloadAll = async () => { await Promise.all([pending.reload(), versions.reload(), links.reload()]); }; return ( <> {/* 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 {message &&
{message.text}
}
{pending.data.length > 0 && (
    {pending.data.map((c) => (
  • {KIND[c.kind]}: {WHAT[c.what]} {c.what !== "design" && <>“{c.label}”}

    {[c.by, when(c.at)].filter(Boolean).join(", ")}

    {editor && run(async () => { await api("/discard", { body: { key: c.key } }); await reloadAll(); }, "Discarded: back to the published version.")}>Discard}
  • ))}
)} {editor ? ( (pending.data.length > 0 || first) && (
setNote(e.target.value)} placeholder="E.g. Christmas service times" /> 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"}
) ) :

An editor publishes your changes.

}
{editor && (

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't change anything.

run(async () => { const l = await api<{ url: string }>("/preview-links", { body: {} }); setNewLink(l.url); await links.reload(); })}>Make a preview link {newLink && (
Copy this link now (it isn't shown again): {newLink}
)} {links.data && links.data.length > 0 && (
    {links.data.map((l) => (
  • Made {when(l.createdAt)}{l.by && ` by ${l.by}`} · {l.active ? `works until ${when(l.expiresAt)}` : "stopped"} {l.active && run(async () => { await api(`/preview-links/${l.id}`, { method: "DELETE" }); await links.reload(); }, "The link stopped working.")}>Stop it}
  • ))}
)}
)} {versions.data.length === 0 ?

Not published yet.

: (
    {versions.data.map((v, i) => (
  • Version {v.number}{i === 0 && " (live)"}{v.note && · {v.note}}

    {when(v.publishedAt)}{v.by && `, ${v.by}`}

    {editor && i > 0 && run(async () => { await api(`/versions/${v.number}/restore`, { body: {} }); await reloadAll(); }, `Version ${v.number} is back in your drafts: preview it, then publish.`)}>Restore}
  • ))}
)}
); }