- 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>
153 lines
8.2 KiB
TypeScript
153 lines
8.2 KiB
TypeScript
"use client";
|
|
|
|
/**
|
|
* The live preview beside an editor: the page as it will look, with your saved drafts, on the right.
|
|
* It folds away to a tab on the right edge (remembered in this browser), shows a desktop or a phone
|
|
* width (the desktop scaled down to fit), and reloads where you were scrolled to each time
|
|
* something is saved. On a small screen it's a button that opens it full screen.
|
|
*
|
|
* The frame's pages are the working copy because they're framed and you're signed in (site/load.ts).
|
|
*/
|
|
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
import { Eye, ExternalLink, Monitor, PanelRightClose, PanelRightOpen, RotateCw, Smartphone, X } from "lucide-react";
|
|
import { cx } from "./ui";
|
|
|
|
type Device = "desktop" | "phone";
|
|
const WIDTH: Record<Device, number> = { desktop: 1280, phone: 390 };
|
|
const OPEN_KEY = "ccw-admin-preview-open";
|
|
const DEVICE_KEY = "ccw-admin-preview-device";
|
|
|
|
const read = (k: string) => { try { return window.localStorage.getItem(k); } catch { return null; } };
|
|
const write = (k: string, v: string) => { try { window.localStorage.setItem(k, v); } catch { /* not kept */ } };
|
|
|
|
function Frame({ path, version, device }: { path: string; version: string; device: Device }) {
|
|
const box = useRef<HTMLDivElement>(null);
|
|
const frame = useRef<HTMLIFrameElement>(null);
|
|
const scroll = useRef(0);
|
|
const shown = useRef(version);
|
|
const [size, setSize] = useState({ w: 0, h: 0 });
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useLayoutEffect(() => {
|
|
const el = box.current;
|
|
if (!el) return;
|
|
const ro = new ResizeObserver(([e]) => setSize({ w: e!.contentRect.width, h: e!.contentRect.height }));
|
|
ro.observe(el);
|
|
return () => ro.disconnect();
|
|
}, []);
|
|
|
|
// A save: reload the page in the frame (wherever it was clicked to), back at the same place.
|
|
useEffect(() => {
|
|
if (shown.current === version) return;
|
|
shown.current = version;
|
|
const w = frame.current?.contentWindow;
|
|
if (!w) return;
|
|
try {
|
|
scroll.current = w.scrollY;
|
|
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
setLoading(true);
|
|
w.location.reload();
|
|
} catch {
|
|
// Not the site's own page any more (a link out): leave it.
|
|
}
|
|
}, [version]);
|
|
|
|
const onLoad = useCallback(() => {
|
|
setLoading(false);
|
|
const w = frame.current?.contentWindow;
|
|
if (w && scroll.current) {
|
|
try { w.scrollTo(0, scroll.current); } catch { /* elsewhere */ }
|
|
scroll.current = 0;
|
|
}
|
|
}, []);
|
|
|
|
// The page at its real width, scaled down to fit (a phone has a margin round it).
|
|
const logical = WIDTH[device];
|
|
const pad = device === "phone" ? 12 : 0;
|
|
const scale = size.w ? Math.min(1, (size.w - pad * 2) / logical) : 1;
|
|
return (
|
|
<div ref={box} className="relative min-h-0 flex-1 overflow-hidden bg-gray-100">
|
|
{size.w > 0 && (
|
|
<iframe ref={frame} src={path} title="Live preview of this page" onLoad={onLoad}
|
|
className={cx("absolute origin-top-left border-0 bg-white", device === "phone" && "rounded-2xl shadow-lg ring-1 ring-gray-300")}
|
|
style={{ top: pad, left: (size.w - logical * scale) / 2, width: logical, height: (size.h - pad * 2) / scale, transform: `scale(${scale})` }} />
|
|
)}
|
|
{loading && <div className="pointer-events-none absolute right-3 top-3 rounded-full bg-white/90 px-2.5 py-1 text-xs text-gray-600 shadow" role="status">Loading…</div>}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function Toolbar({ path, device, setDevice, onReload, onClose, closeIcon }: { path: string; device: Device; setDevice: (d: Device) => void; onReload: () => void; onClose: () => void; closeIcon: "panel" | "x" }) {
|
|
const btn = "rounded-md p-1.5 text-gray-500 hover:bg-gray-100 hover:text-gray-900";
|
|
return (
|
|
<div className="flex items-center gap-2 border-b border-gray-200 bg-white px-3 py-2">
|
|
<Eye className="h-4 w-4 text-brand-text" aria-hidden />
|
|
<div className="min-w-0 flex-1">
|
|
<p className="text-sm font-semibold text-gray-900">Live preview</p>
|
|
<p className="truncate text-xs text-gray-500">Updates each time you save · {path === "/" ? "Home page" : path}</p>
|
|
</div>
|
|
<div className="flex rounded-lg border border-gray-200 p-0.5" role="group" aria-label="Width">
|
|
{(["desktop", "phone"] as const).map((d) => {
|
|
const Icon = d === "desktop" ? Monitor : Smartphone;
|
|
return (
|
|
<button key={d} type="button" onClick={() => setDevice(d)} aria-pressed={device === d} aria-label={d === "desktop" ? "Computer width" : "Phone width"} title={d === "desktop" ? "Computer" : "Phone"}
|
|
className={cx("rounded-md p-1", device === d ? "bg-brand/10 text-brand-text" : "text-gray-500 hover:text-gray-900")}><Icon className="h-4 w-4" aria-hidden /></button>
|
|
);
|
|
})}
|
|
</div>
|
|
<button type="button" onClick={onReload} className={btn} aria-label="Reload the preview" title="Reload"><RotateCw className="h-4 w-4" aria-hidden /></button>
|
|
<a href={`/preview/start?to=${encodeURIComponent(path)}`} target="_blank" rel="noopener" className={btn} aria-label="Open the full preview in a new tab" title="Open the full preview"><ExternalLink className="h-4 w-4" aria-hidden /></a>
|
|
<button type="button" onClick={onClose} className={btn} aria-label="Hide the preview" title="Hide the preview">
|
|
{closeIcon === "panel" ? <PanelRightClose className="h-4 w-4" aria-hidden /> : <X className="h-4 w-4" aria-hidden />}
|
|
</button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** An editor with the live preview of `path` beside it. `version` changes with each save (the saved data, say): the preview reloads. */
|
|
export function WithPreview({ path, version, children }: { path: string; version: string; children: React.ReactNode }) {
|
|
const [open, setOpenState] = useState(true);
|
|
const [device, setDeviceState] = useState<Device>("desktop");
|
|
const [sheet, setSheet] = useState(false);
|
|
const [manual, setManual] = useState(0);
|
|
useEffect(() => {
|
|
// The browser remembers the last choice.
|
|
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
if (read(OPEN_KEY) === "0") setOpenState(false);
|
|
if (read(DEVICE_KEY) === "phone") setDeviceState("phone");
|
|
}, []);
|
|
const setOpen = (v: boolean) => { setOpenState(v); write(OPEN_KEY, v ? "1" : "0"); };
|
|
const setDevice = (d: Device) => { setDeviceState(d); write(DEVICE_KEY, d); };
|
|
const v = `${version}·${manual}`;
|
|
|
|
return (
|
|
<div className="lg:flex lg:items-start lg:gap-6">
|
|
<div className="min-w-0 flex-1">{children}</div>
|
|
|
|
{/* Wide screens: beside the editor, or folded to a tab on the right edge. */}
|
|
{open ? (
|
|
<aside aria-label="Live preview" className="sticky top-[77px] hidden h-[calc(100vh-101px)] w-[45%] max-w-[52rem] shrink-0 flex-col overflow-hidden rounded-xl border border-gray-200 bg-white shadow-sm lg:flex">
|
|
<Toolbar path={path} device={device} setDevice={setDevice} onReload={() => setManual((n) => n + 1)} onClose={() => setOpen(false)} closeIcon="panel" />
|
|
<Frame path={path} version={v} device={device} />
|
|
</aside>
|
|
) : (
|
|
<button type="button" onClick={() => setOpen(true)} aria-label="Show the live preview"
|
|
className="fixed right-0 top-1/2 z-20 hidden -translate-y-1/2 items-center gap-2 rounded-l-xl border border-r-0 border-gray-200 bg-white px-2 py-4 text-sm font-semibold text-gray-700 shadow-lg hover:text-brand-text lg:flex [writing-mode:vertical-rl]">
|
|
<PanelRightOpen className="h-4 w-4 rotate-90" aria-hidden /> Show preview
|
|
</button>
|
|
)}
|
|
|
|
{/* Small screens: a button that opens it full screen. */}
|
|
<button type="button" onClick={() => setSheet(true)} className="fixed bottom-4 right-4 z-20 inline-flex items-center gap-2 rounded-full bg-brand px-4 py-3 text-sm font-semibold text-brand-on shadow-lg lg:hidden">
|
|
<Eye className="h-4 w-4" aria-hidden /> Preview
|
|
</button>
|
|
{sheet && (
|
|
<div className="fixed inset-0 z-50 flex flex-col bg-white lg:hidden" role="dialog" aria-modal="true" aria-label="Live preview">
|
|
<Toolbar path={path} device={device} setDevice={setDevice} onReload={() => setManual((n) => n + 1)} onClose={() => setSheet(false)} closeIcon="x" />
|
|
<Frame path={path} version={v} device={device} />
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|