NGU-Web/src/components/ScheduleBlock.tsx
Zaldimmar f72a9604d2 Make the site faster to load and lighter on the server
- Every page but Home, and the whole admin, loads on first visit: the
  main bundle drops from 579 kB to 314 kB (173 to 98 kB gzipped); a
  tab on an old build reloads once when a page chunk has gone
- Home asks for events and the timeline alongside its own data, and
  the server preloads /api/front-page and the first hero photo
- Hero headline rises in as one piece (it held back the page's
  largest paint), sized to stay within two lines; min-h in dvh; first
  slide fetched at high priority
- Every raster upload gets a 640px copy (at upload, and at boot for
  older files); logos, avatars and card pictures load it and fall
  back to the full file
- Login password checks run off the event loop; only admin requests
  are logged per request; ffmpeg runs at nice 19 on Linux
- Page HTML is no-cache, so a deploy can't strand a cached page on
  missing assets; source maps are no longer linked
- Migration 042: index person_awards by award
- docs/performance.md, and gzip and HTTP/2 for nginx

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 02:41:45 -05:00

291 lines
10 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.
Tabs follow the ARIA tab pattern: arrow keys move between them.
═══════════════════════════════════════════════════════════════ */
import { useId, useState, type KeyboardEvent } 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'
const BODY = 'var(--color-muted)'
const WEEKDAYS = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
type Day = { name: string; entries: ContentBlockItem[] }
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
}
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)}`
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 rounded-full object-cover`} />
) : (
<span
aria-hidden="true"
className={`${size} flex shrink-0 items-center justify-center rounded-full 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 more = Boolean(entry.description || graphic || place || 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">
<div className="flex gap-4 py-4 sm:gap-6">
<p className="w-24 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-8 w-8 shrink-0 rounded-full border text-sm transition-colors hover:bg-tint"
style={{ borderColor: `${withAlpha(accent, 33)}`, color: accentText(accent) }}
>
<span aria-hidden="true" className={`inline-block transition-transform ${open ? 'rotate-180' : ''}`}>
▾
</span>
</button>
)}
</div>
{more && open && (
<div id={panelId} className="mb-5 space-y-5 rounded-2xl 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 rounded-lg" />}
{place && (
<div>
{showMap ? (
<iframe
src={mapEmbed(place)}
title={`Map: ${place}`}
loading="lazy"
referrerPolicy="no-referrer-when-downgrade"
className="aspect-video w-full rounded-lg border-0"
/>
) : (
<button
type="button"
onClick={() => setShowMap(true)}
className="flex aspect-video w-full flex-col items-center justify-center gap-1 rounded-lg border border-dashed 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>
)}
<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>
)
}
export default function ScheduleBlock({
items,
intro,
accent,
}: {
items: ContentBlockItem[]
intro?: string | null
accent: string
}) {
const days = groupByDay(items)
const [active, setActive] = useState(() => todayIndex(days))
const baseId = useId()
if (days.length === 0) return null
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>
)}
{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="rounded-full border px-5 py-2 text-sm font-semibold 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="rounded-2xl border border-brand/15 bg-surface px-5"
>
{current.entries.map((entry, index) => (
<Entry key={`${current.name}-${index}`} entry={entry} accent={accent} />
))}
</ul>
</div>
)
}