- The colour defaults have one source, server/src/themeDefaults.js, read by the server and the admin; the build stops if src/index.css disagrees with it - server/src/admin-schema.js split into admin-schema/, one file per entity plus columns.js (the registry checked identical) - loadOwned() in shape.js replaces the same three loads on four detail routes (every public route checked identical); one NO_STORE - One OrgLogo component for the org lists and the chapter map; an organization's own page now shows the same initials as the lists - One localISO() for the viewer's calendar day, in place of three - Boot refuses a descriptor dropdown value the table's CHECK doesn't allow, instead of it failing on save - Deleted the unused Other.tsx and white-background logo; chapters.ts is useCommunity.ts; isSuper() in the Panel; stale comments and docs corrected Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
324 lines
13 KiB
JavaScript
324 lines
13 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.
|
|
|
|
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
|
|
FROM content_blocks
|
|
WHERE owner_kind = ? AND slot = ? 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,
|
|
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,
|
|
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,
|
|
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; put it in the body slot instead.
|
|
───────────────────────────────────────────────────────────── */
|
|
|
|
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.
|
|
───────────────────────────────────────────────────────────── */
|
|
|
|
export function splitLinks(links = []) {
|
|
return {
|
|
actions: links.filter((link) => link.kind === "action"),
|
|
socials: links.filter((link) => link.kind === "social"),
|
|
website: links.find((link) => link.kind === "website")?.url ?? null,
|
|
email: links.find((link) => link.kind === "email")?.label ?? 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, today), 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-background' and 'featured' (the
|
|
card's own image). 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,
|
|
card_image, event_logo
|
|
FROM events WHERE is_published = 1 AND id IN (${marks})`,
|
|
image: (row) =>
|
|
row.card_image
|
|
? { file: row.card_image, kind: "event-background" }
|
|
: 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) {
|
|
const rows = db
|
|
.prepare(
|
|
`SELECT * FROM featured_cards
|
|
WHERE owner_kind = ? AND owner_id = ?
|
|
ORDER BY sort_order, id`,
|
|
)
|
|
.all(ownerKind, ownerId);
|
|
|
|
// 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);
|
|
|
|
const cards = [];
|
|
for (const row of rows) {
|
|
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),
|
|
});
|
|
} 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),
|
|
});
|
|
}
|
|
}
|
|
return cards;
|
|
}
|
|
|
|
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(",");
|