NGU-Web/src/components/ScheduleBlock.tsx
Zaldimmar 7885d10249 Schedule: entry types, picked times, striped rows, blocks in the presentation
Entries get a type (normal, block, break, heading) and a Featured tick.
Times are picked (start, optional end) and shown as 7:00pm or 9:00 to
10:30am; the typed time stays as the override, so existing entries look
the same. A block holds sub-items (content_block_subitems, migration 086),
each with every entry field. Rows alternate a faint tint. The presentation
gets a Blocks slide (its own seconds) before each sub-item's spotlight,
and sub-items get their own checkboxes on the setup page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 23:15:53 -06:00

467 lines
17 KiB
TypeScript

/* ═══════════════════════════════════════════════════════════════
SCHEDULE BLOCK
The 'schedule' content block (034_schedule_block.sql): entries
in one tab per day, each a row of time, title, place and person,
folding out for the rest.
Tabs are the entries' `day` values in the order they first
appear, so the admin's order is the page's. An entry with no day
joins an untitled group; a schedule of one day draws no tabs. The
tab whose name starts with today's weekday opens first ("Friday",
"Fri", "Friday, June 6"), so on the day of an event the visitor
lands on today; otherwise the first tab.
A row folds out when it has anything more to show: a
description, a graphic, a map, or a person (their photo, tagline
and a link to their page). The map is Google's key-free embed of
the place typed in, with a link out to the full map; an entry's
own map image (069) shows instead, the link out kept.
An entry's kind (086): a normal entry; a block, its sub-items
listed under it, each a row of its own with its own fold-out; a
break (a meal, free time), a quieter row with no fold-out; or a
heading across the day. A featured entry stands out in the
accent, with a star. Rows alternate a faint tint, for reading
across. The time shown is `time`, which the API has already made
from the picked times or the typed override.
On an event page a Present button opens the schedule as a
slideshow for a screen (SchedulePresent.tsx).
Tabs follow the ARIA tab pattern: arrow keys move between them.
Reading mode (readingMode.ts) gets every day as a heading, each
entry a line (time, title, place, person) with its fold-out after
it, no map frame (ReadingSchedule).
═══════════════════════════════════════════════════════════════ */
import { CaretDown, Presentation, Star } from '@phosphor-icons/react'
import { Fragment, useId, useState, type KeyboardEvent, type ReactNode } from 'react'
import { Link } from 'react-router-dom'
import { personHref } from '../lib/hrefs.ts'
import { mediaUrl, initials } from '../lib/media.ts'
import type { ContentBlockItem } from '../lib/useContent.ts'
import { Anchor, Paragraphs } from './InlineText.tsx'
import { withAlpha, accentText, readableOn } from '../lib/color.ts'
import { useSmallImage } from '../lib/useSmallImage.ts'
import { focusOnMount } from '../lib/focus.ts'
import { useReadingMode } from '../lib/readingMode.ts'
const BODY = 'var(--color-muted)'
const WEEKDAYS = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
export type Day = { name: string; entries: ContentBlockItem[] }
export function groupByDay(items: ContentBlockItem[]): Day[] {
const days: Day[] = []
for (const item of items) {
const name = item.day?.trim() ?? ''
let day = days.find((d) => d.name === name)
if (!day) {
day = { name, entries: [] }
days.push(day)
}
day.entries.push(item)
}
return days
}
/* The tab to open with: today's weekday if a tab starts with it (or
its three-letter form), else the first. */
function todayIndex(days: Day[]): number {
const today = WEEKDAYS[new Date().getDay()]
const found = days.findIndex((day) => {
const name = day.name.toLowerCase()
return name.startsWith(today) || name.startsWith(today.slice(0, 3))
})
return found >= 0 ? found : 0
}
/* An entry's own map image says what it maps. */
export const mapAlt = (entry: ContentBlockItem) => `Map: ${entry.map?.trim() || entry.location || entry.text}`
const mapEmbed = (place: string) =>
`https://maps.google.com/maps?q=${encodeURIComponent(place)}&output=embed&maptype=satellite`
const mapLink = (place: string) =>
`https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(place)}`
export function Avatar({ person, size }: { person: NonNullable<ContentBlockItem['person']>; size: string }) {
const img = useSmallImage(mediaUrl(person.photo))
return !img.failed ? (
<img src={img.src} alt="" onError={img.onError} className={`${size} shrink-0 object-cover`} />
) : (
<span
aria-hidden="true"
className={`${size} flex shrink-0 items-center justify-center bg-tint text-xs font-bold text-link`}
>
{initials(person.name)}
</span>
)
}
/* A featured entry's star, read as a word by a screen reader. */
export function FeaturedStar({ className = 'h-4 w-4' }: { className?: string }) {
return (
<>
<Star aria-hidden="true" weight="fill" className={`inline shrink-0 ${className}`} />
<span className="sr-only">Featured: </span>
</>
)
}
/* A featured row's look: a faint wash of the accent and a bar down
its left edge. */
export const featuredStyle = (accent: string) => ({
background: withAlpha(accent, 10),
boxShadow: `inset 4px 0 0 ${accent}`,
})
function Entry({ entry, accent, sub = false }: { entry: ContentBlockItem; accent: string; sub?: boolean }) {
const [open, setOpen] = useState(false)
// Google's map loads only when asked for (Privacy page).
const [showMap, setShowMap] = useState(false)
const panelId = useId()
const graphic = mediaUrl(entry.media)
const place = entry.map?.trim()
const mapImage = mediaUrl(entry.map_image)
const quiet = entry.kind === 'break'
const more = !quiet && Boolean(entry.description || graphic || place || mapImage || entry.person)
if (entry.kind === 'heading') {
return (
<li className="border-b border-brand/10 px-5 pt-6 pb-2 last:border-b-0">
<p className="text-sm font-bold uppercase tracking-wide" style={{ color: accentText(accent) }}>
{entry.text}
</p>
{entry.detail && (
<p className="mt-1 text-sm" style={{ color: BODY }}>
{entry.detail}
</p>
)}
</li>
)
}
const title = entry.url ? (
<Anchor href={entry.url} className="underline-offset-2 hover:underline" style={{ color: accentText(accent) }}>
{entry.text}
</Anchor>
) : (
entry.text
)
return (
<li
className={sub ? 'pl-4' : 'border-b border-brand/10 px-5 last:border-b-0 even:bg-tint-soft/60'}
style={entry.featured ? featuredStyle(accent) : undefined}
>
{/* On a phone the time sits on its own line above the entry,
leaving the title the full width. */}
<div
className={`flex flex-wrap gap-x-4 gap-y-1 sm:flex-nowrap ${quiet || sub ? 'py-2.5' : 'py-4'} ${
sub ? 'sm:gap-4' : 'sm:gap-6'
}`}
>
<p
className={`w-full shrink-0 pt-0.5 text-sm font-semibold ${sub ? 'sm:w-28' : 'sm:w-32'}`}
style={{ color: quiet ? BODY : accentText(accent) }}
>
{entry.time}
</p>
<div className="min-w-0 flex-1">
<p className={quiet ? 'text-muted' : 'font-semibold text-body'}>
{entry.featured && (
<span className="mr-1.5" style={{ color: accentText(accent) }}>
<FeaturedStar className="h-4 w-4 -translate-y-px" />
</span>
)}
{title}
</p>
{(entry.location || entry.person) && (
<p className="mt-1 flex flex-wrap items-center gap-x-3 gap-y-1 text-sm" style={{ color: BODY }}>
{entry.location && <span>{entry.location}</span>}
{entry.person && (
<span className="inline-flex items-center gap-2">
<Avatar person={entry.person} size="h-6 w-6" />
{entry.person.name}
</span>
)}
</p>
)}
{entry.detail && (
<p className="mt-1 text-sm" style={{ color: BODY }}>
{entry.detail}
</p>
)}
</div>
{more && (
<button
type="button"
onClick={() => setOpen((o) => !o)}
aria-expanded={open}
aria-controls={panelId}
aria-label={open ? `Less about ${entry.text}` : `More about ${entry.text}`}
className="h-10 w-10 shrink-0 btn border text-sm transition-colors hover:bg-tint"
style={{ borderColor: `${withAlpha(accent, 33)}`, color: accentText(accent) }}
>
<CaretDown
aria-hidden="true"
weight="bold"
className={`mx-auto h-4 w-4 transition-transform ${open ? 'rotate-180' : ''}`}
/>
</button>
)}
</div>
{more && open && (
<div id={panelId} className={`mb-5 space-y-5 bg-tint-soft p-5 ${sub ? 'sm:ml-32' : 'sm:ml-38'}`}>
{entry.person && (
<div className="flex items-center gap-4">
<Avatar person={entry.person} size="h-16 w-16" />
<div>
<Link
to={personHref(entry.person.id)}
className="font-semibold hover:underline"
style={{ color: accentText(accent) }}
>
{entry.person.name}
</Link>
{entry.person.tagline && (
<p className="text-sm" style={{ color: BODY }}>
{entry.person.tagline}
</p>
)}
</div>
</div>
)}
{entry.description && (
<div className="space-y-3 leading-relaxed" style={{ color: BODY }}>
<Paragraphs text={entry.description} accent={accent} />
</div>
)}
{graphic && <img src={graphic} alt={entry.media_alt ?? ""} loading="lazy" className="w-full" />}
{(place || mapImage) && (
<div>
{mapImage ? (
<img src={mapImage} alt={mapAlt(entry)} loading="lazy" className="w-full" />
) : showMap && place ? (
<iframe
ref={focusOnMount}
src={mapEmbed(place)}
title={`Map: ${place}`}
loading="lazy"
referrerPolicy="no-referrer-when-downgrade"
className="aspect-video w-full border-0"
/>
) : (
<button
type="button"
onClick={() => setShowMap(true)}
className="flex aspect-video w-full flex-col items-center justify-center gap-1 bg-tint-soft text-sm"
style={{ borderColor: `${withAlpha(accent, 33)}`, color: BODY }}
>
<span className="font-semibold">Show map</span>
<span className="text-xs">{place}, from Google Maps</span>
</button>
)}
{place && (
<a
href={mapLink(place)}
target="_blank"
rel="noreferrer"
className="mt-2 inline-block text-sm underline"
style={{ color: accentText(accent) }}
>
Open in Google Maps
</a>
)}
</div>
)}
</div>
)}
{/* A block's sub-items, under its title. */}
{entry.kind === 'block' && entry.items?.length ? (
<ul className="mb-4 sm:ml-38" style={{ borderLeft: `2px solid ${withAlpha(accent, 33)}` }}>
{entry.items.map((item, index) => (
<Entry key={index} entry={item} accent={accent} sub />
))}
</ul>
) : null}
</li>
)
}
/* One entry in reading mode, a block's sub-items in a list under it. */
function readingEntry(entry: ContentBlockItem, accent: string, key: number): ReactNode {
if (entry.kind === 'heading') {
return (
<li key={key}>
<p>
<strong>{entry.text}</strong>
</p>
{entry.detail && <p>{entry.detail}</p>}
</li>
)
}
const graphic = mediaUrl(entry.media)
const place = entry.map?.trim()
const mapImage = mediaUrl(entry.map_image)
const parts: ReactNode[] = [
entry.time,
entry.url ? <Anchor href={entry.url}>{entry.text}</Anchor> : entry.text,
entry.location,
entry.person && <Link to={personHref(entry.person.id)}>{entry.person.name}</Link>,
].filter(Boolean)
return (
<li key={key}>
<p>
{entry.featured && 'Featured: '}
{parts.map((part, j) => (
<Fragment key={j}>
{j > 0 && ' · '}
{part}
</Fragment>
))}
</p>
{entry.detail && <p>{entry.detail}</p>}
{entry.person?.tagline && <p>{entry.person.tagline}</p>}
{entry.description && <Paragraphs text={entry.description} accent={accent} />}
{graphic && <img src={graphic} alt={entry.media_alt ?? ''} />}
{mapImage && <img src={mapImage} alt={mapAlt(entry)} />}
{place && (
<p>
<a href={mapLink(place)}>Open {place} in Google Maps</a>
</p>
)}
{entry.kind === 'block' && entry.items?.length ? (
<ul>{entry.items.map((item, i) => readingEntry(item, accent, i))}</ul>
) : null}
</li>
)
}
/* Reading mode: every day, every entry and its fold-out, as text. A
day is an h4, under the page's h3 block headings (ContentBlocks). */
function ReadingSchedule({ days, intro, accent }: { days: Day[]; intro?: string | null; accent: string }) {
return (
<div>
{intro && <Paragraphs text={intro} accent={accent} />}
{days.map((day, index) => (
<div key={day.name || index}>
{days.length > 1 && <h4>{day.name || 'Schedule'}</h4>}
<ul>{day.entries.map((entry, i) => readingEntry(entry, accent, i))}</ul>
</div>
))}
</div>
)
}
export default function ScheduleBlock({
items,
intro,
accent,
presentHref,
}: {
items: ContentBlockItem[]
intro?: string | null
accent: string
/** Where the Present button goes; no button without it. */
presentHref?: string
}) {
const days = groupByDay(items)
const [active, setActive] = useState(() => todayIndex(days))
const baseId = useId()
const reading = useReadingMode()
if (days.length === 0) return null
if (reading) return <ReadingSchedule days={days} intro={intro} accent={accent} />
const current = days[Math.min(active, days.length - 1)]
// Arrows move one tab and wrap; Home and End jump to the ends.
const onKey = (e: KeyboardEvent<HTMLButtonElement>) => {
const last = days.length - 1
const next =
e.key === 'ArrowRight' ? (active + 1) % days.length
: e.key === 'ArrowLeft' ? (active + last) % days.length
: e.key === 'Home' ? 0
: e.key === 'End' ? last
: null
if (next === null) return
e.preventDefault()
setActive(next)
document.getElementById(`${baseId}-tab-${next}`)?.focus()
}
return (
<div className="space-y-4">
{intro && (
<div className="space-y-3 leading-relaxed" style={{ color: BODY }}>
<Paragraphs text={intro} accent={accent} />
</div>
)}
{presentHref && (
<div className="flex justify-end">
<Link
to={presentHref}
target="_blank"
className="btn inline-flex items-center gap-2 border px-4 py-2 text-sm transition-colors hover:bg-tint"
style={{ borderColor: `${withAlpha(accent, 33)}`, color: accentText(accent) }}
>
<Presentation aria-hidden="true" weight="bold" className="h-4 w-4" />
Present
<span className="sr-only">(opens in a new tab)</span>
</Link>
</div>
)}
{days.length > 1 && (
<div role="tablist" aria-label="Days" className="flex flex-wrap gap-2">
{days.map((day, index) => {
const selected = day === current
return (
<button
key={day.name || index}
id={`${baseId}-tab-${index}`}
type="button"
role="tab"
aria-selected={selected}
aria-controls={`${baseId}-panel`}
tabIndex={selected ? 0 : -1}
onClick={() => setActive(index)}
onKeyDown={onKey}
className="btn border px-5 py-2 text-sm transition-colors"
style={
selected
? { background: accent, borderColor: accent, color: readableOn(accent) }
: { borderColor: `${withAlpha(accent, 33)}`, color: accentText(accent) }
}
>
{day.name || 'Schedule'}
</button>
)
})}
</div>
)}
<ul
id={`${baseId}-panel`}
role={days.length > 1 ? 'tabpanel' : undefined}
aria-labelledby={days.length > 1 ? `${baseId}-tab-${days.indexOf(current)}` : undefined}
className="border border-brand/15 bg-surface"
>
{current.entries.map((entry, index) => (
<Entry key={`${current.name}-${index}`} entry={entry} accent={accent} />
))}
</ul>
</div>
)
}