import { useCallback, useId, useMemo, useState } from 'react' import { useQuery } from '@tanstack/react-query' import type { DataCenterPositionsResponse, PositionQuote } from '../../api/dataCenters' import { fetchTopicCaptionFacts } from '../../api/topics' import { POSITIONS_NOT_LOADED, figureDisplay, positionFigures, positionsCaption, positionsLoaded, storyScopeLabel, type LoadState, type StoryScope } from '../../utils/dataStory' import { chartSpec } from '../../utils/stanceGame' import { Dots } from './ChartPanel' import { SideColumn, type Passage } from './SidesBlock' import CommentMetricsBlock from './CommentMetricsBlock' import { DrillValue, KpiDrillSheet, figureDrill, isDrillable } from './KpiDrill' import FitDisplay from './FitDisplay' import PositionsSummary from './PositionsSummary' import UnitMatrixChart from './UnitMatrixChart' import { specDisplay } from '../../utils/displayFit' interface Props { data: DataCenterPositionsResponse | null | undefined status: LoadState scope: StoryScope /** The id of the heading that names the block (the hub's "Who spoke up"; the story screen's headline). */ labelledBy?: string /** Also list a few of the speakers' own words on each side (the story's screen; the hub stays short). */ quotes?: boolean } type PositionFigureKey = 'for' | 'against' | 'question' | 'votes_for' | 'votes_against' const toPassage = (q: PositionQuote): Passage => ({ quote: q.quote, speakerName: q.speaker_display_name ?? null, speakerRole: q.speaker_role, jurisdictionName: [q.jurisdiction_name, q.state_code].filter(Boolean).join(', '), meetingDate: q.meeting_date, videoUrl: q.video_url, startSeconds: q.start_seconds, about: q.decision_headline, }) /** * Who spoke up (#930): five figures (spoke for, spoke against, asked a question, votes for, votes against) and one dot chart, * one dot per speaker (or per stated number of speakers). Every figure is a whole-scope total from the API. A figure with no * data behind it reads "not recorded" with the reason, never 0. While the table is not in the database the block is one quiet * line. */ export default function PositionsBlock({ data, status, scope, labelledBy, quotes = false }: Props) { const stateName = scope.stateName const spec = useMemo(() => chartSpec('positions', data, stateName), [data, stateName]) // The figure whose records are open (#1288); one at a time. const [drill, setDrill] = useState(null) const sheetId = useId() const closeDrill = useCallback(() => setDrill(null), []) // The caption comments in scope (#1290), from the caption facts the title reads (same query key, so cached): a speaker // figure is 0 only when nobody spoke, and a dash when people spoke but no side is recorded. const factsQ = useQuery({ queryKey: ['caption-facts', 'data-centers', scope.stateCode ?? '', scope.stateCode ? (scope.city ?? '') : ''], queryFn: () => fetchTopicCaptionFacts('data-centers', scope.stateCode ?? undefined, scope.city), staleTime: 5 * 60_000, retry: 1, }) const captionComments = factsQ.data?.caption_comments?.comments ?? null if (status === 'loading') { return (

Loading who spoke up…

) } const figures = positionFigures(data, scope, captionComments) if (!figures || !positionsLoaded(data)) { return (

{POSITIONS_NOT_LOADED}

) } const reasons = [...new Set(figures.map((f) => f.reason).filter((r): r is string => r != null))] const sides = [ { title: 'Spoke for', items: data.quotes.for.map(toPassage) }, { title: 'Spoke against', items: data.quotes.against.map(toPassage) }, { title: 'Asked a question', items: data.quotes.question.map(toPassage) }, ].filter((s) => s.items.length > 0) return (
{/* #1330: who took which side, public and officials, and where they disagree, first; the hand count after it. */}

Speakers counted by hand

{figures.map((f) => (
{f.label}
{/* "—" with "not available yet" for screen readers: no meeting reviewed, never a made-up 0 (#1289). */}
setDrill(drill === f.key ? null : f.key)} > {f.value == null ? : figureDisplay(f.value)} {f.value == null && not available yet}
))}
{drill && ( f.key === drill)?.label.replace(/^./, (c) => c.toUpperCase()) ?? ''} scope={{ stateCode: scope.stateCode, city: scope.city }} scopeLabel={storyScopeLabel(scope)} onClose={closeDrill} /> )} {reasons.map((r) => (

{r}

))} {spec && ( // The shared rule picks the form from the count (#1289): 6-400 individual speakers render as an interactive UnitMatrix (#1400).
{!spec.countsOnly && spec.drawn >= 6 && spec.drawn <= 400 ? ( <>
{spec.dotMeans.replace(/\bdots?\b/g, (w) => (w === 'dot' ? 'square' : 'squares'))}
) : ( <> } /> {specDisplay(spec).display.kind === 'units' &&
{spec.dotMeans}
} )}
)}

{positionsCaption(data, scope, captionComments)}

{quotes && sides.length > 0 && (
{sides.map((s) => ( ))}
)}
) }