Files
crosscode-platform/apps/website/frontend/src/site/load.ts
T
joshuaandClaude Opus 5.5 dcc22a5c16 Accounts 1.8.0, Website 1.1.0: admins redesigned as Cross Events' dashboard and settings
- 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>
2026-11-26 19:57:22 +02:00

67 lines
3.2 KiB
TypeScript

import { cookies, headers } from "next/headers";
import { cache } from "react";
import type { Image, Published, SiteContent, Template } from "./types";
const api = (process.env.WEBSITE_API_URL || "http://127.0.0.1:5300").replace(/\/+$/, "");
const FALLBACK: SiteContent = {
mode: "coming-soon",
template: "heritage",
comingSoonText: "Our new website is on its way.",
church: null,
palette: { brand: "31 78 121", brandDark: "25 62 97", onBrand: "255 255 255", brandText: "31 78 121", brandTint: "241 244 248", accent: "87 113 148", onAccent: "255 255 255", notes: [] },
style: { fontPairing: "classic", corners: "sharp" },
};
/**
* 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.
// The admin's live preview beside its editors is the site in a frame: a signed-in admin's framed
// page is the working copy too, without the cookie (which would turn the whole browser to preview).
const jar = await cookies();
const framed = (await headers()).get("sec-fetch-dest") === "iframe" && jar.has("ccw_session");
const preview = jar.get("ccw_preview")?.value ?? (framed ? "admin" : undefined);
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 } : {}),
...(jar.get("ccw_preview_template")?.value ? { "x-preview-template": jar.get("ccw_preview_template")!.value } : {}),
},
});
if (res.ok) return (await res.json()) as SiteContent;
} catch {
// Falls back to the live site.
}
}
try {
// 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.
}
return FALLBACK;
});
export async function loadPublished(): Promise<Published | null> {
const c = await loadContent();
return c.mode === "published" ? c : null;
}
/** An image from a section's data (a media id), or null. */
export const media = (site: Published, id: unknown): Image | null => (typeof id === "string" ? site.snapshot.media[id] ?? null : null);
/** Per-template class names: `t(template, { heritage: "…", bold: "…" }, "fallback")`. */
export const t = (template: Template, by: Partial<Record<Template, string>>, otherwise = "") => by[template] ?? otherwise;
export const str = (v: unknown): string | null => (typeof v === "string" && v.trim() ? v : null);