Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 11,506 Bytes
a8fb296 | 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 232 233 234 235 | 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<string, string> = {
'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<HTMLDivElement>()
const svgRef = useRef<SVGSVGElement | null>(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<SVGSVGElement>(`${chartKey}|${scopeLabel}`)
const [tableOpen, setTableOpen] = useState(false)
const [playing, setPlaying] = useState<MeetingVideo | null>(null)
const playingFrom = useRef<number | null>(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<GroupingKey>(() => (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<number>(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<SVGElement>(`[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<SVGElement>(`[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 (
<div className="sc-block">
{options.length > 1 && (
<div className="sc-controls">
<div className="folio-seg" role="group" aria-label="Group the squares">
{options.map((k) => (
<button key={k} type="button" aria-pressed={k === active} onClick={() => setGrouping(k)}>
{groupingName(k)}
</button>
))}
</div>
</div>
)}
<div ref={ref} data-keys-own onKeyDown={onKey}>
<svg
ref={svgRefs}
className={`sc-svg sc-matrix${revealed ? ' is-revealed' : ''}`}
viewBox={`0 0 ${layout.width} ${layout.height}`}
role="group"
aria-label={`${spec.title}, ${scopeLabel}: one square per ${spec.unit}, ${groupingName(active)}`}
>
{layout.labels.map((l) => (
<text key={l.key} className="sc-label" x={l.x} y={l.y}>
{l.text}
</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 (
<rect
key={r.d.id + i}
data-sq={i}
className={`sc-sq${g?.tone ? ` sc-tone-${g.tone}` : g?.key === 'none' && active !== 'all' ? ' sc-tone-none' : ''}`}
width={s.size}
height={s.size}
style={{ transform: `translate(${s.x}px, ${s.y}px)`, transitionDelay: `${fly}ms, ${fly}ms, ${wave}ms`, fill: g?.key === 'none' && active !== 'all' ? undefined : colour(gi, g?.tone) }}
tabIndex={i === rove ? 0 : -1}
role="img"
aria-label={label}
{...b}
onFocus={(e) => {
setRove(i)
b.onFocus(e)
}}
onClick={open}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') open(e)
}}
/>
)
})}
</svg>
</div>
{active !== 'all' && (
<p className="sc-note">
{`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.')}
</p>
)}
<p className="stance-note stance-hint-keys">Use the arrow keys to move between squares.</p>
{records.some((r) => r.d.url) && <p className="stance-note stance-hint-touch">{hasVideo ? 'Tap a square to play the meeting.' : 'Tap a square to open it.'}</p>}
<button type="button" className="folio-btn" aria-expanded={tableOpen} aria-controls={`${idBase}-matrix-table`} onClick={() => setTableOpen((o) => !o)}>
{tableOpen ? 'Hide data table' : 'Show data table'} {tableOpen ? '↑' : '↓'}
</button>
{tableOpen && (
<DataTable
id={`${idBase}-matrix-table`}
caption={`Data table: ${spec.title}, ${scopeLabel}, ${groupingName(active)}`}
spec={{
columns: [{ label: 'Group' }, { label: 'Squares', numeric: true }, { label: 'Share', numeric: true }],
rows: groups.map((g) => [g.label, fmt(g.count), `${g.pct.toFixed(1)}%`]),
}}
/>
)}
{typeof document !== 'undefined' && createPortal(<div className="folio" style={HOUSE_STYLE}><Tooltip tip={tip} /></div>, document.body)}
<MeetingVideoModal video={playing} onClose={closeVideo} />
</div>
)
}
|