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>
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
"use client";
|
||||
|
||||
/**
|
||||
* The preview bar (WEBSITE-1.0-PLAN §4): above the page (so the page is laid out exactly as it
|
||||
* will be), with phone / tablet / desktop width, Back to editing, and Publish for an editor. Inside
|
||||
* the width frame it hides itself: the frame page has its own.
|
||||
*/
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export function PreviewBar({ preview, size = "desktop" }: { preview: { by: "admin" | "link"; canPublish: boolean }; size?: "phone" | "tablet" | "desktop" }) {
|
||||
const [inFrame, setInFrame] = useState(false);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
useEffect(() => {
|
||||
// Only the browser knows whether this page is inside the width frame.
|
||||
// eslint-disable-next-line react-hooks/set-state-in-effect
|
||||
setInFrame(window.self !== window.top);
|
||||
}, []);
|
||||
if (inFrame) return null;
|
||||
const publish = async () => {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
const res = await fetch("/api/v1/admin/publish", { method: "POST", headers: { "Content-Type": "application/json" }, body: "{}" });
|
||||
if (res.ok) {
|
||||
await fetch("/revalidate", { method: "POST" }).catch(() => {});
|
||||
// A full page load: the route clears the preview cookie.
|
||||
// eslint-disable-next-line @next/next/no-location-assign-relative-destination
|
||||
window.location.href = "/preview/exit?published=1";
|
||||
}
|
||||
else {
|
||||
setError(((await res.json().catch(() => null)) as { error?: { message?: string } } | null)?.error?.message ?? "Publishing didn't work.");
|
||||
setBusy(false);
|
||||
}
|
||||
};
|
||||
const width = (s: string, label: string, href: string) => (
|
||||
<a href={href} aria-current={size === s ? "true" : undefined} className={`rounded px-2 py-1 ${size === s ? "bg-white text-gray-900" : "hover:bg-white/10"}`}>{label}</a>
|
||||
);
|
||||
return (
|
||||
<div className="bg-gray-900 font-sans text-sm text-white" role="region" aria-label="Preview">
|
||||
<div className="mx-auto flex max-w-6xl flex-wrap items-center justify-between gap-3 px-4 py-2">
|
||||
<p><strong>Preview</strong> · {preview.by === "admin" ? "your changes, not live yet" : "not live yet"}</p>
|
||||
<div className="flex items-center gap-1" aria-label="Width">
|
||||
{width("phone", "Phone", "/preview/frame?size=phone")}
|
||||
{width("tablet", "Tablet", "/preview/frame?size=tablet")}
|
||||
{width("desktop", "Desktop", "/")}
|
||||
</div>
|
||||
<div className="flex items-center gap-3">
|
||||
{error && <span role="alert" className="text-amber-300">{error}</span>}
|
||||
{/* eslint-disable-next-line @next/next/no-html-link-for-pages -- a route handler that clears the preview cookie: a full page load, not a client navigation */}
|
||||
<a href="/preview/exit" className="underline-offset-4 hover:underline">{preview.by === "admin" ? "Back to editing" : "Leave the preview"}</a>
|
||||
{preview.canPublish && (
|
||||
<button type="button" onClick={publish} disabled={busy} className="rounded bg-white px-3 py-1.5 font-semibold text-gray-900 disabled:opacity-60">{busy ? "Publishing…" : "Publish"}</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { cookies } from "next/headers";
|
||||
import { cache } from "react";
|
||||
import type { Image, Published, SiteContent, Template } from "./types";
|
||||
|
||||
@@ -13,14 +14,33 @@ const FALLBACK: SiteContent = {
|
||||
};
|
||||
|
||||
/**
|
||||
* The site as visitors see it: one fetch per request (React `cache`), kept by Next for 30 seconds,
|
||||
* so Publish shows within half a minute. While the backend can't answer: Coming soon, never an
|
||||
* error page.
|
||||
* The site as visitors see it: one fetch per request (React `cache`), kept by Next for
|
||||
* WEBSITE_CONTENT_CACHE_SECONDS (30; tag `site-content`, which Publish clears at once through
|
||||
* app/revalidate). While the backend can't answer: Coming soon, never an error page.
|
||||
*/
|
||||
export const loadContent = cache(async (): Promise<SiteContent> => {
|
||||
// Previewing (§4): the working copy, if the backend agrees this is an admin or a live share link.
|
||||
const jar = await cookies();
|
||||
const preview = jar.get("ccw_preview")?.value;
|
||||
if (preview) {
|
||||
try {
|
||||
const session = jar.get("ccw_session")?.value;
|
||||
const res = await fetch(`${api}/api/v1/site/preview`, {
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
...(session ? { Cookie: `ccw_session=${encodeURIComponent(session)}` } : {}),
|
||||
...(preview !== "admin" ? { "x-preview-token": preview } : {}),
|
||||
},
|
||||
});
|
||||
if (res.ok) return (await res.json()) as SiteContent;
|
||||
} catch {
|
||||
// Falls back to the live site.
|
||||
}
|
||||
}
|
||||
try {
|
||||
// In development (and the e2e run) always fresh, so a Publish shows at once.
|
||||
const res = await fetch(`${api}/api/v1/site/content`, process.env.NODE_ENV === "production" ? { next: { revalidate: 30 } } : { cache: "no-store" });
|
||||
// Not kept in development, or when set to 0 (the e2e run changes content behind Next's back).
|
||||
const seconds = process.env.NODE_ENV === "production" ? Number(process.env.WEBSITE_CONTENT_CACHE_SECONDS ?? 30) : 0;
|
||||
const res = await fetch(`${api}/api/v1/site/content`, seconds > 0 ? { next: { revalidate: seconds, tags: ["site-content"] } } : { cache: "no-store" });
|
||||
if (res.ok) return (await res.json()) as SiteContent;
|
||||
} catch {
|
||||
// Starting or down.
|
||||
|
||||
@@ -78,6 +78,8 @@ export type Published = Common & {
|
||||
transparentHeader: Record<string, boolean>;
|
||||
messages: SiteMessage[];
|
||||
events: SiteEvent[];
|
||||
/** Only when previewing the working copy (§4). */
|
||||
preview?: { by: "admin" | "link"; canPublish: boolean };
|
||||
};
|
||||
|
||||
export type SiteContent = ComingSoon | Published;
|
||||
|
||||
Reference in New Issue
Block a user