Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 12,315 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 236 237 238 239 240 241 242 243 | import { useCallback, useMemo, useRef, useState, type KeyboardEvent, type MouseEvent, type ReactNode } from 'react'
import { createPortal } from 'react-dom'
import { useQuery } from '@tanstack/react-query'
import { fetchChartData } from '../../api/stance'
import { chartSpec, type DotGroup, type DotsSpec } from '../../utils/stanceGame'
import { docsUrl } from '../../utils/docsLinks'
import { GROUP_COLORS, HOUSE_STYLE } from '../../utils/houseTheme'
import { DataTable } from '../matrix/SectionShell'
import { Tooltip, useTooltip } from '../matrix/Tooltip'
import { youtubeThumbnail } from '../../lib/youtubeThumbnail'
import MeetingVideoModal, { type MeetingVideo } from './MeetingVideoModal'
import UnitMatrixChart from './UnitMatrixChart'
import TimelineChart from './TimelineChart'
interface Props {
chartKey: string
round: number
/** Only the round in view and its neighbours load their data. */
enabled: boolean
stateCode: string | undefined
/** Full state name when a state is chosen, otherwise null (national). */
stateName: string | null
/** A city, for the data stories (the challenge never narrows its charts below a state). */
city?: string | null
/** What the caption says the figure shows; defaults to the state name or "National". */
scopeLabel?: string
/** Shown under "No data available" (for example buttons that broaden the place). */
emptyExtra?: ReactNode
/** Draw the dataset another way (`meetings`: the meeting passages' data as one dot per meeting). */
view?: 'meetings'
/** The topic slug behind the meetings timeline (#1140); the data-center story's charts are `data-centers`. */
topic?: string
}
/** Record charts drawn as a unit matrix (#1140): one square per listed record, regrouped by real fields. */
const MATRIX_CHARTS = new Set(['meeting-mentions', 'agenda-items', 'decisions', 'job-claims'])
const fmt = (n: number) => n.toLocaleString('en-US')
/** The dataset behind one round's chart, shared by the chart and the headline through the query cache. */
export function useChartData(chartKey: string, enabled: boolean, stateCode: string | undefined, city?: string | null) {
const c = stateCode && city ? city : ''
return useQuery({
queryKey: ['stance', 'chart', chartKey, stateCode ?? 'national', c],
queryFn: () => fetchChartData(chartKey, stateCode, c || undefined),
enabled,
staleTime: 5 * 60_000,
retry: 1,
})
}
/** A group's colour: its stance tone (set in stance.css, with a shape of its own), its own fixed colour, or the next colour of the house theme. */
function dotLook(g: DotGroup, gi: number): { className: string; background: string | undefined } {
if (g.tone) return { className: ` stance-dot--${g.tone}`, background: undefined }
return { className: '', background: g.color ?? GROUP_COLORS[gi % GROUP_COLORS.length] }
}
/**
* One mark per real record (or per stated number of records), grouped under "Group · N records · P%". Roving focus: arrow
* keys move between the dots. The dots appear one after another when the chart arrives (stance.css reads `--f`).
* A meeting dot with a YouTube recording (#972) previews its thumbnail in the tooltip and plays in a modal on click, Enter
* or Space; nothing loads from YouTube before that. Other dots open their source in a new tab as before.
*/
export function Dots({ spec }: { spec: DotsSpec }) {
const { bind, tip, hide } = useTooltip()
const [rove, setRove] = useState(0)
const [playing, setPlaying] = useState<{ video: MeetingVideo; from: number } | null>(null)
const root = useRef<HTMLDivElement>(null)
const playingFrom = useRef<number | null>(null)
const closeVideo = useCallback(() => {
const from = playingFrom.current
playingFrom.current = null
setPlaying(null)
// Back to the dot the video came from, so keyboard users keep their place.
if (from != null) requestAnimationFrame(() => root.current?.querySelector<HTMLElement>(`[data-dot="${from}"]`)?.focus())
}, [])
const flat = useMemo(() => spec.groups.flatMap((g, gi) => g.dots.map((d) => ({ d, gi, label: g.label }))), [spec])
const onKey = (e: KeyboardEvent) => {
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' ? flat.length - 1 : step ? Math.min(flat.length - 1, Math.max(0, rove + step)) : null
if (to == null) return
e.preventDefault()
e.stopPropagation()
setRove(to)
root.current?.querySelector<HTMLElement>(`[data-dot="${to}"]`)?.focus()
}
let n = -1
const total = flat.length
return (
<>
<div className="stance-dots" ref={root} data-keys-own onKeyDown={onKey}>
{spec.groups.map((g, gi) => {
const look = dotLook(g, gi)
return (
<div className="stance-dotgroup" key={g.key} data-group={g.key}>
<p className="stance-dotgroup-head">
<span className={`stance-swatch${look.className}`} style={{ background: look.background }} aria-hidden="true" />
{g.heading}
</p>
<div className="stance-dotrow" role="list" aria-label={g.heading}>
{g.dots.map((d) => {
n += 1
const i = n
const sub = [d.place, d.date].filter(Boolean).join(' · ') || undefined
const info = { cat: g.label, val: d.title, sub, thumb: d.video ? youtubeThumbnail(d.video.videoId, 'mqdefault') ?? undefined : undefined }
const label = `${d.title}${d.place ? `, ${d.place}` : ''}${d.date ? `, ${d.date}` : ''}${
d.video ? ' (plays the recording)' : d.url ? ' (opens the source)' : ''
}`
const b = bind(info)
const play = d.video
? () => {
hide()
playingFrom.current = i
setPlaying({ video: { ...d.video!, title: d.title, sub, url: d.url, quote: d.quote ?? null, place: d.place, date: d.date }, from: i })
}
: null
const common = {
className: `stance-dot${look.className}${d.weight != null && d.weight < spec.perDot ? ' is-part' : ''}`,
'data-dot': i,
'aria-label': label,
tabIndex: i === rove ? 0 : -1,
style: { background: look.background, ['--f' as string]: total > 1 ? i / total : 0 },
...b,
onFocus: (e: { currentTarget: Element }) => {
setRove(i)
b.onFocus(e)
},
...(play && {
'aria-haspopup': 'dialog' as const,
onClick: (e: MouseEvent) => {
// A modifier click still opens YouTube in a tab, like any link.
if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return
e.preventDefault()
play()
},
onKeyDown: (e: KeyboardEvent) => {
if (e.key === ' ') {
e.preventDefault()
play()
}
},
}),
}
return d.url ? (
<a key={d.id + i} role="listitem" href={d.url} target="_blank" rel="noopener noreferrer" {...common} />
) : (
<span key={d.id + i} role="listitem" {...common} />
)
})}
</div>
</div>
)
})}
{typeof document !== 'undefined' && createPortal(<div className="folio" style={HOUSE_STYLE}><Tooltip tip={tip} /></div>, document.body)}
<p className="stance-note stance-hint-keys">Use the arrow keys to move between dots.</p>
{flat.some((x) => x.d.url) && (
<p className="stance-note stance-hint-touch">{flat.some((x) => x.d.video) ? 'Tap a dot to play the meeting.' : 'Tap a dot to open it.'}</p>
)}
</div>
{/* Outside the dots' div: React portals bubble events up the React tree, so the dialog's keys must not reach the
dots' arrow-key handler and move focus behind the dialog. */}
<MeetingVideoModal video={playing?.video ?? null} onClose={closeVideo} />
</>
)
}
/** The chart for one round: one simple figure from the dataset the statement names, its FIG. caption and data table. */
export default function ChartPanel({ chartKey, round, enabled, stateCode, stateName, city, scopeLabel, emptyExtra, view, topic = 'data-centers' }: Props) {
const q = useChartData(chartKey, enabled, stateCode, city)
const spec = useMemo(() => chartSpec(chartKey, q.data, stateName, view), [chartKey, q.data, stateName, view])
const [open, setOpen] = useState(false)
const fig = String(round).padStart(2, '0')
const scope = scopeLabel ?? stateName ?? 'National'
const hasLinks = Boolean(spec?.groups.some((g) => g.dots.some((d) => d.url)))
const hasVideos = Boolean(spec?.groups.some((g) => g.dots.some((d) => d.video)))
const matrix = Boolean(spec && MATRIX_CHARTS.has(chartKey) && !view && spec.perDot === 1)
/** The caption names the mark drawn: a square in the unit matrix, a dot otherwise. */
const mark = (t: string) => (matrix ? t.replace(/\bdots?\b/g, (w) => (w === 'dot' ? 'square' : 'squares')) : t)
return (
<figure className="stance-figure" aria-label={spec?.title ?? 'Chart'}>
{/* The Meetings screen (#1140): the trend line with its milestone track, above the squares. The period dumbbell moved
to the Overview, the first screen after the cover (#1192), so it is not drawn here again. */}
{chartKey === 'meeting-mentions' && !view && enabled && (
<TimelineChart topic={topic} stateCode={stateCode} city={city} scopeLabel={scope} subject="data centers" idBase={`stance-${round}`} />
)}
{q.isLoading && enabled ? (
<p className="folio-muted" role="status">
Loading chart…
</p>
) : !spec ? (
<div className="stance-nodata" role="status">
<p>{q.isError ? 'This chart could not be loaded right now.' : 'No data available for this chart'}</p>
{emptyExtra}
</div>
) : matrix ? (
<UnitMatrixChart spec={spec} chartKey={chartKey} placeGrain={stateCode ? 'town' : 'state'} idBase={`stance-${round}`} scopeLabel={scope} />
) : (
<Dots spec={spec} />
)}
<figcaption className="folio-fig">
<b>FIG. {fig}</b> — {spec ? `${spec.title}, ${scope}.` : `${scope}.`}
{spec && mark(` ${spec.dotMeans} ${hasVideos ? 'Hover or focus a dot to preview the meeting; select it to play the recording from the quoted moment.' : hasLinks ? 'Select a dot to open it.' : 'Hover or focus a dot for details.'}`)}
{spec && ` Source: ${spec.source}.`}
{spec?.note && ` ${spec.note}`}
{spec && chartKey === 'sites' && city && ' Sites are listed by state only: the source has no city.'}
{spec && stateName == null && ' National figures, because no state is chosen.'}{' '}
<a href={docsUrl('/guides/data-stories#sources-and-limits')} target="_blank" rel="noopener noreferrer">
About this data
</a>
</figcaption>
{spec && (
<>
<button
type="button"
className="folio-btn"
aria-expanded={open}
aria-controls={`stance-data-${round}`}
onClick={() => setOpen((o) => !o)}
>
{open ? 'Hide data table' : 'Show data table'} {open ? '↑' : '↓'}
</button>
{open && (
<DataTable
id={`stance-data-${round}`}
caption={`Data table: ${spec.title}, ${scope}`}
spec={{
columns: [
{ label: 'Group' },
{ label: spec.grandTotal != null ? 'Records' : 'Records shown', numeric: true },
{ label: spec.grandTotal != null ? 'Share' : 'Share (not known)', numeric: true },
{ label: 'Dots drawn', numeric: true },
],
rows: spec.groups.map((g) => [g.label, fmt(g.total ?? g.shown), g.share != null ? `${g.share}%` : '—', fmt(g.shown)]),
}}
/>
)}
</>
)}
</figure>
)
}
|