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>
59 lines
3.1 KiB
TypeScript
59 lines
3.1 KiB
TypeScript
"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>
|
|
);
|
|
}
|