import { useCallback, useEffect, useRef, useState, type KeyboardEvent } from 'react' import { Link } from 'react-router-dom' import { Bike, Camera, ChevronLeft, ChevronRight, Crosshair, Droplets, HandHeart, House, MessagesSquare, Server, ShieldCheck, Volleyball, type LucideIcon } from 'lucide-react' import { useQueries } from '@tanstack/react-query' import TopicCompaniesPanel from './TopicCompaniesPanel' import { fetchTopicCaptionFacts } from '../../api/topics' import { useTopicStories } from '../../hooks/useTopicStories' import { docsUrl } from '../../utils/docsLinks' import { buildTiles } from '../../utils/homeTopicTiles' import { STATE_CODE_TO_NAME } from '../../utils/stateMapping' import { CAPTION_COPY, captionText, pickCaption } from '../../utils/storyCaption' import { scrollLeftForPage, scrollPages, type ScrollPages } from '../../utils/scrollPages' /** The story the home card shows (#1025, #1133): `data-centers` or any live topic story's slug. */ export type HomeStoryKey = string /** Each known tile's look; any other live topic gets the plain one. */ const LOOK: Record = { dc: { Icon: Server, tint: '#ccfbf1', ink: '#0f766e' }, str: { Icon: House, tint: '#dcfce7', ink: '#15803d' }, lpr: { Icon: Camera, tint: '#dbeafe', ink: '#1d4ed8' }, guns: { Icon: Crosshair, tint: '#fee2e2', ink: '#b91c1c' }, pickleball: { Icon: Volleyball, tint: '#ecfccb', ink: '#4d7c0f' }, bike: { Icon: Bike, tint: '#ffedd5', ink: '#c2410c' }, fluoride: { Icon: Droplets, tint: '#e0f2fe', ink: '#0369a1' }, nonprofit: { Icon: HandHeart, tint: '#fce7f3', ink: '#be185d' }, insurance: { Icon: ShieldCheck, tint: '#e0f2fe', ink: '#0369a1' }, } const PLAIN_LOOK = { Icon: MessagesSquare, tint: '#f5f5f4', ink: '#44403c' } interface Props { /** The page's place (?state=&city=), carried by the "all stories" link. */ query: string /** The story shown above the strip. */ selected: HomeStoryKey onSelect: (story: HomeStoryKey) => void /** The page's scope, for the companies panel (CLAUDE.md scope-label rule). */ stateCode?: string city?: string | null scopeLabel: string } /** * "Browse topics" under the home story (#1013, #1025, #1133): picks which story the card above shows (Data centers by * default). A tile is live when the topic catalog has a curated topic for it with meetings (utils/homeTopicTiles); guides * link to their page; the rest honestly say "Coming soon". */ export default function HomeTopicStrip({ query, selected, onSelect, stateCode, city = null, scopeLabel }: Props) { const { topics } = useTopicStories() const TOPICS = buildTiles(topics) // The card whose companies are shown (#1052): the live story's card, or a coming-soon card the reader picked. const storyCard = TOPICS.find((t) => t.story === selected)?.key ?? TOPICS[0]!.key const [picked, setPicked] = useState(storyCard) useEffect(() => setPicked(storyCard), [storyCard]) const shown = TOPICS.find((t) => t.key === picked && t.companies) // Each tile's caption (#1125): the strongest statement its record supports for the page's scope, else its blurb. const scope = { stateCode, stateName: stateCode ? (STATE_CODE_TO_NAME[stateCode] ?? stateCode) : null, city: stateCode ? city : null } // One query per distinct facts slug (#1170): guide tiles have no slug and several tiles can share one, and duplicate // keys in useQueries share observers and trigger TanStack's "Duplicate Queries" warning. // Only live tiles ask (#1191): a "coming soon" tile's topic isn't in the catalog, so the API answers 404 for it (it // showed its blurb anyway); guides have no meeting record behind them. const slugOf = (t: (typeof TOPICS)[number]): string | null => { if (t.status !== 'live') return null const copy = CAPTION_COPY[t.captionKey] return (copy ? copy.facts : t.captionKey) || null } const slugs = [...new Set(TOPICS.map(slugOf).filter((s): s is string => Boolean(s)))] const slugQ = useQueries({ queries: slugs.map((slug) => ({ queryKey: ['caption-facts', slug, stateCode ?? '', scope.city ?? ''], queryFn: () => fetchTopicCaptionFacts(slug, stateCode, scope.city), staleTime: 5 * 60_000, retry: 1, })), }) const factsQ = TOPICS.map((t) => { const slug = slugOf(t) return slug ? slugQ[slugs.indexOf(slug)] : undefined }) const captions = TOPICS.map((t, i) => { const c = pickCaption(t.captionKey, factsQ[i]?.data, scope) return c.kind === 'question' ? null : { text: captionText(c, CAPTION_COPY[t.captionKey]?.topic ?? t.title.toLowerCase()), ai: c.ai } }) // The scroller (#1101): snap per card, previous/next arrows and one dot per page of visible cards. const listRef = useRef(null) const [pos, setPos] = useState({ pages: 1, current: 0, atStart: true, atEnd: true }) const measure = useCallback(() => { const el = listRef.current if (!el) return const next = scrollPages(el.scrollLeft, el.clientWidth, el.scrollWidth) setPos((p) => (p.pages === next.pages && p.current === next.current && p.atStart === next.atStart && p.atEnd === next.atEnd ? p : next)) }, []) useEffect(() => { const el = listRef.current if (!el) return let frame = 0 const onScroll = () => { if (frame) return frame = requestAnimationFrame(() => { frame = 0 measure() }) } measure() el.addEventListener('scroll', onScroll, { passive: true }) const ro = typeof ResizeObserver !== 'undefined' ? new ResizeObserver(measure) : null ro?.observe(el) return () => { el.removeEventListener('scroll', onScroll) ro?.disconnect() if (frame) cancelAnimationFrame(frame) } }, [measure]) const behavior = (): ScrollBehavior => typeof window !== 'undefined' && window.matchMedia?.('(prefers-reduced-motion: reduce)').matches ? 'auto' : 'smooth' const page = (dir: 1 | -1) => { const el = listRef.current if (el) el.scrollBy({ left: dir * el.clientWidth, behavior: behavior() }) } const goTo = (index: number) => { const el = listRef.current if (el) el.scrollTo({ left: scrollLeftForPage(index, el.clientWidth, el.scrollWidth), behavior: behavior() }) } // Keyboard (#1146): one tab stop into the strip (roving tabindex); ←/→/Home/End move between tiles, Enter or Space picks one. const selectedIndex = Math.max(0, TOPICS.findIndex((t) => t.story != null && t.story === selected)) const [focusIndex, setFocusIndex] = useState(selectedIndex) useEffect(() => setFocusIndex(selectedIndex), [selectedIndex]) const tabStop = Math.min(focusIndex, TOPICS.length - 1) const tileAt = (i: number) => listRef.current?.querySelectorAll('[data-topic-tile]')[i] ?? null const onTileKey = (e: KeyboardEvent) => { const n = TOPICS.length const target = e.target as HTMLElement if (!target.hasAttribute('data-topic-tile')) return const next = e.key === 'ArrowRight' ? (focusIndex + 1) % n : e.key === 'ArrowLeft' ? (focusIndex - 1 + n) % n : e.key === 'Home' ? 0 : e.key === 'End' ? n - 1 : null if (next != null) { e.preventDefault() setFocusIndex(next) const el = tileAt(next) el?.focus({ preventScroll: true }) } else if (e.key === ' ' && target.tagName === 'A') { // A guide tile is a link: Space follows it as Enter does, like the other tiles. e.preventDefault() target.click() } } return ( ) }