NGU-Web/src/pages/sections/home/FeaturedTimelineRail.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

287 lines
10 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* ═══════════════════════════════════════════════════════════════
FEATURED TIMELINE RAIL
The history page's featured entries, sideways: oldest on the
left, so scrolling right moves forward through time. A line runs
under the cards with a dot per entry, and a year label wherever
the year changes. The last card goes to the full history.
Featured is the admin's call, timeline_entries.is_featured,
"shown large" on the history page. Nothing else is filtered
here; /history has already decided what's public.
Scrolling is native, touch, trackpad, shift-wheel, with
scroll-snap so it settles on a card. A mouse can also drag it, and
the arrow buttons step one card. A drag that moved more than a few
pixels swallows the click it ends in, so letting go over a card
doesn't open it.
Nothing featured, and the section doesn't render at all.
Reading mode (readingMode.ts) gets the entries as text, oldest
first, each a linked heading with its date and words (ReadingRail).
═══════════════════════════════════════════════════════════════ */
import { ArrowRight, CaretRight, CaretLeft } from '@phosphor-icons/react';
import { useRef, useState, type PointerEvent as ReactPointerEvent } from 'react'
import { Link } from 'react-router-dom'
import HomeLink from './HomeLink.tsx'
import { useHistory } from '../../../lib/useHistory.ts'
import { hrefFor, logoSrc } from '../../../lib/timelineRefs.ts'
import { MONTH_LABELS, type TimelineItem } from '../../../lib/timeline.ts'
import { accentText, BRAND } from '../../../lib/color.ts'
import { scrollBehavior } from '../../../lib/motion.ts';
import { useReadingMode } from '../../../lib/readingMode.ts'
const DRAG_SLOP = 6
type RailProps = { id: string; title: string; blurb?: string | null }
export default function FeaturedTimelineRail({ id, title, blurb }: RailProps) {
const { items, loading, error, reload } = useHistory()
const featured = items
.filter((item) => item.featured)
.sort((a, b) => a.date.localeCompare(b.date))
const railRef = useRef<HTMLOListElement>(null)
const drag = useRef({ x: 0, left: 0, moved: false, active: false })
const [dragging, setDragging] = useState(false)
const reading = useReadingMode()
if (!loading && !error && featured.length === 0) return null
if (reading) {
return (
<section id={id}>
<h2>{title}</h2>
{blurb && <p>{blurb}</p>}
{error ? (
<p>Couldn’t load the timeline. {error}</p>
) : loading ? (
<p>Loading the timeline…</p>
) : (
featured.map((item) => <ReadingEntry key={item.id} item={item} />)
)}
<p>
<Link to="/history">See the full history</Link>
</p>
</section>
)
}
const step = (direction: number) => {
const rail = railRef.current
const card = rail?.querySelector('li')
if (!rail || !card) return
rail.scrollBy({ left: direction * (card.clientWidth + 24), behavior: scrollBehavior() })
}
const onPointerDown = (e: ReactPointerEvent<HTMLOListElement>) => {
if (e.pointerType !== 'mouse' || !railRef.current) return
drag.current = { x: e.clientX, left: railRef.current.scrollLeft, moved: false, active: true }
}
const onPointerMove = (e: ReactPointerEvent<HTMLOListElement>) => {
const d = drag.current
if (!d.active || !railRef.current) return
const dx = e.clientX - d.x
if (!d.moved && Math.abs(dx) > DRAG_SLOP) {
d.moved = true
setDragging(true)
railRef.current.setPointerCapture(e.pointerId)
}
if (d.moved) railRef.current.scrollLeft = d.left - dx
}
const endDrag = () => {
drag.current.active = false
setDragging(false)
}
return (
<section id={id} className="overflow-hidden py-24">
<div className="mx-auto flex max-w-6xl flex-wrap items-end gap-6 px-6">
<div>
<h2 className="font-display text-4xl font-bold text-ink md:text-5xl">
{title}
</h2>
{blurb && <p className="mt-3 max-w-xl text-lg text-muted">{blurb}</p>}
</div>
{featured.length > 1 && (
<div className="ml-auto flex gap-2">
{[
[-1, <CaretLeft size={22} weight="bold" aria-hidden="true" />, 'Earlier'],
[1, <CaretRight size={22} weight="bold" aria-hidden="true" />, 'Later'],
].map(([direction, glyph, label]) => (
<button
key={label as string}
type="button"
onClick={() => step(direction as number)}
aria-label={label as string}
className="flex h-12 w-12 items-center justify-center btn border text-2xl transition-colors hover:bg-surface"
style={{ borderColor: BRAND, color: accentText(BRAND) }}
>
{glyph}
</button>
))}
</div>
)}
</div>
{error ? (
<p className="mx-auto mt-10 max-w-6xl px-6 text-danger">
Couldn’t load the timeline. {error}{' '}
<button type="button" onClick={reload} className="underline">
Try again
</button>
</p>
) : loading ? (
<div className="mx-auto mt-12 flex max-w-6xl gap-6 px-6" aria-hidden="true">
{[0, 1, 2].map((i) => (
<div key={i} className="h-72 w-80 shrink-0 animate-pulse bg-tint" />
))}
</div>
) : (
<ol
ref={railRef}
className={`hp-rail mt-12 flex gap-6 overflow-x-auto px-6 pb-4 md:px-[max(1.5rem,calc((100vw-72rem)/2+1.5rem))] ${
dragging ? 'hp-rail--dragging' : ''
}`}
onPointerDown={onPointerDown}
onPointerMove={onPointerMove}
onPointerUp={endDrag}
onPointerCancel={endDrag}
// Links and images are natively draggable, and a native
// drag cancels the pointer stream this relies on.
onDragStart={(e) => e.preventDefault()}
onClickCapture={(e) => {
if (drag.current.moved) {
e.preventDefault()
e.stopPropagation()
drag.current.moved = false
}
}}
>
{featured.map((item, index) => (
<li key={item.id} className="w-[min(19rem,80vw)] shrink-0 md:w-[22rem]">
<RailCard
item={item}
showYear={index === 0 || year(item) !== year(featured[index - 1])}
/>
</li>
))}
<li className="w-[min(19rem,80vw)] shrink-0 md:w-[22rem]">
<div className="flex h-full flex-col">
<div className="h-12" />
<Link
to="/history"
draggable={false}
className="flex flex-1 flex-col justify-center p-8 text-white transition-transform duration-300 hover:-translate-y-1"
style={{ background: BRAND }}
>
<span className="font-display text-2xl font-bold">The whole story</span>
<span className="mt-2 text-white/75">Every year, every milestone.</span>
<span className="mt-6 font-semibold">See the full history <ArrowRight size="1em" weight="bold" className="inline align-[-0.125em]" aria-hidden="true" /></span>
</Link>
</div>
</li>
</ol>
)}
</section>
)
}
const year = (item: TimelineItem) => item.date.slice(0, 4)
/* "June 2014", "2014", "12 June 2014", only as much as precision
says is true. */
function dateLabel(item: TimelineItem): string {
const [y, m, d] = item.date.split('-')
const month = m ? MONTH_LABELS[Number(m) - 1] : null
if (item.precision === 'day' && d && month) return `${Number(d)} ${month} ${y}`
if (item.precision !== 'year' && month) return `${month} ${y}`
return y
}
function ReadingEntry({ item }: { item: TimelineItem }) {
const href = hrefFor(item)
return (
<>
<h3>{href ? <HomeLink url={href}>{item.title}</HomeLink> : item.title}</h3>
<p>{[dateLabel(item), item.meta].filter(Boolean).join(', ')}</p>
{item.blurb && <p>{item.blurb}</p>}
</>
)
}
function RailCard({ item, showYear }: { item: TimelineItem; showYear: boolean }) {
const href = hrefFor(item)
const logo = logoSrc(item)
const body = (
<>
{logo && (
<img
src={logo}
alt=""
loading="lazy"
draggable={false}
className="logo-plate mb-5 h-14 w-14 object-contain"
/>
)}
<p className="text-sm font-semibold" style={{ color: accentText(BRAND) }}>
{dateLabel(item)}
</p>
<p className="mt-2 font-display text-xl font-bold leading-snug text-ink">
{item.title}
</p>
{item.meta && <p className="mt-1 text-sm text-muted">{item.meta}</p>}
{item.blurb && (
<p className="mt-3 line-clamp-4 text-sm leading-relaxed text-muted">{item.blurb}</p>
)}
{href && (
<span className="mt-auto pt-5 text-sm font-semibold" style={{ color: accentText(BRAND) }}>
Read more <ArrowRight size="1em" weight="bold" className="inline align-[-0.125em]" aria-hidden="true" />
</span>
)}
</>
)
const card =
'flex flex-1 flex-col border border-brand/15 bg-surface p-7 shadow-sm'
return (
<div className="flex h-full flex-col">
{/* The line, its dot, and the year where it changes. */}
<div className="relative mb-4 h-8">
<div className="absolute inset-x-[-1.5rem] top-1/2 h-0.5 bg-brand" />
<span
aria-hidden="true"
className="absolute left-0 top-1/2 h-3.5 w-3.5 -translate-y-1/2 rounded-full bg-brand"
/>
{showYear && (
<span
className="absolute left-6 top-1/2 -translate-y-1/2 px-3 py-0.5 font-display text-sm font-bold text-white"
style={{ background: BRAND }}
>
{year(item)}
</span>
)}
</div>
{href ? (
<HomeLink
url={href}
className={`${card} transition-all duration-300 hover:-translate-y-1 hover:shadow-xl`}
>
{body}
</HomeLink>
) : (
<div className={card}>{body}</div>
)}
</div>
)
}