/** * What an admin fills in for each homepage section (WEBSITE-1.0-PLAN §2: "every section stores its * data, validated with a zod schema per section type"). Never tied to a template: every template * draws every variant from the same data (§3). Lists (ministries, leaders, events, messages) come * from their own tables; the section only holds its heading and intro. * * Images are media ids here; the published snapshot turns them into `{ url, alt }`. */ import { z } from 'zod'; import type { SectionType } from './variants.js'; const text = (max: number) => z.string().trim().max(max); const optional = (max: number) => text(max).nullable().default(null); const mediaId = z.string().uuid(); /** * How each photo sits in its frame, by media id: the point kept in view (`x`, `y`, 0 to 100 from * the left and top, as CSS `object-position`) and how far it's zoomed in (1 fills the frame). */ export const focusSchema = z.object({ x: z.number().min(0).max(100), y: z.number().min(0).max(100), zoom: z.number().min(1).max(4) }); /** A ministry's, leader's or event's one photo: how it sits in its frame, or null (centred). */ export const rowFraming = focusSchema.nullable().default(null); const framing = z.record(z.string().uuid(), focusSchema).optional(); /** Keeps framing only for the photos the section still shows (a removed photo's would hold it "in use"). */ function pruneFraming }>(d: T): T { if (!d.framing) return d; const ids = new Set(); const walk = (x: unknown): void => { if (typeof x === 'string') ids.add(x); else if (Array.isArray(x)) x.forEach(walk); else if (x && typeof x === 'object') Object.values(x).forEach(walk); }; walk({ ...d, framing: undefined }); return { ...d, framing: Object.fromEntries(Object.entries(d.framing).filter(([id]) => ids.has(id))) }; } /** A link on the site (`/visit`) or elsewhere (https only). */ const href = z.string().trim().max(500).refine((v) => /^\/(?!\/)/.test(v) || /^https:\/\//.test(v), 'A page of this site (/visit) or an https:// address'); const button = z.object({ label: text(60).min(1), href }).nullable().default(null); export const SECTION_SCHEMAS = { hero: z.object({ headline: text(120).min(1), subline: optional(300), /** `photo` and `split` use the first; `collage` up to three. */ images: z.array(mediaId).max(3).default([]), /** `video`: a short muted loop (an uploaded .mp4) shown over its still (the first image). */ videoId: mediaId.nullable().default(null), button, framing, }).transform(pruneFraming), serviceTimes: z.object({ heading: optional(80), /** E.g. "Kids church during the morning service". Times and address come from the church profile. */ note: optional(300), }), welcome: z.object({ heading: optional(120), /** The mission line, above the introduction in every variant (§3). */ mission: optional(200), body: text(4000).default(''), imageId: mediaId.nullable().default(null), pastor: z.object({ name: text(120).min(1), role: optional(80), quote: optional(400), photoId: mediaId.nullable().default(null) }).nullable().default(null), framing, }).transform(pruneFraming), ministries: z.object({ heading: optional(80), intro: optional(400) }), team: z.object({ heading: optional(80), intro: optional(400) }), events: z.object({ heading: optional(80), count: z.int().min(2).max(12).default(3) }), latestMessage: z.object({ heading: optional(80) }), invitation: z.object({ heading: text(120).min(1), text: optional(400), button, imageId: mediaId.nullable().default(null), framing }).transform(pruneFraming), } as const satisfies Record; export type SectionData = { [S in SectionType]: z.infer<(typeof SECTION_SCHEMAS)[S]> }; /** Blocks for the other pages and custom pages (§3, "Pages from blocks"); more come with the admin. */ export const BLOCK_SCHEMAS = { text: z.object({ heading: optional(120), body: text(20000) }), 'image-text': z.object({ heading: optional(120), body: text(4000).default(''), imageId: mediaId.nullable().default(null), button, framing }).transform(pruneFraming), quote: z.object({ quote: text(600).min(1), by: optional(120) }), cta: z.object({ heading: text(120).min(1), text: optional(400), button }), } as const; export type BlockType = keyof typeof BLOCK_SCHEMAS; export const isBlockType = (t: string): t is BlockType => t in BLOCK_SCHEMAS; /** A section's or block's data, checked; null when it doesn't fit (it's then left out, never shown broken). */ export function parseSectionData(type: string, data: unknown): Record | null { const schema = (SECTION_SCHEMAS as Record)[type] ?? (BLOCK_SCHEMAS as Record)[type]; if (!schema) return null; const r = schema.safeParse(data ?? {}); return r.success ? (r.data as Record) : null; }