import ChartFrame, { type LegendItem } from '../chart/ChartFrame' import { useMemo, useState } from 'react' import { useQuery } from '@tanstack/react-query' import { fetchTopicLocalActions, type ActionGroup, type TopicLocalActionsResponse } from '../../api/topicPolicy' import { STATE_CODE_TO_NAME } from '../../utils/stateMapping' import { ACTION_FILL, ACTION_ORDER, actionLabels, actionTotal } from '../../utils/topicPolicy' import { useChartWidth } from './useChartWidth' import './storyCharts.css' interface Props { topic: string stateCode?: string scopeLabel: string subject: string } const fmt = (n: number) => n.toLocaleString('en-US') const fmtDate = (iso: string | null) => iso ? new Date(`${iso}T00:00:00`).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' }) : 'Date not recorded' /** Meetings listed before "Show all". */ const FIRST_MEETINGS = 12 /** * Where local bodies acted (#1274): meetings where a council or board voted to move the topic forward, voted to stop or * limit it, or tabled or put it off, as read by the AI (Jev) from the transcript (public.topic_meeting_lean.outcome_group). * A stacked column per year, the states with most actions, and the meetings themselves, each linked to its recording. */ export default function LocalActionsTracker({ topic, stateCode, scopeLabel, subject }: Props) { const q = useQuery({ queryKey: ['topic-local-actions', topic, stateCode ?? 'national'], queryFn: () => fetchTopicLocalActions(topic, stateCode), staleTime: 5 * 60_000, retry: 1, }) const labels = actionLabels(topic) const [only, setOnly] = useState(null) const [showAll, setShowAll] = useState(false) const [hoverYear, setHoverYear] = useState(null) const { ref: figRef, width: figWidth } = useChartWidth(640) const meetings = useMemo(() => (q.data?.meetings ?? []).filter((m) => !only || m.outcome_group === only), [q.data, only]) const title = (

Where local bodies acted on {subject} · {scopeLabel}

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

Loading local actions…

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

Local actions could not be loaded right now.

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

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

) } // Stacked columns, one per year with an action. // Drawn at the host's real width so labels stay legible on a phone. const W = Math.max(280, figWidth) const H = W < 480 ? 160 : 180 const padL = 28 const padB = 22 // Every year from the first to the last action, so an empty year is a gap on the axis, not skipped. const years = (() => { if (!d.by_year.length) return d.by_year const have = new Map(d.by_year.map((y) => [y.year, y.counts])) const lo = Number(d.by_year[0]!.year) const hi = Number(d.by_year[d.by_year.length - 1]!.year) const out = [] for (let y = lo; y <= hi; y++) out.push({ year: String(y), counts: have.get(String(y)) ?? { advanced: 0, blocked: 0, tabled_or_deferred: 0 } }) return out })() const shown = (c: (typeof years)[number]['counts']) => (only ? c[only] : actionTotal(c)) const maxY = Math.max(1, ...years.map((y) => shown(y.counts))) const colW = years.length ? (W - padL) / years.length : 0 const barW = Math.max(4, Math.min(28, colW - 4)) const yScale = (n: number) => (n / maxY) * (H - padB - 8) const hover = hoverYear ? years.find((y) => y.year === hoverYear) : undefined const labelEvery = Math.ceil(years.length / Math.max(3, Math.floor(W / 80))) // Title and key (#1409): the real totals; each colour the bars are drawn with. const lead = [...ACTION_ORDER].sort((a, b) => d.totals[b] - d.totals[a])[0]! const frameTitle = `${fmt(d.total)} ${d.total === 1 ? 'meeting' : 'meetings'} with a vote or delay on ${subject}: ${fmt(d.totals[lead])} ${labels[lead].toLowerCase()}` const legend: LegendItem[] = ACTION_ORDER.map((g) => ({ key: g, label: `${labels[g]} (${fmt(d.totals[g])})`, color: ACTION_FILL[g] })) return (
{title}
    {ACTION_ORDER.map((g) => (
  • ))}
{years.length > 0 && (
{maxY} {years.map((y, i) => { const x = padL + i * colW + (colW - barW) / 2 let base = H - padB return ( setHoverYear(y.year)} onMouseLeave={() => setHoverYear(null)} onClick={() => setHoverYear((h) => (h === y.year ? null : y.year))} opacity={hoverYear && hoverYear !== y.year ? 0.45 : 1} > {ACTION_ORDER.map((g) => { const n = only && only !== g ? 0 : y.counts[g] if (!n) return null const h = yScale(n) base -= h return })} {((i % labelEvery === 0 && (labelEvery === 1 || years.length - 1 - i >= labelEvery)) || i === years.length - 1) && ( {y.year} )} ) })}
{hover ? `${hover.year}: ${ACTION_ORDER.map((g) => `${hover.counts[g]} ${labels[g].toLowerCase()}`).join(', ')}` : `Meetings per year${d.undated ? `; ${d.undated} more have no recorded date` : ''}. Hover or tap a year for its counts.`}
)} {!stateCode && d.by_state.length > 0 && (

By state ({d.by_state.length})

    {d.by_state.slice(0, 12).map((s) => { const n = actionTotal(s.counts) return (
  • `${s.counts[g]} ${labels[g].toLowerCase()}`).join(', ')}> {s.state ?? STATE_CODE_TO_NAME[s.state_code] ?? s.state_code}
  • ) })}
)}

Meetings{only ? `: ${labels[only].toLowerCase()}` : ''} ({fmt(meetings.length)})

{meetings.length > FIRST_MEETINGS && ( )}

What the body did, as an AI (Jev) read it from the meeting transcript, not from official minutes; the percent is how sure the reading was. A council, board or commission's action only, never a person's.

) }