import { useMemo, useRef, useState } from 'react' import { Link, useNavigate } from 'react-router-dom' import { useQuery } from '@tanstack/react-query' import { fetchTopicCaptionFacts, fetchTopicStanceMap, fetchTopicStates } from '../../api/topics' import { topicStanceForMap } from '../../utils/topicStance' import { quoteStanceNote, topicPositions } from '../../utils/topicPositions' import { storyScopeLabel, type StoryScope } from '../../utils/dataStory' import { useStoryScope } from '../../hooks/useStoryScope' import { CAPTION_COPY, captionSubhead, captionText, isLongTitle, pickCaption, storyStartLabel } from '../../utils/storyCaption' import '../../styles/story-actions.css' import type { TopicStoryDef } from '../../utils/topicStories' import { useBoxFlight } from '../stance/BoxFlightProvider' import StoryStatesMap, { type StateQuote } from './StoryStatesMap' import { stanceFromPublicLean } from '../../utils/mapQuote' import { useInsightYears } from '../chart/useInsightYears' import TopicCountyMap from '../stance/TopicCountyMap' import HomeStoryTitle, { type TopicControl, HomeStorySubhead } from './HomeStoryTitle' import { storyStartHref } from '../../utils/storyNav' /** A state's map passages, newest first, at most this many. */ const QUOTES_PER_STATE = 12 interface Props { def: TopicStoryDef /** The place asked (the URL's); the card speaks for the level `useStoryScope` resolves (#1285). */ scope: StoryScope /** "?state=AL&city=…" (or empty): carried by every link. */ query: string /** A place picked in the title's place control (#1206); see `HomeDataStories`. */ onPlaceApply?: (stateCode: string | null, city: string | null) => void /** The topic word as a dropdown of every live story (#1350). */ topicControl?: TopicControl } /** * The home card for one topic story (#1013, #1133): its caption for the page's scope (#1125), its lede, Start the story, * and the map of meetings per state with each state's newest verbatim passage. Every figure is a real count. */ export default function HomeTopicStoryCard({ def, scope: asked, query, onPlaceApply, topicControl }: Props) { const topicNoun = CAPTION_COPY[def.slug]?.topic ?? def.subject.toLowerCase() // The place the story speaks for (#1285): too few meetings for the city (or state) steps up a level, and says why. const placeScope = useStoryScope(def.slug, topicNoun, asked.stateCode, asked.city) const scope: StoryScope = placeScope.effective const label = storyScopeLabel(scope) const natYears = useInsightYears(def.slug, scope.stateCode ?? undefined) // The map's hover card goes in the text column, under Start (#1277), never over the map. const [cardSlot, setCardSlot] = useState(null) const states = useQuery({ queryKey: ['topic-story', def.topicId, 'states', 3], queryFn: () => fetchTopicStates(def.topicId, { quotes_per_state: 3 }), staleTime: 5 * 60_000, retry: 1, }) const facts = useQuery({ queryKey: ['caption-facts', def.slug, scope.stateCode ?? '', scope.city ?? ''], queryFn: () => fetchTopicCaptionFacts(def.slug, scope.stateCode, scope.city), staleTime: 5 * 60_000, retry: 1, }) // The AI reading of each state's meetings (#1127, #1182), the same four-colour split as the topic story's map, so the // title's lean claim and the map beside it say the same thing. No reading yet, or a failed request, leaves the plain // meetings map. const stanceQ = useQuery({ queryKey: ['topic-story', def.topicId, 'state-map'], queryFn: () => fetchTopicStanceMap(def.topicId), staleTime: 5 * 60_000, retry: 1, }) const stance = useMemo(() => topicStanceForMap(stanceQ.data), [stanceQ.data]) const quotes = useMemo(() => { const out = new Map() for (const m of states.data?.newest ?? []) { if (!m.state_code || !m.excerpt) continue const q: StateQuote = { place: [m.jurisdiction_name ?? m.city, m.state_code].filter(Boolean).join(', '), band: stanceFromPublicLean(m.public_stance), text: m.excerpt, date: m.date ?? null, meeting: m.title ?? null, // Where the public stood at the quoted meeting (#1230): the API picks a meeting where it took a side first. stanceNote: quoteStanceNote(def.slug, CAPTION_COPY[def.slug]?.topic ?? def.subject.toLowerCase(), m.public_stance), url: m.video_url ?? null, videoId: m.video_id ?? null, // Named only where the speaker introduced themselves on the record (#1231). speakerName: m.speaker_display_name ?? null, role: m.speaker_display_name ? (m.speaker_role ?? null) : null, } const list = out.get(m.state_code) if (!list) out.set(m.state_code, [q]) else if (list.length < QUOTES_PER_STATE) list.push(q) } // At city level (#1285) the state shows only that city's passages: label and quotes move together. if (scope.city && scope.stateCode) { const c = scope.city.toLowerCase() out.set(scope.stateCode, (out.get(scope.stateCode) ?? []).filter((x) => x.place.toLowerCase().startsWith(c))) } return out }, [states.data, def.slug, def.subject, scope.city, scope.stateCode]) const caption = pickCaption(def.slug, facts.data, scope) const title = captionText(caption, topicNoun) // Start opens the story past its cover (#1222). const href = storyStartHref(def.slug, query) const navigate = useNavigate() const mapWrapRef = useRef(null) const boxFlight = useBoxFlight() const positions = topicPositions(def.slug) // At state scope (#1349) the hero draws the story Overview's county map: a county or a town ring opens the story there. const drill = (city: string | null) => { if (!scope.stateCode) return if (mapWrapRef.current) { boxFlight.launch({ source: mapWrapRef.current, focusGroup: city ?? scope.stateCode, targetSelector: '.stance-story [data-live], .stance-story [data-panel="0"]', }) } const p = new URLSearchParams({ state: scope.stateCode, screen: 'overview' }) if (city) p.set('city', city) navigate(`/stories/${encodeURIComponent(def.slug)}?${p.toString()}`) } const statesMap = ( `/stories/${encodeURIComponent(def.slug)}?state=${encodeURIComponent(code)}&screen=overview`} subject={topicNoun.toLowerCase()} stance={stance && stance.length > 0 ? stance : undefined} stanceUnit="meetings" positions={positions} cardSlot={cardSlot} /> ) return (
{/* Keyed text only: the map keeps its boxes and recolours in place when the topic changes (#1416). */}

Data story · {def.title}

{/* A broader scope's title (#1222): the line under it names the place in scope (as its control); else the lede. */}

{caption.basis ?? 'Counted from local meeting recordings and transcripts that mention the topic.'}

{ if (mapWrapRef.current) { boxFlight.launch({ source: mapWrapRef.current, focusGroup: scope.stateCode ?? undefined, targetSelector: '.stance-story [data-live], .stance-story [data-panel="0"]', }) } }} > {storyStartLabel(def.slug, label, def.subject)}
{states.isError ? (

The map could not be loaded right now.

) : scope.stateCode ? ( onPlaceApply?.(code, null)} onDrill={drill} chrome={false} fallback={statesMap} /> ) : ( statesMap )}
) } /** * The drill-down line under the hero's buttons (#1176): says how to go deeper, so the reader knows the map is the way in. * The scope in effect is named by the title's place control (#1206; CLAUDE.md, scope label rule), so this line no longer * repeats it. It is also the slot the map's desktop hover card fills (#1277): the card replaces the line while a state is * hovered, and the slot keeps its height so the page doesn't jump. */ export function HomeStoryDrill({ slotRef, hint = 'Pick a state on the map to open its story.' }: { slotRef?: (el: HTMLDivElement | null) => void; hint?: string }) { return (

{hint}

) }