diff --git a/server/src/admin-schema/organizations.js b/server/src/admin-schema/organizations.js index bfc531d..f48afd3 100644 --- a/server/src/admin-schema/organizations.js +++ b/server/src/admin-schema/organizations.js @@ -1,4 +1,4 @@ -import { text, int, real, bool, enumeration, placeColumns, timelineExtension, linksChild, blocksChild } from "./columns.js"; +import { text, int, real, bool, enumeration, placeColumns, timelineExtension, linksChild, blocksChild, featuredCardsChild } from "./columns.js"; import { tagsChild } from "./tags.js"; /* ── Organizations ───────────────────────────────────────────── */ @@ -86,6 +86,9 @@ export const organizations = { text("phone"), ], }, + // A region's own featured cards (072), in its row of the region + // list and on its page. + { ...featuredCardsChild("organization"), when: { column: "kind", value: "region" } }, { key: "region_areas", table: "region_areas", diff --git a/server/src/migrations/072_region_featured_cards.sql b/server/src/migrations/072_region_featured_cards.sql new file mode 100644 index 0000000..3dfc505 --- /dev/null +++ b/server/src/migrations/072_region_featured_cards.sql @@ -0,0 +1,111 @@ +-- ═══════════════════════════════════════════════════════════════ +-- 072 REGION FEATURED CARDS +-- +-- A region gets featured cards of its own, the same cards the front +-- page and the Community blog own, shown in its row of the region +-- list and on its page. featured_cards takes a third owner kind, +-- 'organization'. The CHECK can't change in place, so the table is +-- rebuilt, then its index and triggers are made again. +-- +-- An organization's own cards go when it does +-- (organizations_owned_featured_cleanup), and an insert has to name +-- an organization that exists (featured_cards_owner_exists). +-- ═══════════════════════════════════════════════════════════════ + +CREATE TABLE featured_cards_new ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + owner_kind TEXT NOT NULL CHECK (owner_kind IN ('front_page', 'community_page', 'organization')), + owner_id TEXT NOT NULL, + sort_order INTEGER NOT NULL DEFAULT 0, + + kind TEXT NOT NULL DEFAULT 'link' + CHECK (kind IN ('record', 'link', 'video')), + + ref_kind TEXT CHECK (ref_kind IN ('event', 'organization', 'person', 'team', 'award')), + ref_id TEXT, + url TEXT, + + eyebrow TEXT, + title TEXT, + blurb TEXT, + image TEXT, + + CHECK ((ref_kind IS NULL) = (ref_id IS NULL)), + CONSTRAINT "A record card needs a record to point at." + CHECK (kind != 'record' OR ref_kind IS NOT NULL), + CONSTRAINT "A link or video card needs a URL." + CHECK (kind = 'record' OR url IS NOT NULL) +) STRICT; + +INSERT INTO featured_cards_new + (id, owner_kind, owner_id, sort_order, kind, ref_kind, ref_id, url, eyebrow, title, blurb, image) +SELECT id, owner_kind, owner_id, sort_order, kind, ref_kind, ref_id, url, eyebrow, title, blurb, image + FROM featured_cards; + +-- The cleanup triggers on events, people, teams, awards and +-- organizations name featured_cards; legacy mode leaves them alone +-- through the rename. +PRAGMA legacy_alter_table = ON; +DROP TABLE featured_cards; +ALTER TABLE featured_cards_new RENAME TO featured_cards; +PRAGMA legacy_alter_table = OFF; + +CREATE INDEX featured_cards_owner_idx ON featured_cards (owner_kind, owner_id, sort_order); + +CREATE TRIGGER organizations_owned_featured_cleanup +AFTER DELETE ON organizations +BEGIN + DELETE FROM featured_cards WHERE owner_kind = 'organization' AND owner_id = old.id; +END; + +CREATE TRIGGER featured_cards_owner_exists +BEFORE INSERT ON featured_cards +WHEN new.owner_kind = 'organization' +BEGIN + SELECT RAISE(ABORT, 'featured_cards: no such organization') + WHERE NOT EXISTS (SELECT 1 FROM organizations WHERE id = new.owner_id); +END; + +CREATE TRIGGER featured_cards_ref_exists +BEFORE INSERT ON featured_cards +BEGIN + SELECT CASE + WHEN new.ref_kind = 'event' + AND NOT EXISTS (SELECT 1 FROM events WHERE id = new.ref_id) + THEN RAISE(ABORT, 'featured_cards: no such event') + WHEN new.ref_kind = 'organization' + AND NOT EXISTS (SELECT 1 FROM organizations WHERE id = new.ref_id) + THEN RAISE(ABORT, 'featured_cards: no such organization') + WHEN new.ref_kind = 'person' + AND NOT EXISTS (SELECT 1 FROM people WHERE id = new.ref_id) + THEN RAISE(ABORT, 'featured_cards: no such person') + WHEN new.ref_kind = 'team' + AND NOT EXISTS (SELECT 1 FROM teams WHERE id = new.ref_id) + THEN RAISE(ABORT, 'featured_cards: no such team') + WHEN new.ref_kind = 'award' + AND NOT EXISTS (SELECT 1 FROM awards WHERE id = new.ref_id) + THEN RAISE(ABORT, 'featured_cards: no such award') + END; +END; + +CREATE TRIGGER featured_cards_ref_exists_update +BEFORE UPDATE OF ref_kind, ref_id ON featured_cards +BEGIN + SELECT CASE + WHEN new.ref_kind = 'event' + AND NOT EXISTS (SELECT 1 FROM events WHERE id = new.ref_id) + THEN RAISE(ABORT, 'featured_cards: no such event') + WHEN new.ref_kind = 'organization' + AND NOT EXISTS (SELECT 1 FROM organizations WHERE id = new.ref_id) + THEN RAISE(ABORT, 'featured_cards: no such organization') + WHEN new.ref_kind = 'person' + AND NOT EXISTS (SELECT 1 FROM people WHERE id = new.ref_id) + THEN RAISE(ABORT, 'featured_cards: no such person') + WHEN new.ref_kind = 'team' + AND NOT EXISTS (SELECT 1 FROM teams WHERE id = new.ref_id) + THEN RAISE(ABORT, 'featured_cards: no such team') + WHEN new.ref_kind = 'award' + AND NOT EXISTS (SELECT 1 FROM awards WHERE id = new.ref_id) + THEN RAISE(ABORT, 'featured_cards: no such award') + END; +END; diff --git a/server/src/routes/content.js b/server/src/routes/content.js index 1bcc307..f95a4bb 100644 --- a/server/src/routes/content.js +++ b/server/src/routes/content.js @@ -56,6 +56,7 @@ import { asBool, json, loadBlocks, + loadFeaturedCardsFor, loadLinks, loadOwned, marks, @@ -286,6 +287,9 @@ function attachRegionDetails(db, orgs) { ) .all(...ids); + // Its own featured cards (072), for its row and its page. + const featuredBy = loadFeaturedCardsFor(db, "organization", ids); + const byId = Object.fromEntries(rows.map((r) => [r.id, r])); const areasBy = new Map(); const childrenBy = new Map(); @@ -321,6 +325,7 @@ function attachRegionDetails(db, orgs) { map_note: byId[org.id]?.map_note ?? null, areas: areasBy.get(org.id) ?? [], groups: childrenBy.get(org.id) ?? [], + featured: featuredBy.get(org.id) ?? [], }; } } diff --git a/server/src/shape.js b/server/src/shape.js index e7d617a..762f9d5 100644 --- a/server/src/shape.js +++ b/server/src/shape.js @@ -226,7 +226,7 @@ export function shapeSeries(row) { } /* ── Featured cards ──────────────────────────────────────────── - One owner's cards (the front page's, today), in order, with + 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. @@ -285,13 +285,22 @@ const FEATURED_REFS = { }; 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 = ? - ORDER BY sort_order, id`, + WHERE owner_kind = ? AND owner_id IN (${placeholders(ownerIds.length)}) + ORDER BY owner_id, sort_order, id`, ) - .all(ownerKind, ownerId); + .all(ownerKind, ...ownerIds); // kind → id → record row const records = new Map(); @@ -306,8 +315,9 @@ export function loadFeaturedCards(db, ownerKind, ownerId) { const own = (row) => (row.image ? { file: row.image, kind: "featured" } : null); - const cards = []; 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 @@ -332,7 +342,7 @@ export function loadFeaturedCards(db, ownerKind, ownerId) { }); } } - return cards; + return byOwner; } export { asBool }; diff --git a/src/components/FeaturedCards.tsx b/src/components/FeaturedCards.tsx index 6b23755..da018ea 100644 --- a/src/components/FeaturedCards.tsx +++ b/src/components/FeaturedCards.tsx @@ -2,7 +2,8 @@ FEATURED CARDS A grid of cards that point somewhere: the front page's featured - band now, the Community blog's posts next. The data comes from + band, the Community blog's posts, a region's own cards (in the + region list and on its page). The data comes from loadFeaturedCards in server/src/shape.js, already resolved, so this only draws. @@ -85,9 +86,9 @@ function VideoPicture({ card }: { card: FeaturedCard }) { const RULE = 'border-brand' -function Card({ card, large, level }: { card: FeaturedCard; large: boolean; level: 3 | 4 }) { +function Card({ card, large, level }: { card: FeaturedCard; large: boolean; level: 3 | 4 | 5 }) { const href = hrefFor(card) - const Title = level === 4 ? 'h4' : 'h3' + const Title = `h${level}` as const const text = (