"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(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) => ( {label} ); return (

Preview · {preview.by === "admin" ? "your changes, not live yet" : "not live yet"}

{width("phone", "Phone", "/preview/frame?size=phone")} {width("tablet", "Tablet", "/preview/frame?size=tablet")} {width("desktop", "Desktop", "/")}
{error && {error}} {/* 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 */} {preview.by === "admin" ? "Back to editing" : "Leave the preview"} {preview.canPublish && ( )}
); }