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 = (
@@ -137,10 +138,10 @@ function Card({ card, large, level }: { card: FeaturedCard; large: boolean; leve ) } -type FeaturedCardsProps = { cards: FeaturedCard[]; level?: 3 | 4 } +type FeaturedCardsProps = { cards: FeaturedCard[]; level?: 3 | 4 | 5 } function ReadingCards({ cards, level = 3 }: FeaturedCardsProps) { - const Title = level === 4 ? 'h4' : 'h3' + const Title = `h${level}` as const return ( <> {cards.map((card, index) => { diff --git a/src/lib/adminSchema/organizations.ts b/src/lib/adminSchema/organizations.ts index ec3252c..26b51da 100644 --- a/src/lib/adminSchema/organizations.ts +++ b/src/lib/adminSchema/organizations.ts @@ -1,5 +1,5 @@ import { mediaUrl } from "../media.ts"; -import { PLACE_FIELDS, PUBLISH_FIELDS, timelineGroup, timelineDetailGroup, linksChild, blocksChild, tagsChild } from "./shared.ts"; +import { PLACE_FIELDS, PUBLISH_FIELDS, timelineGroup, timelineDetailGroup, linksChild, blocksChild, tagsChild, featuredCardsChild } from "./shared.ts"; /* ── Organizations ───────────────────────────────────────────── */ @@ -90,6 +90,11 @@ export const organizations = { children: [ tagsChild, + { + ...featuredCardsChild, + when: { path: "kind", value: "region" }, + note: "Shown in this region's row of the region list (when it's opened) and on its page, in this order.", + }, { key: "region_areas", label: "Map areas", diff --git a/src/lib/useContent.ts b/src/lib/useContent.ts index c669f1a..63abdda 100644 --- a/src/lib/useContent.ts +++ b/src/lib/useContent.ts @@ -12,6 +12,7 @@ */ import type { BlogPostSummary } from './useBlog.ts' +import type { FeaturedCard } from './useFrontPage.ts' import { detailPath } from './hrefs.ts' import { useRecord, type Resource } from './useRecord.ts' import type { EventType } from './eventTypes.ts' @@ -257,6 +258,8 @@ export type OrganizationRecord = { map_note?: string | null areas?: RegionArea[] groups?: Array<{ id: string; name: string; location_label?: string | null; logo?: string | null }> + /** A region's own featured cards, in order. */ + featured?: FeaturedCard[] region_id?: string | null region_name?: string | null region_color?: string | null diff --git a/src/pages/OrganizationDetail.tsx b/src/pages/OrganizationDetail.tsx index b081bed..8618feb 100644 --- a/src/pages/OrganizationDetail.tsx +++ b/src/pages/OrganizationDetail.tsx @@ -20,6 +20,7 @@ import { Link, Navigate, useLocation, useParams } from 'react-router-dom' import PageShell from '../components/PageShell.tsx' import PageState from '../components/PageState.tsx' import ContentBlocks from '../components/ContentBlocks.tsx' +import FeaturedCards from '../components/FeaturedCards.tsx' import OrgTeams, { hasTeams } from '../components/OrgTeams.tsx' import OrgContact from '../components/OrgContact.tsx' import Tags from '../components/Tags.tsx' @@ -94,6 +95,21 @@ export default function OrganizationDetail() { }, ] + // A region's own featured cards, the same ones its row in the + // region list shows. + if (org.kind === 'region' && (org.details.featured?.length ?? 0) > 0) { + sections.push({ + id: 'featured', + title: 'Featured', + accent, + content: ( +
+ +
+ ), + }) + } + if (hasTeams(org)) { sections.push({ id: 'leadership', diff --git a/src/pages/sections/OrgList-Vertical.tsx b/src/pages/sections/OrgList-Vertical.tsx index 6394bc7..6a66aab 100644 --- a/src/pages/sections/OrgList-Vertical.tsx +++ b/src/pages/sections/OrgList-Vertical.tsx @@ -2,6 +2,7 @@ import { ArrowUpRight, CaretRight } from '@phosphor-icons/react'; import { useId, useState } from "react"; import { Link } from "react-router-dom"; import ArrowLink from "../../components/ArrowLink.tsx"; +import FeaturedCards from "../../components/FeaturedCards.tsx"; import { mediaUrl } from "../../lib/media.ts"; import Logo from "../../components/Logo.tsx"; import PlatformIcon from "../../components/PlatformIcon.tsx"; @@ -38,9 +39,9 @@ import { useReadingMode } from '../../lib/readingMode.ts' groupBy={org => org.details?.scope} /> - Kind-specific extras (the areas a region covers, its map note) - come from `details` and render only when present, so a partner - row simply doesn't have them. + Kind-specific extras (the areas a region covers, its map note, + its featured cards) come from `details` and render only when + present, so a partner row simply doesn't have them. ═══════════════════════════════════════════════════════════════ */ const MUTED = "var(--color-muted)"; @@ -100,9 +101,12 @@ function OrgRow({ org, pageLabel, siteLabel, titled }) { exactly once rather than in both places. */ const summary = org.tagline; + // A region's own featured cards, last in the panel. + const featured = org.details?.featured ?? []; + // Something has to be behind the chevron or opening it does nothing. const hasPanel = - org.description?.length > 0 || areas || note || org.links?.length > 0; + org.description?.length > 0 || areas || note || org.links?.length > 0 || featured.length > 0; return (
@@ -214,6 +218,12 @@ function OrgRow({ org, pageLabel, siteLabel, titled }) { ))}
)} + + {featured.length > 0 && ( +
+ +
+ )}