The demo church (and the eight demo profiles) used one generated "hills and sun" SVG for every picture. Now 104 Unsplash stock photos (models; free under the Unsplash License, credited in backend/demo-photos/CREDITS.md): a set per profile, a portrait for every leader (before: only the pastor), a picture for each event, and a photo for each message instead of the stand-in video's thumbnail. Demo photos go through processImage like an upload, made once per machine and copied; without demo-photos/ the illustration still stands in. A message's thumbnailUrl that is one of the site's own uploads now comes before the YouTube thumbnail; admins and the YouTube feed store YouTube's address there, so a church's own messages are unchanged. Faster on phones: the Community collage hero, the collage row, the team's portraits and the ministry cards give the browser their real width (sizes), so a phone no longer fetches the full-width photo for a small frame. Checked: backend tests (131), Website e2e (130, baselines retaken; Lighthouse mobile 90-98 performance, 100 accessibility and SEO), typecheck, lint and both builds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
190 lines
9.6 KiB
TypeScript
190 lines
9.6 KiB
TypeScript
/**
|
|
* Everything the public site needs to draw a page (WEBSITE-1.0-PLAN §1): the latest published
|
|
* snapshot, the church profile from Accounts, the colours made safe, the fonts and corners, and
|
|
* the messages and events (which publish on their own, so they're read live). Before the first
|
|
* publish: Coming soon, from the profile alone (§5).
|
|
*/
|
|
import { TEMPLATE_FONT_PAIRINGS, type Church, type CrosscodeEvent, type WebsiteTemplate } from '@crosscode/contracts';
|
|
import type { Ctx } from '../context.js';
|
|
import { churchProfile } from '../church/profile.js';
|
|
import { buildPalette, type Palette } from './palette.js';
|
|
import { buildSnapshot, imageOf, mediaUrl, publishedSnapshot, type Image, type Snapshot } from './snapshot.js';
|
|
import { headerIsTransparent, TEMPLATE_STYLE, type Variant } from './variants.js';
|
|
|
|
export type SiteEvent = {
|
|
title: string;
|
|
summary: string | null;
|
|
startsAt: string;
|
|
endsAt: string | null;
|
|
allDay: boolean;
|
|
location: string | null;
|
|
image: Image | null;
|
|
/** The event's page: on this site, or at its origin (Cross Events, for registering). */
|
|
url: string;
|
|
/** The Website's own events: their page here is /events/<slug> (`url` may be their outside link). */
|
|
slug: string | null;
|
|
/** An outside link for more information or signing up (the Website's own events). */
|
|
link: string | null;
|
|
from: 'website' | 'events' | 'app';
|
|
registration: { status: string; priceFromCents: number | null } | null;
|
|
};
|
|
|
|
export type SiteMessage = {
|
|
slug: string;
|
|
title: string;
|
|
date: string;
|
|
speaker: string | null;
|
|
series: string | null;
|
|
passage: string | null;
|
|
source: 'youtube' | 'spotify';
|
|
/** For the embed (youtube-nocookie) or the Spotify player, loaded only when clicked. */
|
|
embedUrl: string | null;
|
|
url: string;
|
|
thumbnail: string | null;
|
|
};
|
|
|
|
type Common = {
|
|
church: Church | null;
|
|
palette: Palette;
|
|
/** The shared brand style, else what the template suggests (§3). */
|
|
style: { fontPairing: string; corners: string };
|
|
/** Search engines' verification codes, for the home page's meta tags (§11). */
|
|
verification: { google: string | null; bing: string | null };
|
|
/** 1.2.0: the site's own icon (Settings), else null: the church's icon, else one made for it. */
|
|
favicon: string | null;
|
|
};
|
|
|
|
export type SiteContent =
|
|
| (Common & { mode: 'coming-soon'; template: WebsiteTemplate; comingSoonText: string })
|
|
| (Common & {
|
|
mode: 'published';
|
|
version: number;
|
|
/** Without its `source` (the working copy's rows): visitors never get internal ids or hidden sections. */
|
|
snapshot: Omit<Snapshot, 'source'>;
|
|
/** Per page path: whether the header sits over the hero (Bold's transparent header, §3). */
|
|
transparentHeader: Record<string, boolean>;
|
|
messages: SiteMessage[];
|
|
events: SiteEvent[];
|
|
});
|
|
|
|
const DEFAULT_COLOUR = '#1f4e79';
|
|
|
|
function brandFor(church: Church | null, template: WebsiteTemplate) {
|
|
return {
|
|
palette: buildPalette(church?.branding.colours.primary ?? DEFAULT_COLOUR, church?.branding.colours.accent ?? null),
|
|
style: {
|
|
fontPairing: church?.branding.style?.fontPairing ?? TEMPLATE_FONT_PAIRINGS[template][0],
|
|
corners: church?.branding.style?.corners ?? TEMPLATE_STYLE[template].suggestedCorners,
|
|
},
|
|
};
|
|
}
|
|
|
|
/** Where the site serves a YouTube video's thumbnail from (site/thumbnails.ts): visitors never ask YouTube. */
|
|
export const thumbnailPath = (videoId: string) => `/api/thumbnails/${videoId}.jpg`;
|
|
|
|
/**
|
|
* A message's thumbnail on the site: its YouTube video's, served from here; none for Spotify. A
|
|
* `thumbnailUrl` that is one of the site's own uploads comes first: the demo church's messages
|
|
* have photos, while their video is a stand-in (admins and the YouTube feed store YouTube's
|
|
* address there, which is never used).
|
|
*/
|
|
export function messageThumbnail(m: { source: string; mediaUrl: string; thumbnailUrl?: string | null }) {
|
|
if (m.thumbnailUrl && /^\/api\/uploads\/[\w./-]+$/.test(m.thumbnailUrl) && !m.thumbnailUrl.includes('..')) return m.thumbnailUrl;
|
|
const yt = m.source === 'spotify' ? null : youtubeId(m.mediaUrl);
|
|
return yt ? thumbnailPath(yt) : null;
|
|
}
|
|
|
|
/** `https://www.youtube.com/watch?v=abc` → its id; null for anything else. */
|
|
export function youtubeId(url: string): string | null {
|
|
try {
|
|
const u = new URL(url);
|
|
if (u.hostname === 'youtu.be') return u.pathname.slice(1) || null;
|
|
if (/(^|\.)youtube(-nocookie)?\.com$/.test(u.hostname)) return u.searchParams.get('v') ?? (/^\/(embed|shorts|live)\/([\w-]+)/.exec(u.pathname)?.[2] ?? null);
|
|
} catch {
|
|
// Not a URL.
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/** `https://open.spotify.com/episode/abc` → the embed address; null for anything else. */
|
|
export function spotifyEmbed(url: string): string | null {
|
|
const m = /^https:\/\/open\.spotify\.com\/(episode|show)\/([A-Za-z0-9]+)/.exec(url);
|
|
return m ? `https://open.spotify.com/embed/${m[1]}/${m[2]}` : null;
|
|
}
|
|
|
|
async function liveMessages(ctx: Ctx): Promise<SiteMessage[]> {
|
|
const rows = await ctx.db.message.findMany({ where: { hidden: false, publishedAt: { not: null } }, orderBy: { date: 'desc' }, take: 24, include: { series: true } });
|
|
return rows.map((m) => {
|
|
const yt = m.source === 'spotify' ? null : youtubeId(m.mediaUrl);
|
|
return {
|
|
slug: m.slug, title: m.title, date: m.date.toISOString(), speaker: m.speaker, series: m.series?.title ?? null, passage: m.passage,
|
|
source: m.source === 'spotify' ? 'spotify' : 'youtube',
|
|
embedUrl: yt ? `https://www.youtube-nocookie.com/embed/${yt}?autoplay=1` : spotifyEmbed(m.mediaUrl),
|
|
url: m.mediaUrl,
|
|
thumbnail: messageThumbnail(m),
|
|
};
|
|
});
|
|
}
|
|
|
|
async function liveEvents(ctx: Ctx): Promise<SiteEvent[]> {
|
|
const now = ctx.now();
|
|
const [simple, imported, media] = await Promise.all([
|
|
ctx.db.simpleEvent.findMany({ where: { publishedAt: { not: null }, cancelled: false, OR: [{ startsAt: { gte: now } }, { endsAt: { gte: now } }] }, orderBy: { startsAt: 'asc' }, take: 50 }),
|
|
ctx.db.importedEvent.findMany({ where: { startsAt: { gte: new Date(now.getTime() - 7 * 864e5) } }, orderBy: { startsAt: 'asc' }, take: 50 }),
|
|
ctx.db.mediaAsset.findMany(),
|
|
]);
|
|
const image = (id: string | null) => {
|
|
const m = id ? media.find((x) => x.id === id) : null;
|
|
return m ? imageOf(m) : null;
|
|
};
|
|
const own: SiteEvent[] = simple.map((e) => ({
|
|
title: e.title, summary: e.description ? e.description.slice(0, 200) : null, startsAt: e.startsAt.toISOString(), endsAt: e.endsAt?.toISOString() ?? null,
|
|
allDay: e.allDay, location: e.location, image: image(e.imageId), url: e.link || `/events/${e.slug}`, slug: e.slug, link: e.link, from: 'website', registration: null,
|
|
}));
|
|
const copies: SiteEvent[] = imported.flatMap((row) => {
|
|
const e = row.data as unknown as CrosscodeEvent;
|
|
if (!e?.title || (e.endsAt ? new Date(e.endsAt) < now : new Date(e.startsAt) < now)) return [];
|
|
return [{
|
|
title: e.title, summary: e.summary ?? null, startsAt: e.startsAt, endsAt: e.endsAt ?? null, allDay: !!e.allDay, location: e.location?.name ?? null,
|
|
image: e.imageUrl ? { url: e.imageUrl, alt: e.title, width: null, height: null } : null, url: e.origin.url, slug: null, link: null, from: e.origin.product,
|
|
registration: e.registration ? { status: e.registration.status, priceFromCents: e.registration.priceFrom?.amountCents ?? null } : null,
|
|
} satisfies SiteEvent];
|
|
});
|
|
return [...own, ...copies].sort((a, b) => a.startsAt.localeCompare(b.startsAt));
|
|
}
|
|
|
|
/** What visitors see: the latest published version, else Coming soon. */
|
|
export async function siteContent(ctx: Ctx): Promise<SiteContent> {
|
|
const [church, published] = await Promise.all([churchProfile(ctx), publishedSnapshot(ctx)]);
|
|
return contentFrom(ctx, church, published);
|
|
}
|
|
|
|
/** The preview (§4): the working copy as it would look published, whatever was published before. */
|
|
export async function previewContent(ctx: Ctx, opts: { asTemplate?: WebsiteTemplate } = {}): Promise<SiteContent> {
|
|
const [church, snapshot] = await Promise.all([churchProfile(ctx), buildSnapshot(ctx, opts)]);
|
|
return contentFrom(ctx, church, { number: 0, publishedAt: ctx.now(), snapshot });
|
|
}
|
|
|
|
async function contentFrom(ctx: Ctx, church: Church | null, published: { number: number; publishedAt: Date; snapshot: Snapshot } | null): Promise<SiteContent> {
|
|
const [google, bing, faviconId] = await Promise.all([ctx.settings.get('google_verification'), ctx.settings.get('bing_verification'), ctx.settings.get('favicon_media_id')]);
|
|
const verification = { google: google || null, bing: bing || null };
|
|
const icon = faviconId ? await ctx.db.mediaAsset.findUnique({ where: { id: faviconId } }) : null;
|
|
const favicon = icon ? mediaUrl(icon.filename) : null;
|
|
if (!published) {
|
|
const design = await ctx.db.siteDesign.findUnique({ where: { id: 1 }, select: { template: true } });
|
|
const template = (design?.template ?? 'heritage') as WebsiteTemplate;
|
|
return {
|
|
mode: 'coming-soon', template, church, ...brandFor(church, template), verification, favicon,
|
|
comingSoonText: (await ctx.settings.get('coming_soon_text')) || 'Our new website is on its way.',
|
|
};
|
|
}
|
|
const { source: _source, ...snapshot } = published.snapshot;
|
|
const template = snapshot.design.template;
|
|
const transparentHeader = Object.fromEntries(snapshot.pages.map((p) => {
|
|
const hero = p.sections[0]?.type === 'hero' ? (p.sections[0].variant as Variant<'hero'>) : null;
|
|
return [p.path, headerIsTransparent(template, snapshot.design.style.header, hero)];
|
|
}));
|
|
const [messages, events] = await Promise.all([liveMessages(ctx), liveEvents(ctx)]);
|
|
return { mode: 'published', version: published.number, snapshot, transparentHeader, church, ...brandFor(church, template), verification, favicon, messages, events };
|
|
}
|