Files
crosscode-platform/apps/website/frontend/src/admin/fields.tsx
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

133 lines
8.3 KiB
TypeScript

"use client";
/**
* The forms for each homepage section and block, from one description per type (the backend's
* zod schemas in src/site/sections.ts check what's saved; field errors come back by path).
*/
import type { Focus } from "@/site/types";
import { FramingContext, type Framing } from "./framing";
import { ImagePicker } from "./media";
import { Btn, Field, inputClass } from "./ui";
type Spec =
| { key: string; kind: "text" | "textarea"; label: string; hint?: string; max?: number }
| { key: string; kind: "number"; label: string; min: number; max: number }
| { key: string; kind: "image"; label: string }
| { key: string; kind: "images"; label: string; max: number }
| { key: string; kind: "button"; label: string }
| { key: string; kind: "group"; label: string; fields: Spec[] };
const heading: Spec = { key: "heading", kind: "text", label: "Heading", max: 120 };
const button: Spec = { key: "button", kind: "button", label: "Button" };
export const SECTION_FIELDS: Record<string, { name: string; fields: Spec[] }> = {
hero: { name: "Top of the page (hero)", fields: [
{ key: "headline", kind: "text", label: "Headline", max: 120 },
{ key: "subline", kind: "textarea", label: "Under the headline", max: 300 },
{ key: "images", kind: "images", label: "Photos (the first for Photo and Split; up to three for Collage)", max: 3 },
button,
] },
serviceTimes: { name: "Service times and location", fields: [heading, { key: "note", kind: "textarea", label: "A note for visitors", hint: "The times and address come from your church profile.", max: 300 }] },
welcome: { name: "Welcome", fields: [
heading,
{ key: "mission", kind: "text", label: "Mission (one line)", max: 200 },
{ key: "body", kind: "textarea", label: "Introduction", hint: "Leave an empty line between paragraphs.", max: 4000 },
{ key: "imageId", kind: "image", label: "Photo (Image and text layout)" },
{ key: "pastor", kind: "group", label: "Pastor's photo and quote (With pastor layout)", fields: [
{ key: "name", kind: "text", label: "Name", max: 120 }, { key: "role", kind: "text", label: "Role", max: 80 },
{ key: "quote", kind: "textarea", label: "Quote", max: 400 }, { key: "photoId", kind: "image", label: "Photo" },
] },
] },
ministries: { name: "Ministries", fields: [heading, { key: "intro", kind: "textarea", label: "Introduction", max: 400 }] },
team: { name: "Team", fields: [heading, { key: "intro", kind: "textarea", label: "Introduction", max: 400 }] },
events: { name: "Upcoming events", fields: [heading, { key: "count", kind: "number", label: "How many to show", min: 2, max: 12 }] },
latestMessage: { name: "Latest message", fields: [heading] },
invitation: { name: "Invitation", fields: [heading, { key: "text", kind: "textarea", label: "Text", max: 400 }, button, { key: "imageId", kind: "image", label: "Background photo (Image background layout)" }] },
};
export const BLOCK_FIELDS: Record<string, { name: string; fields: Spec[] }> = {
text: { name: "Text", fields: [heading, { key: "body", kind: "textarea", label: "Text", hint: "Leave an empty line between paragraphs.", max: 20000 }] },
"image-text": { name: "Image and text", fields: [heading, { key: "body", kind: "textarea", label: "Text", max: 4000 }, { key: "imageId", kind: "image", label: "Photo" }, button] },
quote: { name: "Quote", fields: [{ key: "quote", kind: "textarea", label: "Quote", max: 600 }, { key: "by", kind: "text", label: "Who said it", max: 120 }] },
cta: { name: "Call to action", fields: [heading, { key: "text", kind: "textarea", label: "Text", max: 400 }, button] },
};
type Data = Record<string, unknown>;
/**
* A section's or block's fields. With `frame` (the top-level form of a section), its photos show in
* the shape of their frame on the site and can be moved and zoomed (saved in `data.framing`).
*/
export function DataForm({ fields, value, onChange, errors, prefix = "", frame }: { fields: Spec[]; value: Data; onChange: (v: Data) => void; errors: Record<string, string>; prefix?: string; frame?: Framing["frame"] }) {
const set = (k: string, v: unknown) => onChange({ ...value, [k]: v });
const form = (
<div className="space-y-4">
{fields.map((f) => {
const err = errors[`${prefix}${f.key}`];
const v = value[f.key];
switch (f.kind) {
case "text":
return <Field key={f.key} label={f.label} hint={f.hint} error={err}><input className={inputClass} value={(v as string) ?? ""} maxLength={f.max} onChange={(e) => set(f.key, e.target.value || null)} /></Field>;
case "textarea":
return <Field key={f.key} label={f.label} hint={f.hint} error={err}><textarea className={inputClass} rows={f.key === "body" ? 6 : 3} value={(v as string) ?? ""} maxLength={f.max} onChange={(e) => set(f.key, e.target.value || (f.key === "body" ? "" : null))} /></Field>;
case "number":
return <Field key={f.key} label={f.label} error={err}><input type="number" className={`${inputClass} w-24`} min={f.min} max={f.max} value={(v as number) ?? f.min} onChange={(e) => set(f.key, Number(e.target.value))} /></Field>;
case "image":
return <div key={f.key}><ImagePicker label={f.label} slot={{ key: f.key }} value={(v as string) ?? null} onChange={(id) => set(f.key, id)} />{err && <p className="text-xs text-red-700">{err}</p>}</div>;
case "images": {
const list = Array.isArray(v) ? (v as string[]) : [];
return (
<div key={f.key} className="space-y-2">
<p className="text-sm font-medium text-gray-800">{f.label}</p>
{list.map((id, i) => <ImagePicker key={i} label={`Photo ${i + 1}`} slot={{ key: f.key, index: i }} value={id} onChange={(n) => set(f.key, n ? list.map((x, j) => (j === i ? n : x)) : list.filter((_, j) => j !== i))} />)}
{list.length < f.max && <ImagePicker label={`Photo ${list.length + 1}`} slot={{ key: f.key, index: list.length }} value={null} onChange={(n) => n && set(f.key, [...list, n])} />}
</div>
);
}
case "button": {
const b = v as { label: string; href: string } | null;
return (
<fieldset key={f.key} className="rounded border border-gray-200 p-3">
<legend className="px-1 text-sm font-medium text-gray-800">{f.label}</legend>
{b ? (
<div className="grid gap-3 sm:grid-cols-2">
<Field label="Says" error={errors[`${prefix}${f.key}.label`]}><input className={inputClass} value={b.label} maxLength={60} onChange={(e) => set(f.key, { ...b, label: e.target.value })} /></Field>
<Field label="Goes to" hint="A page here (/visit) or https://…" error={errors[`${prefix}${f.key}.href`]}><input className={inputClass} value={b.href} onChange={(e) => set(f.key, { ...b, href: e.target.value })} /></Field>
<Btn kind="link" onClick={() => set(f.key, null)}>No button</Btn>
</div>
) : <Btn onClick={() => set(f.key, { label: "Plan your visit", href: "/visit" })}>Add a button</Btn>}
</fieldset>
);
}
case "group": {
const g = v as Data | null;
return (
<fieldset key={f.key} className="rounded border border-gray-200 p-3">
<legend className="px-1 text-sm font-medium text-gray-800">{f.label}</legend>
{g ? (
<>
<DataForm fields={f.fields} value={g} onChange={(n) => set(f.key, n)} errors={errors} prefix={`${prefix}${f.key}.`} />
<Btn kind="link" className="mt-3" onClick={() => set(f.key, null)}>Remove</Btn>
</>
) : <Btn onClick={() => set(f.key, { name: "", role: null, quote: null, photoId: null })}>Add</Btn>}
</fieldset>
);
}
}
})}
</div>
);
if (!frame) return form;
const framing = (value.framing ?? {}) as Record<string, Focus>;
const ctx: Framing = {
get: (id) => framing[id],
set: (id, f) => {
const next = { ...framing };
if (f) next[id] = f; else delete next[id];
onChange({ ...value, framing: next });
},
frame,
};
return <FramingContext.Provider value={ctx}>{form}</FramingContext.Provider>;
}