import { useEffect, useRef } from 'react' import { useQuery } from '@tanstack/react-query' import type { StoryData, StoryScope, StoryScreen, ScreenText, StoryStatus } from '../../utils/dataStory' import { chartScopeLabel, contextSourceFor, positionsLoaded, statsScopeNote, storyScopeLabel, storyStats } from '../../utils/dataStory' import type { AreaConcernsResponse, DataCenterMeetingMentionsResponse, DataCenterPositionsResponse, JobClaimsResponse } from '../../api/dataCenters' import { fetchDataCenterMeetingMentions } from '../../api/dataCenters' import ChartPanel from './ChartPanel' import PayrollJobsPanel from './PayrollJobsPanel' import PositionsBlock from './PositionsBlock' import KineticText from './KineticText' import ScreenNav from './ScreenNav' import StatRow from './StatRow' import StoryDotMap from './StoryDotMap' import DumbbellChart from './DumbbellChart' import ConcernsDumbbell from './ConcernsDumbbell' import StoryCountyMap from './StoryCountyMap' import MapDrill from './MapDrill' import StoryStatement from './StoryStatement' import type { ScreenStatement } from './useStoryStatements' import { speakerCounts } from '../../utils/stanceGame' interface Props { screen: StoryScreen index: number text: ScreenText data: StoryData status: StoryStatus scope: StoryScope active: boolean nextLabel: string | null onPrev?: () => void onNext: () => void /** Broaden the place: to the state (null when none is chosen) or to the whole country. */ onBroaden: (stateCode: string | null, city: string | null) => void onRetry: () => void /** The statement this screen asks (#981): Agree / Disagree / Not sure, then where the reader sits. */ stance?: ScreenStatement /** Overview only (#1134): the story's question worded for the place, and the reader's answer before reading, which the * retired Where screen used to show. */ question?: string | null said?: string | null } /** One screen of the record: the short word, the headline, one line of context and the chart (or counts) beside it. */ export default function StoryScreenView(p: Props) { const { screen, text, scope } = p const leftRef = useRef(null) const rightRef = useRef(null) const nextRef = useRef(null) // A column taller than its frame scrolls on its own: Lenis must leave wheel and touch over it alone, but only then. useEffect(() => { const els = [leftRef.current, rightRef.current].filter((e): e is HTMLDivElement => e != null) const mark = () => els.forEach((e) => e.toggleAttribute('data-lenis-prevent', e.scrollHeight > e.clientHeight + 1)) mark() if (typeof ResizeObserver === 'undefined') return const ro = new ResizeObserver(mark) els.forEach((e) => ro.observe(e)) return () => ro.disconnect() }) const broaden = (
{text.failed && ( )} {text.empty && scope.city && scope.stateName && ( )} {text.empty && scope.stateName && ( )}
) const stats = screen.kind === 'overview' ? storyStats(p.data, scope) : [] // The Overview map (#970) needs the per-state counts; an older API without them keeps the dot chart. const mentions = p.data['meeting-mentions'] as DataCenterMeetingMentionsResponse | null | undefined const byState = mentions?.counts?.by_state const byPlace = mentions?.counts?.by_place // The national map's hover quote (#1230): each state's map-quote meeting (real public meetings only, meetings where the // public mostly opposed first, then newest), not the newest passage, which can be a briefing or a keyword-only mention. // The same query as the home map, so it is cached. A failure falls back to the scope's passages. const overviewNational = screen.kind === 'overview' && !scope.stateCode const mapQuotes = useQuery({ queryKey: ['home', 'story-map', 'meeting-mentions', 'quotes-per-state', 3], queryFn: () => fetchDataCenterMeetingMentions({ quotesPerState: 3 }), enabled: overviewNational, staleTime: 5 * 60_000, retry: 1, }) const nationalPassages = mapQuotes.data?.items ?? mentions?.items const jobsData = p.data['job-claims'] as JobClaimsResponse | null | undefined const noClaims = (jobsData?.totals?.construction ?? 0) + (jobsData?.totals?.operations ?? 0) === 0 return (
{screen.kicker && (

{screen.kicker} ยท source: {contextSourceFor(screen.chart)}

)}

{screen.eyebrow}

{text.context}

{(p.question || p.said) && (
{p.question && (

{p.question}

)} {p.said && (

You said {p.said}.

)}
)} {(text.empty || text.failed) && broaden} {/* Asked only when the screen has a record to react to: no statement under "No data available". */} {p.stance && !text.empty && !text.failed && ( )}
{screen.chart === 'positions' ? ( // While the table is not loaded the screen's own line says so; the figure is left out, not repeated. (p.status.positions ?? 'loading') !== 'loading' && !positionsLoaded(p.data.positions as DataCenterPositionsResponse | null | undefined) ? null : ( ) ) : screen.kind === 'overview' ? ( // Narrowing to a state, its boxes re-form into the county map; broadening plays it back (#1195). The wrapper stays // mounted while the new scope's counts load, so the transition can wait for the county map. 0 ? 'ready' : 'none' } > {(beforePick) => stats.length > 0 ? ( <> {stats.some((s) => s.key === 'meetings') && scope.stateCode && byPlace && byPlace.length > 0 ? ( // At state scope, the state's counties (#1039), like the Census map's state drill-down. { beforePick(code) p.onBroaden(code, null) }} labelledBy={`story-h-${screen.id}`} passages={mentions?.items} /> ) : stats.some((s) => s.key === 'meetings') && byState ? ( { beforePick(code) p.onBroaden(code, null) }} labelledBy={`story-h-${screen.id}`} passages={nationalPassages} /> ) : stats.some((s) => s.key === 'meetings') && ( )} {/* The period dumbbell (#1192, owner: "earlier in every story"): last year against this year, by state, or by town at state scope, right under the map. It compares places, so at city scope it says where to see it. */} {scope.city ? (

The year-over-year comparison is drawn by state, or by town within a state; choose All of {scope.stateName} to see it.

) : ( )}

{statsScopeNote(stats, scope)}

) : (

{text.failed ? 'The counts could not be loaded right now.' : 'No counts available for this place.'}

) }
) : screen.chart === 'concerns' ? ( // The concerns dumbbell (#1209): the place's share of mentions against the nation's, or one dot each nationally. ) : screen.chart === 'job-claims' && jobsData && noClaims ? ( // No claims on record: the screen shows actual payroll jobs and where jobs are tracked instead (#1019). ) : ( <> {screen.chart === 'job-claims' && jobsData && } )}
) }