/* ═══════════════════════════════════════════════════════════════ CONTENT BLOCKS Renders the `body` slot of content_blocks. Events, organizations, teams, people and blog posts all own blocks and all render them the same way, so this is written once and tinted by the caller's accent. The admin's block editor previews each block through this same component. What each type reads (see 032_content_block_types.sql): heading, subheading text paragraph text (formatted), href makes it all a link callout text (formatted), in a tinted box quote text (formatted), cite is who said it list items: text, detail, url links items with a url, as outlined buttons button text is the label, href where it goes image media, alt, text is the caption, href video href is the video, media a still, text the caption; click to play schedule items as entries in day tabs (day, time, text, detail, url, location, person, and a fold-out of description, media, map); text is an optional intro. ScheduleBlock table text: a row per line, cells split by | cards items as link cards, up to three in a row: text the title, detail, url, media the picture people team_id's members as people tiles, then items as more tiles: person, text their title (an unpublished person is left out) divider nothing "Formatted" is InlineText.tsx: **bold**, *italic*, [text](url), line breaks, and a blank line for a new paragraph. Reading mode (readingMode.ts): a linked image with no alt text is a line linking with its caption, and a video's caption is its link (VideoPlayer), not said twice. An unknown `type` renders its text as a paragraph rather than disappearing. A block somebody typed into the admin should be visible even if the renderer hasn't caught up with it. ═══════════════════════════════════════════════════════════════ */ import { mediaUrl } from '../lib/media.ts' import PeopleTiles from './PeopleTiles.tsx' import type { ContentBlock, ContentBlockItem } from '../lib/useContent.ts' import { Anchor, Inline, Paragraphs, isExternalUrl } from './InlineText.tsx' import VideoPlayer from './VideoPlayer.tsx' import ScheduleBlock from './ScheduleBlock.tsx' import { accentText, BRAND } from '../lib/color.ts' import { headingAnchors } from '../lib/contents.ts' import { SITE_NAME } from '../lib/site.ts' import { useSmallImage } from '../lib/useSmallImage.ts' import { useReadingMode } from '../lib/readingMode.ts' import { WordMotion, Words } from './RotatingWords.tsx' const BODY = 'var(--color-muted)' export default function ContentBlocks({ blocks, accent = BRAND, className = '', presentAt, anchorPrefix = '', }: { blocks?: ContentBlock[] | null accent?: string className?: string /** Put before each heading's id: a blog post's tab (contents.ts, splitTabs). */ anchorPrefix?: string /** An event page's presentation route: each schedule gets a Present * button to it, `?block=` naming which schedule (from 0). */ presentAt?: string }) { if (!blocks?.length) return null // Headings get ids so a post's contents (PostContents) and a shared // link (#getting-there) can point at them. const { idAt } = headingAnchors(blocks, anchorPrefix) // The heading level a block's own headings take: under the last // heading (h3) or subheading (h4) before it, else the section's h2. let under = 2 const levels = blocks.map((block) => { const level = (under + 1) as 3 | 4 | 5 if (block.type === 'heading') under = 3 if (block.type === 'subheading') under = 4 return level }) let schedules = 0 const presentHrefs = blocks.map((block) => presentAt && block.type === 'schedule' ? `${presentAt}?block=${schedules++}` : undefined, ) return (
{blocks.map((block, index) => ( ))}
) } /* One link card: the whole card is the link. The site's name above the title says where it goes (the host for an outside link). */ function LinkCard({ item }: { item: ContentBlockItem }) { const url = item.url ?? '' const img = useSmallImage(mediaUrl(item.media)) const where = isExternalUrl(url) ? (() => { try { const host = new URL(url).hostname.replace(/^www\./, '') // A file from Tools → Media → Files (routes/files.js). return /^(drive|docs)\.google\.com$/.test(host) ? 'Google Drive' : host } catch { return url } })() : SITE_NAME return ( {!img.failed && ( )} {where} {item.text} {item.detail && {item.detail}} ) } /* A table block's text as rows of cells: one row per line, cells split by "|" (a literal bar is "\|"), blank lines skipped. A leading and trailing bar, as in Markdown, is allowed. */ export function tableRows(text: string): string[][] { return text .split('\n') .map((line) => line.trim()) .filter(Boolean) .map((line) => line .replace(/^\|/, '') .replace(/(? cell.replace(/\\\|/g, '|').trim()), ) } function Caption({ text }: { text?: string | null }) { if (!text) return null return (
{text}
) } function Block({ block, accent, anchor, level, presentHref, }: { block: ContentBlock accent: string anchor?: string level: 3 | 4 | 5 presentHref?: string }) { const text = block.text ?? '' const reading = useReadingMode() switch (block.type) { // A blog post's tab marker (BlogPost.tsx splits the post at // them); anywhere else, it reads as a heading. case 'tab': case 'heading': return (

) case 'subheading': return (

) case 'list': return ( ) case 'links': return (
{(block.items ?? []) .filter((item) => item.url) .map((item, index) => ( {item.text} ))}
) case 'button': if (!text || !block.href) return null return (
{text}
) case 'quote': return (
{block.cite && (
{block.cite}
)}
) case 'cards': { const cards = (block.items ?? []).filter((item) => item.url && item.text) if (cards.length === 0) return null return ( // One grid whatever the count: a lone card takes a column // rather than the width, and more sit up to three in a row. ) } case 'table': { const rows = tableRows(text) if (rows.length === 0) return null const [head, ...body] = rows const width = Math.max(...rows.map((row) => row.length)) return ( // Wide tables scroll sideways inside their box on a phone // rather than squeezing every column to a word. Focusable, so // the arrow keys scroll it too (Safari won't on its own).
{Array.from({ length: width }, (_, i) => ( ))} {body.map((row, r) => ( {Array.from({ length: width }, (_, i) => i === 0 ? ( ) : ( ), )} ))}
) } case 'callout': if (!text) return null return ( ) case 'image': { const src = mediaUrl(block.media) if (!src) return null // The reader leaves out a picture with no alt, and its link with it. if (reading && block.href && !block.alt) { return (

{text || 'Open link'}

) } const img = ( ) return (
{block.href ? {img} : img}
) } case 'video': if (!block.href) return null return (
{!reading && }
) case 'schedule': if (!block.items?.length) return null return case 'people': { const picked = (block.items ?? []) .filter((item) => item.person) .map((item) => ({ peopleslug: item.person!.id, title: item.text || null })) if (!block.team_id && picked.length === 0) return null return (
{block.team_id && } {picked.length > 0 && }
) } case 'divider': return
case 'paragraph': default: if (!text) return null return block.href ? (

{text}

) : (
) } }