Files
crosscode-platform/apps/website/frontend/src/site/load.ts
T
joshuaandClaude Opus 5.5 a9215ac4e4 Website 1.7.0: move and zoom photos in their frame; delete unused photos
- A section's photo (hero, welcome, invitation, image-text) shows in the
  shape of its frame for the current template and layout, with a Move and
  zoom dialog: drag or arrow keys to choose what stays in view, zoom to
  300%. Saved with the section as data.framing ({ x, y, zoom } by media
  id), a draft until published. The site draws it with object-position
  and scale() around that point; Bold split/collage frames now clip, and
  a zoomed photo stays inside Heritage's mat. Framing for photos a section
  no longer shows is dropped on save.
- Media library: each photo says where it's used; one used nowhere can be
  deleted with its files (DELETE /admin/media/:id, 409 IN_USE otherwise,
  audited as media_deleted).

Checked: Website backend tests (135) and typecheck, frontend typecheck,
lint and production build, and an e2e test of both flows in Chromium.
Lighthouse not run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 23:34:10 +02:00

71 lines
3.5 KiB
TypeScript

import { cookies, headers } from "next/headers";
import { cache } from "react";
import type { Focus, 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; placed as the section frames it (`data.framing`). */
export const media = (site: Published, id: unknown, data?: Record<string, unknown>): Image | null => {
const img = typeof id === "string" ? site.snapshot.media[id] ?? null : null;
const focus = img && (data?.framing as Record<string, Focus> | undefined)?.[id as string];
return img && focus ? { ...img, focus } : img;
};
/** 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);