Front page editor: Featured Content > Columns (2, 3 or 4 across on a wide screen) and a Size on each card (normal, wide, tall, large). Blank keeps today's layout: 3 across, the first of three or more large. Regions and the Community blog stay automatic. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
372 lines
15 KiB
JavaScript
372 lines
15 KiB
JavaScript
/* ═══════════════════════════════════════════════════════════════
|
|
SHAPE
|
|
|
|
Blocks and links are polymorphic: any organization, event, person
|
|
or team can own them. Every list endpoint therefore needs the
|
|
same move, fetch the parent rows, then fetch all their children
|
|
in one query each and stitch.
|
|
|
|
The alternative is a query per row, which at a dozen events is
|
|
invisible and at two hundred is not. Three queries is three
|
|
queries whatever the row count, so it may as well be right now.
|
|
|
|
One limit worth knowing: SQLite caps bound parameters per
|
|
statement (999 on older builds). If a list ever exceeds that,
|
|
these need chunking. Nothing here comes close.
|
|
═══════════════════════════════════════════════════════════════ */
|
|
|
|
const placeholders = (n) => Array(n).fill("?").join(",");
|
|
|
|
const asBool = (value) => value === 1;
|
|
|
|
/* ── Links ─────────────────────────────────────────────────────
|
|
Map of owner_id → links, in sort order.
|
|
───────────────────────────────────────────────────────────── */
|
|
|
|
export function loadLinks(db, ownerKind, ids) {
|
|
const out = new Map();
|
|
if (ids.length === 0) return out;
|
|
|
|
const rows = db
|
|
.prepare(
|
|
`SELECT owner_id, kind, platform, label, url, is_primary
|
|
FROM links
|
|
WHERE owner_kind = ? AND owner_id IN (${placeholders(ids.length)})
|
|
ORDER BY owner_id, sort_order`,
|
|
)
|
|
.all(ownerKind, ...ids);
|
|
|
|
for (const row of rows) {
|
|
const link = {
|
|
kind: row.kind,
|
|
platform: row.platform,
|
|
label: row.label,
|
|
url: row.url,
|
|
is_primary: asBool(row.is_primary),
|
|
};
|
|
const list = out.get(row.owner_id);
|
|
if (list) list.push(link);
|
|
else out.set(row.owner_id, [link]);
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
/* ── Blocks ────────────────────────────────────────────────────
|
|
Map of owner_id → blocks for one slot, items attached. A
|
|
"both" block (071) is in the card's and the body's.
|
|
|
|
Two queries: the blocks, then every item belonging to them.
|
|
Blocks with no children come back with an empty items array
|
|
rather than no key, so the renderer never has to check.
|
|
───────────────────────────────────────────────────────────── */
|
|
|
|
export function loadBlocks(db, ownerKind, ids, slot = "body") {
|
|
const out = new Map();
|
|
if (ids.length === 0) return out;
|
|
|
|
const blockRows = db
|
|
.prepare(
|
|
`SELECT id, owner_id, type, text, media, href, alt, cite, captions, team_id
|
|
FROM content_blocks
|
|
WHERE owner_kind = ? AND slot IN (?, 'both') AND owner_id IN (${placeholders(ids.length)})
|
|
ORDER BY owner_id, sort_order`,
|
|
)
|
|
.all(ownerKind, slot, ...ids);
|
|
|
|
if (blockRows.length === 0) return out;
|
|
|
|
const byId = new Map();
|
|
|
|
for (const row of blockRows) {
|
|
const block = {
|
|
type: row.type,
|
|
text: row.text,
|
|
media: row.media,
|
|
href: row.href,
|
|
alt: row.alt,
|
|
cite: row.cite,
|
|
captions: row.captions,
|
|
team_id: row.team_id,
|
|
items: [],
|
|
};
|
|
byId.set(row.id, block);
|
|
|
|
const list = out.get(row.owner_id);
|
|
if (list) list.push(block);
|
|
else out.set(row.owner_id, [block]);
|
|
}
|
|
|
|
const blockIds = [...byId.keys()];
|
|
|
|
const itemRows = db
|
|
.prepare(
|
|
`SELECT i.block_id, i.text, i.detail, i.url,
|
|
i.day, i.time, i.location, i.description, i.media, i.media_alt, i.map, i.map_image,
|
|
p.id AS person_id, p.display_name AS person_name,
|
|
p.photo AS person_photo, p.tagline AS person_tagline
|
|
FROM content_block_items i
|
|
LEFT JOIN people p ON p.id = i.person_id AND p.is_published = 1
|
|
WHERE i.block_id IN (${placeholders(blockIds.length)})
|
|
ORDER BY i.block_id, i.sort_order`,
|
|
)
|
|
.all(...blockIds);
|
|
|
|
// The schedule fields are null on a list or link-row item; they
|
|
// ride along anyway rather than branching on the block's type.
|
|
// An unpublished person comes back as no person.
|
|
for (const row of itemRows) {
|
|
byId.get(row.block_id)?.items.push({
|
|
text: row.text,
|
|
detail: row.detail,
|
|
url: row.url,
|
|
day: row.day,
|
|
time: row.time,
|
|
location: row.location,
|
|
description: row.description,
|
|
media: row.media,
|
|
media_alt: row.media_alt,
|
|
map: row.map,
|
|
map_image: row.map_image,
|
|
person: row.person_id
|
|
? {
|
|
id: row.person_id,
|
|
name: row.person_name,
|
|
photo: row.person_photo,
|
|
tagline: row.person_tagline,
|
|
}
|
|
: null,
|
|
});
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
/* ── Card description ──────────────────────────────────────────
|
|
The card slot is paragraphs by convention, so it collapses to
|
|
an array of strings, desc_a and desc_b become description[0]
|
|
and description[1]. A non-paragraph block in the card slot is
|
|
ignored here. Only cards show it: a record's page draws its
|
|
body blocks, "both" ones among them.
|
|
───────────────────────────────────────────────────────────── */
|
|
|
|
export function paragraphs(blocks = []) {
|
|
return blocks
|
|
.filter((block) => block.type === "paragraph" && block.text)
|
|
.map((block) => block.text);
|
|
}
|
|
|
|
/* ── Split entity links ────────────────────────────────────────
|
|
Socials are lifted out of the list because cards treat them
|
|
differently: Instagram is an icon, Register is a button. The
|
|
underlying rows are the same table.
|
|
───────────────────────────────────────────────────────────── */
|
|
|
|
/* ── Tags (070_tags.sql) ───────────────────────────────────────
|
|
Sets `tags` ([{ id, name }], in the record's order) on each of
|
|
these shaped organizations or events, in one query. */
|
|
const TAG_TABLES = { organization: ["organization_tags", "org_id"], event: ["event_tags", "event_id"] };
|
|
|
|
export function attachTags(db, kind, records) {
|
|
if (records.length === 0) return;
|
|
const [table, owner] = TAG_TABLES[kind];
|
|
const rows = db
|
|
.prepare(
|
|
`SELECT x.${owner} AS owner_id, t.id, t.name
|
|
FROM ${table} x
|
|
JOIN tags t ON t.id = x.tag_id
|
|
WHERE x.${owner} IN (${placeholders(records.length)})
|
|
ORDER BY x.sort_order, t.sort_order, t.name`,
|
|
)
|
|
.all(...records.map((record) => record.id));
|
|
for (const record of records) {
|
|
record.tags = rows.filter((row) => row.owner_id === record.id).map(({ id, name }) => ({ id, name }));
|
|
}
|
|
}
|
|
|
|
export function splitLinks(links = []) {
|
|
const websites = links.filter((link) => link.kind === "website");
|
|
// An email row's URL is the address, with or without mailto:.
|
|
const emails = links
|
|
.filter((link) => link.kind === "email")
|
|
.map((link) => ({ ...link, url: link.url.replace(/^mailto:/i, "") }));
|
|
return {
|
|
actions: links.filter((link) => link.kind === "action"),
|
|
socials: links.filter((link) => link.kind === "social"),
|
|
// Every one, for a record's own page; the first, for cards.
|
|
websites,
|
|
emails,
|
|
website: websites[0]?.url ?? null,
|
|
email: emails[0]?.url ?? null,
|
|
instagram:
|
|
links.find((link) => link.platform === "instagram")?.label ?? null,
|
|
};
|
|
}
|
|
|
|
/* ── Event series ──────────────────────────────────────────────
|
|
The repeating schedule, or null for a one-off. Weekdays
|
|
collapse from seven flags to a list of the ticked ones, Sunday
|
|
first; an empty list means "starts_on's weekday", which the
|
|
client resolves since it already holds starts_on. Occurrences
|
|
are not sent, they are derived, and the client derives them
|
|
against its own today. Shared by /events and /front-page.
|
|
───────────────────────────────────────────────────────────── */
|
|
const SERIES_WEEKDAYS = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
|
|
|
|
export function shapeSeries(row) {
|
|
if (!asBool(row.is_series)) return null;
|
|
return {
|
|
frequency: row.series_frequency,
|
|
interval: row.series_interval,
|
|
weekdays: SERIES_WEEKDAYS.filter((day) => asBool(row[`series_${day}`])),
|
|
start_time: row.series_start_time,
|
|
end_time: row.series_end_time,
|
|
count: row.series_count,
|
|
};
|
|
}
|
|
|
|
/* ── Featured cards ────────────────────────────────────────────
|
|
One owner's cards (the front page's, a region's), in order, with
|
|
each record card filled in from its record. Shared so the
|
|
Community blog's cards will come out the same shape.
|
|
|
|
A record card's own title, blurb and image override the
|
|
record's. One query per kind that is actually referenced, so a
|
|
row of six cards is at most six queries.
|
|
|
|
`image` is { file, kind }, like the timeline's logo: the record
|
|
tables keep bare filenames in different public/ directories,
|
|
and logoForKind in media.ts knows which is which. The kinds are
|
|
the ref kinds, plus 'event-photo' and 'featured' (the
|
|
card's own image). `size` is its shape in the grid (073), null
|
|
for automatic. An unpublished
|
|
record's card is dropped; an unlisted one's stays, since
|
|
featuring it is attaching it by hand.
|
|
───────────────────────────────────────────────────────────── */
|
|
|
|
const FEATURED_REFS = {
|
|
event: {
|
|
sql: (marks) =>
|
|
`SELECT id, title, tagline AS blurb, date_label AS eyebrow,
|
|
photo, event_logo
|
|
FROM events WHERE is_published = 1 AND id IN (${marks})`,
|
|
image: (row) =>
|
|
row.photo
|
|
? { file: row.photo, kind: "event-photo" }
|
|
: row.event_logo
|
|
? { file: row.event_logo, kind: "event" }
|
|
: null,
|
|
},
|
|
organization: {
|
|
sql: (marks) =>
|
|
`SELECT id, name AS title, tagline AS blurb, kind AS org_kind, logo
|
|
FROM organizations WHERE is_published = 1 AND id IN (${marks})`,
|
|
image: (row) => (row.logo ? { file: row.logo, kind: "organization" } : null),
|
|
},
|
|
person: {
|
|
sql: (marks) =>
|
|
`SELECT id, display_name AS title, tagline AS blurb, photo
|
|
FROM people WHERE is_published = 1 AND id IN (${marks})`,
|
|
image: (row) => (row.photo ? { file: row.photo, kind: "person" } : null),
|
|
},
|
|
team: {
|
|
sql: (marks) =>
|
|
`SELECT t.id, t.name AS title, t.tagline AS blurb, t.logo
|
|
FROM teams t
|
|
JOIN organizations o ON o.id = t.org_id AND o.is_published = 1
|
|
WHERE t.is_published = 1 AND t.id IN (${marks})`,
|
|
image: (row) => (row.logo ? { file: row.logo, kind: "team" } : null),
|
|
},
|
|
award: {
|
|
sql: (marks) =>
|
|
`SELECT id, name AS title, description AS blurb, logo
|
|
FROM awards WHERE is_published = 1 AND id IN (${marks})`,
|
|
image: (row) => (row.logo ? { file: row.logo, kind: "award" } : null),
|
|
},
|
|
};
|
|
|
|
export function loadFeaturedCards(db, ownerKind, ownerId) {
|
|
return loadFeaturedCardsFor(db, ownerKind, [ownerId]).get(ownerId) ?? [];
|
|
}
|
|
|
|
/* Several owners' cards at once (every region in the region list),
|
|
as owner id → cards, in the same number of queries as one owner. */
|
|
export function loadFeaturedCardsFor(db, ownerKind, ownerIds) {
|
|
const byOwner = new Map();
|
|
if (ownerIds.length === 0) return byOwner;
|
|
|
|
const rows = db
|
|
.prepare(
|
|
`SELECT * FROM featured_cards
|
|
WHERE owner_kind = ? AND owner_id IN (${placeholders(ownerIds.length)})
|
|
ORDER BY owner_id, sort_order, id`,
|
|
)
|
|
.all(ownerKind, ...ownerIds);
|
|
|
|
// kind → id → record row
|
|
const records = new Map();
|
|
for (const [kind, spec] of Object.entries(FEATURED_REFS)) {
|
|
const ids = [
|
|
...new Set(rows.filter((r) => r.kind === "record" && r.ref_kind === kind).map((r) => r.ref_id)),
|
|
];
|
|
if (ids.length === 0) continue;
|
|
const found = db.prepare(spec.sql(placeholders(ids.length))).all(...ids);
|
|
records.set(kind, new Map(found.map((r) => [r.id, r])));
|
|
}
|
|
|
|
const own = (row) => (row.image ? { file: row.image, kind: "featured" } : null);
|
|
|
|
for (const row of rows) {
|
|
let cards = byOwner.get(row.owner_id);
|
|
if (!cards) byOwner.set(row.owner_id, (cards = []));
|
|
if (row.kind === "record") {
|
|
const record = records.get(row.ref_kind)?.get(row.ref_id);
|
|
if (!record) continue; // unpublished, or its organization is
|
|
cards.push({
|
|
kind: "record",
|
|
ref: { kind: row.ref_kind, id: row.ref_id, org_kind: record.org_kind ?? null },
|
|
url: null,
|
|
eyebrow: row.eyebrow ?? record.eyebrow ?? null,
|
|
title: row.title ?? record.title,
|
|
blurb: row.blurb ?? record.blurb ?? null,
|
|
image: own(row) ?? FEATURED_REFS[row.ref_kind].image(record),
|
|
size: row.size,
|
|
});
|
|
} else {
|
|
cards.push({
|
|
kind: row.kind, // 'link' | 'video'
|
|
ref: null,
|
|
url: row.url,
|
|
eyebrow: row.eyebrow,
|
|
title: row.title,
|
|
blurb: row.blurb,
|
|
image: own(row),
|
|
size: row.size,
|
|
});
|
|
}
|
|
}
|
|
return byOwner;
|
|
}
|
|
|
|
export { asBool };
|
|
|
|
/* One record's links, card blocks and body blocks: what every detail
|
|
page sends beside the record itself. */
|
|
export function loadOwned(db, ownerKind, id) {
|
|
return {
|
|
links: loadLinks(db, ownerKind, [id]).get(id) ?? [],
|
|
cards: loadBlocks(db, ownerKind, [id], "card").get(id) ?? [],
|
|
body: loadBlocks(db, ownerKind, [id], "body").get(id) ?? [],
|
|
};
|
|
}
|
|
|
|
/* Public reads may be cached a minute, and served a little stale
|
|
while a fresh copy is fetched. json() sends a body with it. */
|
|
export const CACHE = "public, max-age=60, stale-while-revalidate=300";
|
|
export const json = (c, body) => c.json(body, 200, { "Cache-Control": CACHE });
|
|
|
|
// Admin reads and anything personal: never kept by a browser or proxy.
|
|
export const NO_STORE = { "Cache-Control": "no-store" };
|
|
|
|
// "?,?,?" for an IN (...) list of n values.
|
|
export const marks = (n) => Array(n).fill("?").join(",");
|