import { useEffect, useMemo, useRef } from 'react' import { Link } from 'react-router-dom' import { CENSUS_REGION_LABEL, type CensusRegionId } from '../../utils/censusRegions' import { rowDelayMs } from '../../utils/folioMotion' import { pivotStory } from '../../utils/matrixStory' import { barFraction } from '../../utils/matrixTable' import { useFlip, useIntro } from '../../hooks/useFlip' import { cellStyle, itemsOf, sortByKey } from './data' import { useSection } from './hooks' import { Empty, GROUP_OPTIONS, InlineBar, ScaleLegend, Seg, SortTh } from './parts' import SectionShell from './SectionShell' import { delayVar, type MatrixCtx } from './types' const MAX_COLS = 8 const INTRO_MS = 900 + 600 + 100 /** The chosen pivot columns from `?cols=`, else the first four metrics. */ export function pivotCols(ctx: Pick): string[] { const fromUrl = (ctx.sp.get('cols') ?? '').split(',').filter((s) => ctx.metrics.some((m) => m.slug === s)) return (fromUrl.length ? fromUrl : ctx.metrics.slice(0, 4).map((m) => m.slug)).slice(0, MAX_COLS) } /** Header click: sort by that key; the same key again flips the direction. */ export function nextSort(ctx: MatrixCtx, active: string, key: string) { ctx.setMany({ sort: key, dir: active === key && ctx.sortDir === 'desc' ? 'asc' : 'desc', }) } export default function PivotSection({ ctx }: { ctx: MatrixCtx }) { const cols = pivotCols(ctx) const group = ctx.sp.get('group') === 'region' ? 'region' : 'none' const activeSort = ctx.sortKey || cols[0] || '' const sorted = useMemo(() => sortByKey(ctx.rows, activeSort, ctx.sortDir), [ctx.rows, activeSort, ctx.sortDir]) const groups = useMemo( () => group === 'none' ? [{ id: 'all', label: null as string | null, items: sorted }] : (Object.keys(CENSUS_REGION_LABEL) as CensusRegionId[]) .map((id) => ({ id: id as string, label: CENSUS_REGION_LABEL[id] as string | null, items: sorted.filter((r) => r.region === id), })) .filter((g) => g.items.length), [sorted, group], ) const toggleCol = (slug: string) => { if (cols.length === 1 && cols[0] === slug) return const next = cols.includes(slug) ? cols.filter((c) => c !== slug) : [...cols, slug].slice(0, MAX_COLS) ctx.setQP('cols', next.join(',')) } let lit = 0 let total = 0 for (const r of ctx.rows) for (const c of cols) { const t = ctx.shade[c]?.(r.vals[c] ?? null) ?? null if (r.vals[c] != null) total += 1 if (t != null && t * 100 >= ctx.minPct) lit += 1 } const noun = cols.length === 1 ? 'metric' : 'metrics' const story = pivotStory({ vintage: ctx.vintage, colCount: cols.length, rowCount: ctx.rows.length, sortLabel: activeSort === 'name' ? null : ctx.labelOf(activeSort), sortDir: ctx.sortDir, items: itemsOf(ctx.rows, activeSort), fmt: (v) => ctx.fmt(activeSort, v), }) // The Share column belongs to the selected metric when it is a count; a rate has no total to take a share of. const shareSlug = ctx.countMetrics.some((m) => m.slug === ctx.metricSlug) ? ctx.metricSlug : null const isCount = (slug: string) => ctx.countMetrics.some((m) => m.slug === slug) const share = useMemo(() => { if (!shareSlug) return null const vals = ctx.rows.map((r) => r.vals[shareSlug]).filter((v): v is number => v != null) const total = vals.reduce((a, v) => a + v, 0) return vals.length && total > 0 ? { slug: shareSlug, total, maxPct: (Math.max(...vals) / total) * 100 } : null }, [ctx.rows, shareSlug]) const showTotal = !!share || cols.some(isCount) return (

A sortable breakdown: one row per geography, one column per chosen metric, each column shaded on its own scale. Click a column header to sort by it (again to reverse); the rows glide to their new places. Sorting here also orders the Heatmap and Table.

{share ? `The Share column is each row's part of the ${ctx.labelOf(share.slug)} total across all ${ctx.rows.length} rows. ` : `Share needs a count metric; ${ctx.metricLabel} is not one, so there is no Share column. `} The Total row adds up count columns only; a rate has no total, so it reads “—”.

Showing {cols.map((c) => ctx.labelOf(c)).join(', ')}. Sorted by {activeSort === 'name' ? 'name' : ctx.labelOf(activeSort)},{' '} {ctx.sortDir === 'asc' ? 'lowest' : 'highest'} first.

} controls={ <> ctx.setQP('group', v === 'none' ? null : v)} options={GROUP_OPTIONS} />
Columns ({cols.length}/{MAX_COLS})
{ctx.metrics.map((m) => ( ))}
} caption={ <> {cols.length} {noun} by state, each column shaded on its own scale; {ctx.vintage} ACS 5-year. Source: ACS 5-year, {ctx.vintage}. } table={() => ({ columns: [ { label: 'Geography' }, ...cols.map((c) => ({ label: ctx.labelOf(c), numeric: true })), ...(share ? [ { label: `Share of ${ctx.labelOf(share.slug)} total`, numeric: true, }, ] : []), ], rows: groups.flatMap((g) => g.items.map((r) => [ r.name, ...cols.map((c) => ctx.fmt(c, r.vals[c] ?? null)), ...(share ? [sharePct(r.vals[share.slug] ?? null, share.total) ?? '—'] : []), ]), ), })} > {ctx.rows.length === 0 ? ( No data available for {ctx.vintage}. ) : ( <>
{groups.map((g) => (
{g.label && (

{g.label} · {g.items.length}

)}
))}
)}
) } const sharePct = (v: number | null, total: number): string | null => (v == null ? null : `${((v / total) * 100).toFixed(1)} %`) interface ShareModel { slug: string total: number maxPct: number } function PivotTable({ ctx, items, cols, activeSort, share, showTotal, isCount, }: { ctx: MatrixCtx items: MatrixCtx['rows'] cols: string[] activeSort: string share: ShareModel | null showTotal: boolean isCount: (slug: string) => boolean }) { const { seen } = useSection() const ref = useRef(null) const intro = useIntro(INTRO_MS, ctx.reduced, seen) useFlip(ref, `${items.map((r) => r.fips).join(',')}|${cols.join(',')}`, !ctx.reduced && !intro) const { setHotFips } = ctx // A row that goes away while hovered must not leave Tiles dimmed. useEffect(() => () => setHotFips(null), [setHotFips]) const sortFirst = (key: string) => nextSort(ctx, activeSort, key) const sum = (slug: string): number | null => { const xs = items.map((r) => r.vals[slug]).filter((v): v is number => v != null) return xs.length ? xs.reduce((a, v) => a + v, 0) : null } const totalShare = share ? sum(share.slug) : null return (
sortFirst('name')} className="folio-sticky" /> {cols.map((c) => ( sortFirst(c)} /> ))} {share && ( sortFirst(share.slug)} /> )} {items.map((r, i) => { const sv = share ? (r.vals[share.slug] ?? null) : null const pct = share && sv != null ? (sv / share.total) * 100 : null return ( setHotFips(r.fips)} onPointerLeave={() => setHotFips(null)} onFocus={() => setHotFips(r.fips)} onBlur={() => setHotFips(null)} > {cols.map((c) => { const v = r.vals[c] ?? null return ( ) })} {share && ( )} ) })} {showTotal && ( {cols.map((c) => { const t = isCount(c) ? sum(c) : null return ( ) })} {share && ( )} )}
{r.name} {ctx.fmt(c, v)} {pct == null ? '—' : `${pct.toFixed(1)} %`}
Total · {items.length} {t == null ? '—' : ctx.fmt(c, t)} {totalShare == null ? '—' : `${((totalShare / share.total) * 100).toFixed(1)} %`}
) }