import { useMemo, useRef } from 'react' import { CENSUS_REGION_LABEL, type CensusRegionId } from '../../utils/censusRegions' import { rowDelayMs } from '../../utils/folioMotion' import { useFlip } from '../../hooks/useFlip' import { barsStory } from '../../utils/matrixStory' import { itemsOf, sortByMetric, statsOf, statsSentence } from './data' import { Empty } from './parts' import SectionShell from './SectionShell' import { delayVar, type MatrixCtx } from './types' const REGIONS = Object.keys(CENSUS_REGION_LABEL) as CensusRegionId[] /** One horizontal bar per geography for the chosen metric; region chips filter the rows (`?region=`). */ export default function BarsSection({ ctx }: { ctx: MatrixCtx }) { const q = ctx.sp.get('region') const region: CensusRegionId | null = REGIONS.includes(q as CensusRegionId) ? (q as CensusRegionId) : null const slug = ctx.metricSlug const scoped = useMemo(() => (region ? ctx.rows.filter((r) => r.region === region) : ctx.rows), [ctx.rows, region]) const rows = useMemo(() => sortByMetric(scoped, slug, ctx.sortDir), [scoped, slug, ctx.sortDir]) const ref = useRef(null) useFlip(ref, `${region ?? 'all'}|${slug}|${ctx.sortDir}|${rows.map((r) => r.fips).join(',')}`, !ctx.reduced) const vals = rows.flatMap((r) => (r.vals[slug] != null ? [r.vals[slug] as number] : [])) const lo = Math.min(0, ...vals) const hi = Math.max(0, ...vals) const span = hi - lo || 1 const st = statsOf(scoped, slug) const scope = region ? `${CENSUS_REGION_LABEL[region]} states` : ctx.geoNoun const story = barsStory({ metricLabel: ctx.metricLabel, vintage: ctx.vintage, scopeLabel: scope, items: itemsOf(scoped, slug), fmt: (v) => ctx.fmt(slug, v) }) return (

One horizontal bar per geography, ordered by {ctx.metricLabel.toLowerCase()} ({ctx.sortDir === 'asc' ? 'lowest' : 'highest'}{' '} first). Pick a region and the bars re-order and grow again; change the metric above and they animate to the new values.

{statsSentence(st, slug, ctx.fmt)}

} controls={
{REGIONS.map((id) => ( ))}
} caption={ <> {ctx.metricLabel}, {scope}, {ctx.vintage}; {rows.length} {rows.length === 1 ? 'row' : 'rows'}. Source: ACS 5-year, {ctx.vintage}. } table={() => ({ columns: [{ label: 'Geography' }, { label: 'Region' }, { label: ctx.metricLabel, numeric: true }], rows: rows.map((r) => [r.name, r.region ? CENSUS_REGION_LABEL[r.region] : '—', ctx.fmt(slug, r.vals[slug] ?? null)]), })} > {!rows.length || !vals.length ? ( No data available for {ctx.metricLabel} in {scope}, {ctx.vintage}. ) : ( )}
) }