NGU-Web/src/components/VideoPlayer.tsx
Zaldimmar 27e5fbd690 Reading mode: every page and section reads as text
The reader (ReaderView.tsx) now reads an icon-only link's label, puts
a card's link on its first line or picture when it has no heading,
gives each item of a row of facts or links its own line, keeps line
breaks, skips what CSS hides, turns a form into one line saying it
can't be used there, shows empty table cells as "-", keeps section and
heading ids so #links land, and opens file links as files.

What it can't read draws its own text version (useReadingMode()):
the hero, pathfinder, front page history, event lists (every event,
past ones too), org lists and groups map (every region and group,
linked), history timeline (every year open), schedules (every day,
fold-outs included), people tiles (everyone, with details) and videos.

Also: org names are linked headings, heading levels fixed where cards
repeated their section's, Team pages lose the doubled "Members",
Giving links the treasurer's email and names Church Center.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 17:12:27 -05:00

140 lines
4.8 KiB
TypeScript

/* ═══════════════════════════════════════════════════════════════
VIDEO PLAYER: click to play
A still with a play button; pressing it swaps in the player, so a
page loads no iframes until someone asks for one. Used by the
featured cards and the video content block.
The still is the caller's image, else YouTube's own thumbnail
(its 16:9 one, falling back to the 4:3 one cropped). Any still
that fails, or comes back as YouTube's gray "no thumbnail"
placeholder, gives way to the next; with none left it's a flat
teal panel with the play button always showing. The play button shows on hover or keyboard focus, and
always on a touch screen, which can't hover. Small rounded
corners, as the only rounded thing besides buttons and dots. A link that embeds.ts can't turn into a
player (not https, or not a host it knows how to frame) still
goes somewhere: the still becomes a link to it.
Reading mode (readingMode.ts) gets a link to the video (an
uploaded file's own address), named by its title.
═══════════════════════════════════════════════════════════════ */
import { Play } from '@phosphor-icons/react'
import { useState, type SyntheticEvent } from 'react'
import HomeLink from '../pages/sections/home/HomeLink.tsx'
import { playingSource, videoSource, videoThumbnails } from '../lib/embeds.ts'
import { BRAND } from '../lib/color.ts'
import { focusOnMount } from '../lib/focus.ts'
import { useReadingMode } from '../lib/readingMode.ts'
export default function VideoPlayer({
url,
still,
title,
className = '',
}: {
url?: string | null
/** An image URL to show before it plays. */
still?: string | null
title?: string | null
className?: string
}) {
const [playing, setPlaying] = useState(false)
const [skipped, setSkipped] = useState(0)
const source = videoSource(url)
const reading = useReadingMode()
if (reading) {
const href = source?.kind === 'file' ? source.src : url
return href ? (
<p>
<a href={href}>{title || 'Watch the video'}</a>
</p>
) : null
}
if (playing && source) {
const live = playingSource(source)
return live.kind === 'file' ? (
<video
ref={focusOnMount}
src={live.src}
controls
autoPlay
playsInline
aria-label={title || 'Video'}
className={`aspect-video w-full rounded-md bg-black ${className}`}
/>
) : (
<iframe
ref={focusOnMount}
src={live.src}
title={title || 'Video'}
allow="autoplay; encrypted-media; picture-in-picture; fullscreen"
allowFullScreen
className={`aspect-video w-full rounded-md border-0 bg-black ${className}`}
/>
)
}
const stills = [...(still ? [still] : []), ...videoThumbnails(url)]
const image = stills[skipped] ?? null
const next = () => setSkipped((n) => n + 1)
// YouTube's placeholder for a missing still is a real 120 x 90 image,
// not an error, so it's caught by its size; wherever it came from,
// since Fetch preview may have saved one into the library.
const checkLoaded = (event: SyntheticEvent<HTMLImageElement>) => {
const img = event.currentTarget
if (img.naturalWidth <= 120 && img.naturalHeight <= 90) next()
}
const face = (
<span className={`relative block overflow-hidden rounded-md ${className}`}>
{image ? (
<img
key={image}
src={image}
alt=""
loading="lazy"
onError={next}
onLoad={checkLoaded}
className="block aspect-video w-full object-cover"
/>
) : (
<span className="block aspect-video w-full bg-[var(--pal-teal-1)]" />
)}
<span
aria-hidden="true"
className={`video-play ${image ? '' : 'video-play--always'} absolute inset-0 flex items-center justify-center transition-colors group-hover:bg-black/20 group-focus-visible:bg-black/20`}
>
<span className="flex h-16 w-16 items-center justify-center rounded-full bg-white/95 shadow-lg transition-transform group-hover:scale-105">
<Play className="h-7 w-7" weight="fill" style={{ color: BRAND }} aria-hidden="true" />
</span>
</span>
</span>
)
if (!source) {
return url ? (
<HomeLink url={url} className="group relative block">
{face}
</HomeLink>
) : (
<div className="relative">{face}</div>
)
}
return (
<button
type="button"
onClick={() => setPlaying(true)}
aria-label={`Play ${title || 'video'}`}
className="group relative block w-full"
>
{face}
</button>
)
}