import { useMemo, useState } from 'react' import { PlayIcon } from '@heroicons/react/24/solid' import { Banknote, Gavel, LandPlot, Landmark, Server, VideoOff, type LucideIcon } from 'lucide-react' import { INITIAL_THUMBNAIL_STATE, onThumbnailError, onThumbnailLoad, youtubeThumbnailCandidates, type ThumbnailKind, type ThumbnailState, } from '../lib/youtubeThumbnail' /** * MeetingThumbnail — the ONE place a YouTube meeting still is rendered. A 16:9 * image addressed purely by video id off the i.ytimg.com CDN (no YouTube Data * API, no quota). * * It walks the ordered candidate URLs (maxresdefault → sddefault → hqdefault), * advancing on each AND on a load whose naturalWidth <= 120: * for private / removed / processing videos YouTube answers hqdefault with * HTTP 200 and a 120x90 grey "…" box, so onError never fires (issue #502). * * Image-absent behaviour: * - While the still loads, a designed cover sits behind the . * - When every candidate fails, the cover stays and says "No video preview". * It carries only the card's own real facts (government + state, date) and * an icon for the item kind on the card's theme colour — no stock imagery * and no stand-in content (CLAUDE.md No Fabricated Data). * - No video id at all ⇒ renders NOTHING (recording-less cards stay clean). */ interface MeetingThumbnailProps { /** Bare YouTube video id, e.g. "dQw4w9WgXc8". Falsy ⇒ renders nothing. */ videoId?: string | null /** Accessible label for the still (defaults to a generic meeting-video alt). */ alt?: string /** Extra classes on the wrapper (e.g. fixed width in a row layout). */ className?: string /** Legacy short context line for the cover; used when `place` is absent. */ label?: string /** Government name and state for the cover, e.g. "Bulloch County, GA". */ place?: string | null /** Meeting / decision date: ISO yyyy-mm-dd (formatted) or a display string. */ date?: string | null /** Picks the cover icon (see inferThumbnailKind). Defaults to 'meeting'. */ kind?: ThumbnailKind /** Card theme colour (any CSS colour). Darkened for white-text contrast. */ accent?: string } const KIND_ICON: Record = { 'data-center': { icon: Server, label: 'Data center' }, zoning: { icon: LandPlot, label: 'Zoning & land use' }, budget: { icon: Banknote, label: 'Budget & money' }, decision: { icon: Gavel, label: 'Decision' }, meeting: { icon: Landmark, label: 'Public meeting' }, } const DEFAULT_ACCENT = '#1a6b6b' /** "2026-03-04" → "Mar 4, 2026"; non-ISO strings pass through unchanged. */ function formatThumbnailDate(date?: string | null): string { if (!date) return '' const iso = /^\d{4}-\d{2}-\d{2}/.exec(date) if (!iso) return date const d = new Date(`${iso[0]}T00:00:00`) if (Number.isNaN(d.getTime())) return date return d.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' }) } /** * Designed cover: the theme colour mixed heavily toward near-black, so white * text clears WCAG AA even for bright accents (Contested #e0603a mixes to * roughly rgb(135,62,40), about 7.9:1 against white). */ function ThumbnailCover({ unavailable, place, date, kind, accent, }: { unavailable: boolean place: string date: string kind: ThumbnailKind accent: string }) { const { icon: Icon, label: kindLabel } = KIND_ICON[kind] return (
{/* Kind watermark, bottom-right */} {unavailable && ( No video preview )}
{place || kindLabel} {date && ( {date} )}
) } export default function MeetingThumbnail({ videoId, alt, className = '', label, place, date, kind = 'meeting', accent = DEFAULT_ACCENT, }: MeetingThumbnailProps) { const candidates = useMemo(() => youtubeThumbnailCandidates(videoId), [videoId]) const [walk, setWalk] = useState({ ...INITIAL_THUMBNAIL_STATE, id: videoId, }) // A recycled component (new video id) restarts the candidate walk. if (walk.id !== videoId) setWalk({ ...INITIAL_THUMBNAIL_STATE, id: videoId }) // No video id ⇒ nothing to preview; stay invisible (recording-less card). if (candidates.length === 0) return null const exhausted = walk.idx >= candidates.length const { loaded } = walk return (
{(!loaded || exhausted) && ( )} {!exhausted && ( {alt { const img = e.currentTarget setWalk((s) => ({ ...s, ...onThumbnailLoad(s, img) })) }} onError={() => setWalk((s) => ({ ...s, ...onThumbnailError(s) }))} className={`relative h-full w-full object-cover transition-opacity duration-300 ${loaded ? 'opacity-100' : 'opacity-0'}`} /> )} {/* Decorative play affordance over a real still only; the parent handles the click. */} {loaded && !exhausted && ( )}
) }