import { useQuery } from '@tanstack/react-query' import { fetchTopicPositions, type TopicPositionsResponse } from '../../api/topicPolicy' import { pct, positionFill, positionStyle } from '../../utils/topicPolicy' import './storyCharts.css' interface Props { topic: string stateCode?: string scopeLabel: string subject: string /** Show only for topics read with their own five positions (beside a story's four-way breakdown); otherwise nothing. */ fiveOnly?: boolean } const fmt = (n: number) => n.toLocaleString('en-US') /** * The positions the public took in meetings (#1274): one bar per position, the count of meetings where the AI reading * (Jev) put the public who spoke closest to it, out of the meetings where the public spoke on the topic. Support on the * violet side, opposition on the vermilion side, no opinion in grey. Counts of meetings, never of speakers. Meetings * where the public didn't speak are said in the note, not drawn. */ export default function PositionBreakdown({ topic, stateCode, scopeLabel, subject, fiveOnly = false }: Props) { const q = useQuery({ queryKey: ['topic-positions', topic, stateCode ?? 'national'], queryFn: () => fetchTopicPositions(topic, stateCode), staleTime: 5 * 60_000, retry: 1, }) if (fiveOnly && q.data?.scale !== 'five') return null const title = (

What the public said in meetings on {subject} · {scopeLabel}

) if (q.isLoading) { return (
{title}

Loading positions…

) } if (q.isError || !q.data) { return (
{title}

Positions could not be loaded right now.

) } const d = q.data if (!d.available || d.read_meetings === 0) { return (
{title}

No meeting on {subject} has an AI reading{scopeLabel === 'National' ? '' : ` in ${scopeLabel}`} yet.

) } const max = Math.max(1, ...d.options.map((o) => o.meetings)) return (
{title} {d.spoke_meetings === 0 ? (

The public didn't speak on {subject} in any of the {fmt(d.read_meetings)} meetings read {scopeLabel === 'National' ? '' : ` in ${scopeLabel}`}.

) : ( )}

An AI reading (Jev) of each meeting transcript: the position closest to most of the members of the public who spoke. Out of {fmt(d.spoke_meetings)} meeting{d.spoke_meetings === 1 ? '' : 's'} where the public spoke on it {d.states > 0 && !stateCode ? ` in ${d.states} state${d.states === 1 ? '' : 's'}` : ''}; in {fmt(d.did_not_speak)} more read meeting{d.did_not_speak === 1 ? '' : 's'} the public didn't speak on it. Counts meetings, not people.

) }