import { useCallback, useEffect, useMemo, useRef, useState, type KeyboardEvent, type MouseEvent } from 'react' import { createPortal } from 'react-dom' import type { DotsSpec } from '../../utils/stanceGame' import { GROUP_COLORS, HOUSE_STYLE } from '../../utils/houseTheme' import { STATE_CODE_TO_NAME } from '../../utils/stateMapping' import { availableGroupings, groupRecords, layoutMatrix, sideLabelsFor, type GroupingKey, type MatrixRecord } from '../../utils/unitMatrix' import { DataTable } from '../matrix/SectionShell' import { Tooltip, useTooltip } from '../matrix/Tooltip' import { youtubeThumbnail } from '../../lib/youtubeThumbnail' import { staggerDelayMs } from '../../utils/folioMotion' import { useChartReveal } from './useChartReveal' import MeetingVideoModal, { type MeetingVideo } from './MeetingVideoModal' import { useChartWidth } from './useChartWidth' import './storyCharts.css' import { topicPositions } from '../../utils/topicPositions' /** What a chart's own grouping is called on its toggle. */ const NATIVE_NAMES: Record = { 'job-claims': 'By phase', 'agenda-items': 'Upcoming or past', decisions: 'Debated or not', sites: 'By status', projects: 'By status', 'community-events': 'By kind', } const fmt = (n: number) => n.toLocaleString('en-US') interface Props { spec: DotsSpec chartKey: string /** National scope groups places by state; a state scope groups them by town. */ placeGrain: 'state' | 'town' /** Used for ids (data table). */ idBase: string scopeLabel: string } /** * Unit matrix (#1140, the reference's §4.1): one square per real record, regrouped by a toggle into labelled blocks * "Name · N (x%)". Squares keep their identity across groupings and slide to their new place (no motion with * prefers-reduced-motion). Arrow keys move between squares; a meeting square plays its recording, any other opens its source. */ const DC_SIDE_LABELS = sideLabelsFor(topicPositions('data-centers')) export default function UnitMatrixChart({ spec, chartKey, placeGrain, idBase, scopeLabel }: Props) { const { ref, width } = useChartWidth() const svgRef = useRef(null) const { bind, tip, hide } = useTooltip() const [rove, setRove] = useState(0) // Squares fade in on a diagonal wave once the chart scrolls into view (#1228, the reference's §4.1); a new scope replays it. const { ref: revealRef, revealed, reduced } = useChartReveal(`${chartKey}|${scopeLabel}`) const [tableOpen, setTableOpen] = useState(false) const [playing, setPlaying] = useState(null) const playingFrom = useRef(null) const records = useMemo( () => spec.groups.flatMap((g) => g.dots.map((d) => ({ d, nativeKey: g.key, group: g.label, facets: d.facets }))), [spec], ) // A narrower scope can leave fewer squares than the roving index: keep one square tabbable. useEffect(() => setRove((r) => Math.min(r, Math.max(0, records.length - 1))), [records.length]) const native = useMemo(() => spec.groups.map((g) => ({ key: g.key, label: g.label })), [spec]) const nativeName = NATIVE_NAMES[chartKey] const options = useMemo( () => availableGroupings(records as MatrixRecord[], Boolean(nativeName) && spec.groups.length > 1, placeGrain), [records, nativeName, spec.groups.length, placeGrain], ) const [grouping, setGrouping] = useState(() => (options.includes('native') ? 'native' : options.includes('year') ? 'year' : 'all')) const active = options.includes(grouping) ? grouping : 'all' const groups = useMemo( () => groupRecords(records as MatrixRecord[], active, { placeGrain, native, placeLabel: placeGrain === 'state' ? (k) => STATE_CODE_TO_NAME[k] ?? k : undefined, // The matrix is the data center story's (#1140): its sides in that topic's words (#1200). sideLabels: DC_SIDE_LABELS, }), [records, active, placeGrain, native], ) const layout = useMemo(() => layoutMatrix(records.length, groups, width, active), [records.length, groups, width, active]) const groupOf = useMemo(() => { const m = new Array(records.length) groups.forEach((g, gi) => g.members.forEach((i) => (m[i] = gi))) return m }, [groups, records.length]) const svgRefs = useCallback( (node: SVGSVGElement | null) => { svgRef.current = node revealRef(node) }, [revealRef], ) const closeVideo = useCallback(() => { const from = playingFrom.current playingFrom.current = null setPlaying(null) if (from != null) requestAnimationFrame(() => svgRef.current?.querySelector(`[data-sq="${from}"]`)?.focus()) }, []) // Arrow keys walk the squares in the current grouping's reading order. const order = useMemo(() => groups.flatMap((g) => g.members), [groups]) const onKey = (e: KeyboardEvent) => { const pos = order.indexOf(rove) const step = e.key === 'ArrowRight' || e.key === 'ArrowDown' ? 1 : e.key === 'ArrowLeft' || e.key === 'ArrowUp' ? -1 : 0 const to = e.key === 'Home' ? 0 : e.key === 'End' ? order.length - 1 : step ? Math.min(order.length - 1, Math.max(0, pos + step)) : null if (to == null || to < 0) return e.preventDefault() e.stopPropagation() const idx = order[to]! setRove(idx) svgRef.current?.querySelector(`[data-sq="${idx}"]`)?.focus() } // Years and places are told apart by their block labels, so one colour; the chart's own groups keep the house palette. const colour = (gi: number, tone?: string) => tone ? undefined : active === 'all' ? 'var(--ink-3)' : active === 'native' ? GROUP_COLORS[gi % GROUP_COLORS.length] : 'var(--violet)' const groupingName = (k: GroupingKey) => k === 'all' ? 'All' : k === 'native' ? nativeName : k === 'year' ? 'By year' : k === 'place' ? (placeGrain === 'town' ? 'By town' : 'By state') : k === 'outcome' ? 'By outcome (AI reading)' : 'By who spoke (AI reading)' const aiGrouping = active === 'outcome' || active === 'side' const capped = spec.grandTotal != null ? spec.drawn < spec.grandTotal : /Showing/.test(spec.note ?? '') const hasVideo = records.some((r) => r.d.video) return (
{options.length > 1 && (
{options.map((k) => ( ))}
)}
{layout.labels.map((l) => ( {l.text} ))} {records.map((r, i) => { const s = layout.squares[i]! const gi = groupOf[i] ?? 0 const g = groups[gi] const sub = [r.d.place, r.d.date].filter(Boolean).join(' · ') || undefined const b = bind({ cat: g ? g.label : r.group, val: r.d.title, sub, thumb: r.d.video ? youtubeThumbnail(r.d.video.videoId, 'mqdefault') ?? undefined : undefined, }) const label = `${r.d.title}${sub ? `, ${sub}` : ''}${g && active !== 'all' ? `, ${g.label}` : ''}${r.d.video ? ' (plays the recording)' : r.d.url ? ' (opens the source)' : ''}` const open = (e: MouseEvent | KeyboardEvent) => { if (r.d.video) { if ('metaKey' in e && (e.metaKey || e.ctrlKey)) return e.preventDefault() hide() playingFrom.current = i setPlaying({ ...r.d.video, title: r.d.title, sub, url: r.d.url, quote: r.d.quote ?? null, place: r.d.place, date: r.d.date }) } else if (r.d.url) { window.open(r.d.url, '_blank', 'noopener,noreferrer') } } // .sc-sq transitions transform, fill, opacity. A regrouping flies the squares in reading order (up to ~0.5 s); // the entrance fades them in on the diagonal wave, top-left first, like the reference's first draw. const fly = reduced ? 0 : Math.min(500, Math.round((i / Math.max(1, records.length)) * 480)) const wave = reduced ? 0 : Math.round(staggerDelayMs(s.x, s.y, layout.width + layout.height)) return ( { setRove(i) b.onFocus(e) }} onClick={open} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') open(e) }} /> ) })}
{active !== 'all' && (

{`Percentages are of the ${fmt(records.length)} ${capped ? 'shown' : 'on record in this scope'}. `} {aiGrouping && (chartKey === 'decisions' ? 'Outcome as written in the meeting analysis, an AI reading of the transcript, not an official record.' : 'An AI reading (Jev) of each meeting transcript, not an official record; "Not screened" means the meeting has no reading.')}

)}

Use the arrow keys to move between squares.

{records.some((r) => r.d.url) &&

{hasVideo ? 'Tap a square to play the meeting.' : 'Tap a square to open it.'}

} {tableOpen && ( [g.label, fmt(g.count), `${g.pct.toFixed(1)}%`]), }} /> )} {typeof document !== 'undefined' && createPortal(
, document.body)}
) }