Migration 084: site defaults for the motion (slide, flip, fade, type, mirror), direction and seconds per word in Site settings, and a hero's and a block's own. The hero's headline, section and band titles, headings, paragraphs, callouts and quotes rotate; menus, tab titles, search text and emails show the first word; less motion shows it still. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
290 lines
11 KiB
JavaScript
290 lines
11 KiB
JavaScript
/* ═══════════════════════════════════════════════════════════════
|
|
SECTION DATA
|
|
|
|
What a section type shows beyond its blocks, resolved for the page
|
|
(routes/sitePages.js, GET /site-pages/:id), from its settings
|
|
tables (083_section_settings.sql):
|
|
|
|
hero its words, buttons, backdrop, slides, and the
|
|
event its card shows (with the 'event' feature)
|
|
countdown the event it counts down to
|
|
stats its numbers, counted
|
|
connect its paths, each with its links
|
|
featured_cards its cards, record cards filled in from their
|
|
records
|
|
community_blog our posts (pinned, then newest) and community
|
|
posts (pinned cards, then its feeds' newest)
|
|
|
|
── Stats ──
|
|
A stat's source picks a query from STAT_QUERIES. Each counts
|
|
exactly what the matching public page shows: published, listed
|
|
rows, and for awards only public citations to published people.
|
|
A count that disagreed with the page it summarizes would be worse
|
|
than none. 'manual' and 'years_since' read the row's own value.
|
|
|
|
── Countdown ──
|
|
The pinned event if it is still published and not over, listed
|
|
or not; otherwise the next published, listed, non-canceled event
|
|
that hasn't ended (COALESCE(ends_on, starts_on) >= today, so a
|
|
running series still counts), narrowed by the section's scope and
|
|
type. The client works out a series' next meeting from `series`.
|
|
|
|
── Hero event ──
|
|
With the 'event' feature, the hero draws its pinned event if it's
|
|
still published and not over, else the page's countdown's event,
|
|
else the next event, so the two never disagree.
|
|
═══════════════════════════════════════════════════════════════ */
|
|
|
|
import { asBool, loadFeaturedCards, loadLinks, marks, shapeSeries, splitLinks } from "./shape.js";
|
|
import { readFeeds } from "./feeds.js";
|
|
import { firstParagraphs, loadAuthors, shapeSummary } from "./routes/blog.js";
|
|
|
|
const STAT_QUERIES = {
|
|
regions: `SELECT COUNT(*) AS n FROM organizations WHERE kind = 'region' AND is_published = 1 AND is_listed = 1`,
|
|
groups: `SELECT COUNT(*) AS n FROM organizations WHERE kind = 'group' AND is_published = 1 AND is_listed = 1`,
|
|
partners: `SELECT COUNT(*) AS n FROM organizations WHERE kind = 'partner' AND is_published = 1 AND is_listed = 1`,
|
|
events_held: `SELECT COUNT(*) AS n FROM v_events
|
|
WHERE is_published = 1 AND is_listed = 1 AND effective_status = 'past'`,
|
|
retreats_held: `SELECT COUNT(*) AS n FROM v_events
|
|
WHERE is_published = 1 AND is_listed = 1 AND effective_status = 'past'
|
|
AND event_type = 'retreat'`,
|
|
people: `SELECT COUNT(*) AS n FROM people WHERE is_published = 1 AND is_listed = 1`,
|
|
awards_given: `SELECT COUNT(*) AS n
|
|
FROM person_awards pa
|
|
JOIN v_people_public p ON p.id = pa.person_id AND p.is_published = 1
|
|
JOIN awards a ON a.id = pa.award_id AND a.is_published = 1
|
|
WHERE pa.is_public = 1`,
|
|
};
|
|
|
|
/* The number as a string, or null when there's nothing to print. */
|
|
function statValue(db, row) {
|
|
if (row.source === "manual") return row.value || null;
|
|
if (row.source === "years_since") {
|
|
const year = Number.parseInt(row.value ?? "", 10);
|
|
if (!Number.isInteger(year)) return null;
|
|
return String(Math.max(0, new Date().getFullYear() - year));
|
|
}
|
|
const sql = STAT_QUERIES[row.source];
|
|
return sql ? String(db.prepare(sql).get().n) : null;
|
|
}
|
|
|
|
function shapeCountdown(row) {
|
|
if (!row) return null;
|
|
return {
|
|
id: row.id,
|
|
title: row.title,
|
|
theme: row.theme,
|
|
starts_on: row.starts_on,
|
|
ends_on: row.ends_on,
|
|
date_label: row.date_label,
|
|
location_label: row.location_label,
|
|
is_online: asBool(row.is_online),
|
|
color: row.effective_color,
|
|
event_logo: row.event_logo,
|
|
series: shapeSeries(row),
|
|
};
|
|
}
|
|
|
|
/* The countdown's fields plus what the hero's event card also draws. */
|
|
function shapeHeroEvent(db, row) {
|
|
if (!row) return null;
|
|
const { actions } = splitLinks(loadLinks(db, "event", [row.id]).get(row.id) ?? []);
|
|
return {
|
|
...shapeCountdown(row),
|
|
tagline: row.tagline,
|
|
photo: row.photo,
|
|
photo_alt: row.photo_alt,
|
|
org_logo: row.effective_org_logo,
|
|
links: actions,
|
|
};
|
|
}
|
|
|
|
const NOT_OVER = `is_published = 1
|
|
AND effective_status != 'cancelled'
|
|
AND COALESCE(ends_on, starts_on) >= date('now')`;
|
|
|
|
const pinnedEvent = (db, id) =>
|
|
id ? db.prepare(`SELECT * FROM v_events WHERE id = ? AND ${NOT_OVER}`).get(id) ?? null : null;
|
|
|
|
/* A countdown's event: the pinned one, else the next of its scope and type. */
|
|
function countdownEvent(db, settings) {
|
|
const pinned = pinnedEvent(db, settings?.event_id);
|
|
if (pinned) return pinned;
|
|
const filters = [];
|
|
const params = [];
|
|
if (settings?.scope_id) {
|
|
filters.push("AND scope_id = ?");
|
|
params.push(settings.scope_id);
|
|
}
|
|
if (settings?.event_type) {
|
|
filters.push("AND event_type = ?");
|
|
params.push(settings.event_type);
|
|
}
|
|
return (
|
|
db
|
|
.prepare(
|
|
`SELECT * FROM v_events
|
|
WHERE ${NOT_OVER} AND is_listed = 1 ${filters.join(" ")}
|
|
ORDER BY starts_on, title
|
|
LIMIT 1`,
|
|
)
|
|
.get(...params) ?? null
|
|
);
|
|
}
|
|
|
|
const byOrder = (db, table, column, id) =>
|
|
db.prepare(`SELECT * FROM ${table} WHERE ${column} = ? ORDER BY sort_order, id`).all(id);
|
|
|
|
function heroOf(db, section, pageCountdown) {
|
|
const h = db.prepare(`SELECT * FROM section_hero WHERE section_id = ?`).get(section.id) ?? {};
|
|
const event =
|
|
h.feature === "event" ? pinnedEvent(db, h.event_id) ?? pageCountdown ?? countdownEvent(db, null) : null;
|
|
return {
|
|
backdrop: h.backdrop ?? "brand",
|
|
feature: h.feature ?? "none",
|
|
eyebrow: h.eyebrow ?? null,
|
|
headline: h.headline ?? "",
|
|
subhead: h.subhead ?? null,
|
|
primary: h.primary_label && h.primary_url ? { label: h.primary_label, url: h.primary_url } : null,
|
|
secondary: h.secondary_label && h.secondary_url ? { label: h.secondary_label, url: h.secondary_url } : null,
|
|
slide_seconds: h.slide_seconds ?? 7,
|
|
slides: byOrder(db, "section_slides", "section_id", section.id).map((row) => ({
|
|
media: row.media,
|
|
alt: row.alt,
|
|
caption: row.caption,
|
|
link_url: row.link_url,
|
|
})),
|
|
livestream: h.livestream_url ? { url: h.livestream_url, title: h.livestream_title } : null,
|
|
video: h.video_url
|
|
? { url: h.video_url, title: h.video_title, captions: h.video_captions, still: h.video_still }
|
|
: null,
|
|
event: shapeHeroEvent(db, event),
|
|
// Its rotating words' motion (084); null keeps the site's.
|
|
words: { motion: h.word_motion ?? null, direction: h.word_direction ?? null, seconds: h.word_seconds ?? null },
|
|
};
|
|
}
|
|
|
|
function pathsOf(db, section) {
|
|
const actions = db.prepare(
|
|
`SELECT label, description, url FROM section_path_actions WHERE path_id = ? ORDER BY sort_order, id`,
|
|
);
|
|
return (
|
|
byOrder(db, "section_paths", "section_id", section.id)
|
|
.map((row) => ({ label: row.label, icon: row.icon, blurb: row.blurb, actions: actions.all(row.id) }))
|
|
// A path with nothing to do is a dead tab.
|
|
.filter((path) => path.actions.length > 0)
|
|
);
|
|
}
|
|
|
|
async function blogOf(db, section) {
|
|
const page = db.prepare(`SELECT * FROM section_blog WHERE section_id = ?`).get(section.id) ?? {
|
|
internal_slots: 3,
|
|
external_slots: 3,
|
|
};
|
|
|
|
// Ours: pinned, then newest.
|
|
const pinned = db
|
|
.prepare(
|
|
`SELECT b.*
|
|
FROM section_blog_posts pp
|
|
JOIN blog_posts b ON b.id = pp.post_id AND b.is_published = 1
|
|
WHERE pp.section_id = ?
|
|
ORDER BY pp.sort_order`,
|
|
)
|
|
.all(section.id);
|
|
const room = Math.max(0, page.internal_slots - pinned.length);
|
|
const pinnedIds = pinned.map((row) => row.id);
|
|
const newest = room
|
|
? db
|
|
.prepare(
|
|
`SELECT * FROM blog_posts
|
|
WHERE is_published = 1 AND is_listed = 1
|
|
${pinnedIds.length ? `AND id NOT IN (${marks(pinnedIds.length)})` : ""}
|
|
ORDER BY published_on IS NULL, published_on DESC, created_at DESC
|
|
LIMIT ?`,
|
|
)
|
|
.all(...pinnedIds, room)
|
|
: [];
|
|
const posts = [...pinned, ...newest];
|
|
const authors = loadAuthors(db, posts.map((row) => row.id));
|
|
const first = firstParagraphs(db, posts);
|
|
|
|
// Theirs: pinned cards, then the feeds' newest.
|
|
const cards = loadFeaturedCards(db, "page_section", String(section.id));
|
|
const feedRows = db
|
|
.prepare(`SELECT url, label FROM section_feeds WHERE section_id = ? ORDER BY sort_order`)
|
|
.all(section.id);
|
|
const shown = new Set(cards.map((card) => card.url).filter(Boolean));
|
|
const spare = Math.max(0, page.external_slots - cards.length);
|
|
const fromFeeds = spare && feedRows.length ? await readFeeds(feedRows) : [];
|
|
const latest = fromFeeds
|
|
.filter((item) => !shown.has(item.url))
|
|
.slice(0, spare)
|
|
.map((item) => ({
|
|
kind: "link",
|
|
ref: null,
|
|
url: item.url,
|
|
eyebrow: item.source,
|
|
title: item.title,
|
|
blurb: item.excerpt,
|
|
image: item.image ? { file: item.image, kind: "featured" } : null,
|
|
published: item.published,
|
|
}));
|
|
|
|
return {
|
|
internal: {
|
|
title: page.internal_title,
|
|
posts: posts.map((row) => shapeSummary(row, authors.get(row.id) ?? [], first.get(row.id))),
|
|
},
|
|
external: {
|
|
title: page.external_title,
|
|
url: page.external_url,
|
|
label: page.external_label,
|
|
cards: [...cards, ...latest],
|
|
},
|
|
};
|
|
}
|
|
|
|
/* Each section with what its type shows, as `data`. A page's first
|
|
countdown is the event its heroes fall back to. */
|
|
export async function withSectionData(db, sections) {
|
|
const countdown = sections.find((s) => s.preset === "countdown");
|
|
const pageCountdown = countdown
|
|
? countdownEvent(db, db.prepare(`SELECT * FROM section_countdown WHERE section_id = ?`).get(countdown.id))
|
|
: null;
|
|
|
|
return Promise.all(
|
|
sections.map(async (section) => {
|
|
switch (section.preset) {
|
|
case "hero":
|
|
return { ...section, data: heroOf(db, section, pageCountdown) };
|
|
case "countdown": {
|
|
const settings = db.prepare(`SELECT * FROM section_countdown WHERE section_id = ?`).get(section.id);
|
|
const event = section === countdown ? pageCountdown : countdownEvent(db, settings);
|
|
return { ...section, data: shapeCountdown(event) };
|
|
}
|
|
case "stats":
|
|
return {
|
|
...section,
|
|
data: byOrder(db, "section_stats", "section_id", section.id)
|
|
.map((row) => ({ label: row.label, value: statValue(db, row), suffix: row.suffix, note: row.note }))
|
|
.filter((stat) => stat.value !== null),
|
|
};
|
|
case "connect":
|
|
return { ...section, data: pathsOf(db, section) };
|
|
case "featured_cards":
|
|
return {
|
|
...section,
|
|
data: {
|
|
cards: loadFeaturedCards(db, "page_section", String(section.id)),
|
|
columns: db.prepare(`SELECT columns FROM section_featured WHERE section_id = ?`).get(section.id)?.columns ?? null,
|
|
},
|
|
};
|
|
case "community_blog":
|
|
return { ...section, data: await blogOf(db, section) };
|
|
default:
|
|
return section;
|
|
}
|
|
}),
|
|
);
|
|
}
|