NGU-Web/src/components/FeaturedCards.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

178 lines
6.3 KiB
TypeScript

/* ═══════════════════════════════════════════════════════════════
FEATURED CARDS
A grid of cards that point somewhere: the front page's featured
band now, the Community blog's posts next. The data comes from
loadFeaturedCards in server/src/shape.js, already resolved, so
this only draws.
Three kinds of card:
record links to the record's own page (hrefs.ts builds it)
link links to its URL, a new tab when it leaves the site
video VideoPlayer: a still with a play button that swaps in
the player when pressed. The still is the card's
image, else YouTube's thumbnail, else a plain panel.
A photo fills the picture area; a logo sits inside it on a soft
panel instead, since cropping a logo to 16:9 cuts it off.
The grid is one continuous block: no gaps and no frame, only a
colored rule between cards (each card's right and bottom border,
with the grid pulled past its clipping wrapper so the outer ones
fall outside), 4px thick. With three or more cards the first is the large
one, two rows tall, and the next two stack beside it, so it no
longer stretches its neighbor.
Reading mode (readingMode.ts) gets each card as a linked heading
and its words, a video card linking to its video. `level` is that
heading's level, one under the heading above the cards.
═══════════════════════════════════════════════════════════════ */
import HomeLink from '../pages/sections/home/HomeLink.tsx'
import VideoPlayer from './VideoPlayer.tsx'
import { refHref } from '../lib/hrefs.ts'
import { mediaUrl } from '../lib/media.ts'
import type { FeaturedCard } from '../lib/useFrontPage.ts'
import { useReadingMode } from '../lib/readingMode.ts'
import { useSmallImage } from '../lib/useSmallImage.ts'
/* Image kinds that are photographs, drawn edge to edge. Anything
else is a logo, drawn whole. */
const PHOTO_KINDS = new Set(['event-photo', 'featured', 'person'])
function hrefFor(card: FeaturedCard): string | null {
if (card.kind === 'record' && card.ref) {
return refHref(card.ref.kind, card.ref.id, card.ref.org_kind)
}
return card.kind === 'link' ? card.url : null
}
function Picture({ card }: { card: FeaturedCard }) {
const img = useSmallImage(card.image ? mediaUrl(card.image.file) : null)
const photo = card.image ? PHOTO_KINDS.has(card.image.kind) : false
if (img.failed) {
return <div className="aspect-video w-full bg-tint" />
}
return photo ? (
<img
src={img.src}
alt=""
loading="lazy"
onError={img.onError}
className="aspect-video w-full object-cover"
/>
) : (
<div className="flex aspect-video w-full items-center justify-center bg-tint p-8">
<img
src={img.src}
alt=""
loading="lazy"
onError={img.onError}
className="logo-plate max-h-full max-w-full object-contain"
/>
</div>
)
}
function VideoPicture({ card }: { card: FeaturedCard }) {
const own = card.image ? mediaUrl(card.image.file) : null
return <VideoPlayer url={card.url} still={own} title={card.title} />
}
const RULE = 'border-brand'
function Card({ card, large, level }: { card: FeaturedCard; large: boolean; level: 3 | 4 }) {
const href = hrefFor(card)
const Title = level === 4 ? 'h4' : 'h3'
const text = (
<div className={`flex flex-1 flex-col ${large ? 'p-8 md:p-10' : 'p-7'}`}>
{card.eyebrow && (
<p className="text-sm font-semibold text-link">{card.eyebrow}</p>
)}
{card.title && (
<Title className={`mt-2 font-display font-bold leading-snug text-ink ${large ? 'text-2xl md:text-3xl' : 'text-lg'}`}>
{href ? (
// Stretched over the whole card, so the picture and the
// blurb are part of the click without nesting links.
<HomeLink
url={href}
className="after:absolute after:inset-0 after:content-[''] hover:underline"
>
{card.title}
</HomeLink>
) : (
card.title
)}
</Title>
)}
{card.blurb && (
<p className={`mt-2 leading-relaxed text-muted ${large ? '' : 'line-clamp-3 text-sm'}`}>{card.blurb}</p>
)}
</div>
)
return (
<li
className={`relative flex flex-col overflow-hidden border-r-4 border-b-4 ${RULE} bg-raised transition-colors hover:bg-tint-soft ${
large ? 'lg:col-span-2 lg:row-span-2' : ''
}`}
>
{card.kind === 'video' ? (
// Above the stretched link, which a video card doesn't have
// anyway, so the play button always gets the click. Inset
// from the card's edges, so the frame sits inside the cell.
<div className="relative z-10 p-4 pb-0">
<VideoPicture card={card} />
</div>
) : (
<Picture card={card} />
)}
{text}
</li>
)
}
type FeaturedCardsProps = { cards: FeaturedCard[]; level?: 3 | 4 }
function ReadingCards({ cards, level = 3 }: FeaturedCardsProps) {
const Title = level === 4 ? 'h4' : 'h3'
return (
<>
{cards.map((card, index) => {
const href = card.kind === 'video' ? card.url : hrefFor(card)
return (
<div key={`${card.kind}-${card.ref?.id ?? card.url}-${index}`}>
{card.eyebrow && <p>{card.eyebrow}</p>}
{card.title && <Title>{href ? <HomeLink url={href}>{card.title}</HomeLink> : card.title}</Title>}
{card.blurb && <p>{card.blurb}</p>}
</div>
)
})}
</>
)
}
export default function FeaturedCards({ cards, level = 3 }: FeaturedCardsProps) {
const reading = useReadingMode()
if (reading) return <ReadingCards cards={cards} level={level} />
const leadLarge = cards.length >= 3
return (
<div className="overflow-hidden shadow-md">
<ul className="-mr-[4px] -mb-[4px] grid sm:grid-cols-2 lg:grid-cols-3">
{cards.map((card, index) => (
<Card
key={`${card.kind}-${card.ref?.id ?? card.url}-${index}`}
card={card}
large={leadLarge && index === 0}
level={level}
/>
))}
</ul>
</div>
)
}