NGU-Web/src/components/ScheduleBlock.tsx
Zaldimmar 07c15ce96d Schedule presentation and map images for schedule entries (migration 069)
A schedule block on an event page has a Present button, opening
/events/<id>/present in a new tab: a setup screen (days, entries,
seconds for each kind of slide) and then an endless full screen
slideshow of day overviews, entry spotlights with their person,
graphics and map images, its settings kept in the address so a
signage screen can bookmark it.

Schedule entries take a Map image (069), shown in the fold-out in place
of the Google map and as a slide in the presentation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 04:47:25 -05:00

385 lines
14 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.
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 } 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>
)
}
function Entry({ entry, accent }: { entry: ContentBlockItem; accent: string }) {
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 more = Boolean(entry.description || graphic || place || mapImage || entry.person)
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="border-b border-brand/10 last:border-b-0">
{/* 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 py-4 sm:flex-nowrap sm:gap-6">
<p className="w-full shrink-0 pt-0.5 text-sm font-semibold sm:w-32" style={{ color: accentText(accent) }}>
{entry.time}
</p>
<div className="min-w-0 flex-1">
<p className="font-semibold text-body">{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 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>
)}
</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) => {
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={i}>
<p>
{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>
)}
</li>
)
})}
</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 px-5"
>
{current.entries.map((entry, index) => (
<Entry key={`${current.name}-${index}`} entry={entry} accent={accent} />
))}
</ul>
</div>
)
}