Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 10,443 Bytes
d15f07c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | 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<TopicLocalActionsResponse>({
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<ActionGroup | null>(null)
const [showAll, setShowAll] = useState(false)
const [hoverYear, setHoverYear] = useState<string | null>(null)
const { ref: figRef, width: figWidth } = useChartWidth<HTMLElement>(640)
const meetings = useMemo(() => (q.data?.meetings ?? []).filter((m) => !only || m.outcome_group === only), [q.data, only])
const title = (
<p className="sc-h">
Where local bodies acted on {subject} · {scopeLabel}
</p>
)
if (q.isLoading) {
return (
<div className="sc-block">
{title}
<p className="sc-note" role="status">Loading local actions…</p>
</div>
)
}
if (q.isError || !q.data) {
return (
<div className="sc-block">
{title}
<p className="sc-note">Local actions could not be loaded right now.</p>
</div>
)
}
const d = q.data
if (!d.available || d.total === 0) {
return (
<div className="sc-block">
{title}
<p className="sc-note">
No meeting{scopeLabel === 'National' ? '' : ` in ${scopeLabel}`} has an AI reading of a vote or a delay on {subject} yet.
</p>
</div>
)
}
// 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 (
<div className="sc-block la">
{title}
<ul className="la-totals" aria-label="Totals">
{ACTION_ORDER.map((g) => (
<li key={g}>
<button type="button" className="la-total" aria-pressed={only === g} onClick={() => setOnly(only === g ? null : g)}>
<span className="lm-swatch" style={{ background: ACTION_FILL[g] }} aria-hidden="true" />
<span className="la-num">{fmt(d.totals[g])}</span>
<span className="la-word">{labels[g]}</span>
</button>
</li>
))}
</ul>
{years.length > 0 && (
<ChartFrame title={frameTitle} legend={legend} caption={`Meetings with an action on ${subject}, by year · ${scopeLabel}`}>
<figure className="la-fig" ref={figRef}>
<svg viewBox={`0 0 ${W} ${H}`} className="sc-svg" role="img" aria-label={`Meetings with an action on ${subject}, by year`}>
<line x1={padL} x2={W} y1={H - padB} y2={H - padB} stroke="var(--rule)" />
<text x={padL - 6} y={H - padB - yScale(maxY) + 4} textAnchor="end" className="sc-micro">
{maxY}
</text>
{years.map((y, i) => {
const x = padL + i * colW + (colW - barW) / 2
let base = H - padB
return (
<g
key={y.year}
onMouseEnter={() => setHoverYear(y.year)}
onMouseLeave={() => setHoverYear(null)}
onClick={() => setHoverYear((h) => (h === y.year ? null : y.year))}
opacity={hoverYear && hoverYear !== y.year ? 0.45 : 1}
>
<rect x={padL + i * colW} y={0} width={colW} height={H - padB} fill="transparent" />
{ACTION_ORDER.map((g) => {
const n = only && only !== g ? 0 : y.counts[g]
if (!n) return null
const h = yScale(n)
base -= h
return <rect key={g} x={x} y={base + 1} width={barW} height={Math.max(1, h - 2)} rx={2} fill={ACTION_FILL[g]} />
})}
{((i % labelEvery === 0 && (labelEvery === 1 || years.length - 1 - i >= labelEvery)) || i === years.length - 1) && (
<text
x={i === years.length - 1 ? Math.min(W, x + barW) : i === 0 ? Math.max(padL, x) : x + barW / 2}
y={H - 6}
textAnchor={i === years.length - 1 ? 'end' : i === 0 ? 'start' : 'middle'}
className="sc-micro"
>
{y.year}
</text>
)}
</g>
)
})}
</svg>
<figcaption className="lm-readout" aria-live="polite">
{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.`}
</figcaption>
</figure>
</ChartFrame>
)}
{!stateCode && d.by_state.length > 0 && (
<div className="la-states">
<p className="la-sub">By state ({d.by_state.length})</p>
<ul className="la-statelist">
{d.by_state.slice(0, 12).map((s) => {
const n = actionTotal(s.counts)
return (
<li key={s.state_code} title={ACTION_ORDER.map((g) => `${s.counts[g]} ${labels[g].toLowerCase()}`).join(', ')}>
<span className="la-state">{s.state ?? STATE_CODE_TO_NAME[s.state_code] ?? s.state_code}</span>
<span className="la-strip" aria-hidden="true">
{ACTION_ORDER.map((g) =>
s.counts[g] ? <span key={g} style={{ flex: s.counts[g], background: ACTION_FILL[g] }} /> : null,
)}
</span>
<span className="la-n">{n}</span>
</li>
)
})}
</ul>
</div>
)}
<p className="la-sub">
Meetings{only ? `: ${labels[only].toLowerCase()}` : ''} ({fmt(meetings.length)})
</p>
<ul className="la-meetings">
{(showAll ? meetings : meetings.slice(0, FIRST_MEETINGS)).map((m) => (
<li key={`${m.video_id}-${m.outcome_group}`}>
<span className="lm-swatch" style={{ background: ACTION_FILL[m.outcome_group] }} aria-hidden="true" />
<span className="la-when">{fmtDate(m.meeting_date)}</span>
<span className="la-where">
{m.url ? (
<a href={m.url} target="_blank" rel="noopener noreferrer">
{m.jurisdiction_name ?? 'Meeting'}
{m.state_code ? `, ${m.state_code}` : ''}
</a>
) : (
<>
{m.jurisdiction_name ?? 'Meeting'}
{m.state_code ? `, ${m.state_code}` : ''}
</>
)}
</span>
<span className="la-what">
{labels[m.outcome_group]}
{m.outcome_p != null && <span className="lm-meta"> · AI reading, {Math.round(m.outcome_p * 100)}% sure</span>}
</span>
</li>
))}
</ul>
{meetings.length > FIRST_MEETINGS && (
<button type="button" className="lm-chip" onClick={() => setShowAll((v) => !v)}>
{showAll ? 'Show fewer' : `Show all ${fmt(meetings.length)}`}
</button>
)}
<p className="sc-note">
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.
</p>
</div>
)
}
|