import { useMemo, useRef, useState, type ReactNode } from 'react'
import { SectionContext, useInViewOnce } from './hooks'
import TitleBlock from './TitleBlock'
import ScreenPager from './ScreenPager'
import { CHAPTERS, SECTION_EYEBROW } from '../../utils/matrixStory'
import { SECTIONS, type MatrixCtx, type SectionId, type TableSpec } from './types'
/** A real
of the rows and values a chart draws (same scope, same formatting). The first column is the row header. */
export function DataTable({ id, caption, spec }: { id: string; caption: string; spec: TableSpec }) {
return (
{caption}
{spec.columns.map((c, i) => (
{c.label}
))}
{spec.rows.map((r, ri) => (
{r.map((cell, ci) =>
ci === 0 ? (
{cell}
) : (
{cell}
),
)}
))}
{spec.rows.length === 0 &&
No data available for this view.
}
)
}
interface Props {
ctx: MatrixCtx
id: SectionId
/** The section's story sentence (matrixStory.ts). It rises line by line (80 ms / 160 ms stagger) the first time it enters view. */
headline: string
/** One supporting sentence under the rule, or null to leave it out. */
lead?: string | null
/** Short text column: how to read the figure, with real numbers from the data in scope. */
text: ReactNode
/** Per-section controls (above the figure). */
controls?: ReactNode
/** The figure's caption, including the scope. */
caption: ReactNode
/** The rows the chart draws, for "Show data table". Evaluated only while the table is open. */
table?: () => TableSpec | null
/** Put the figure full width under the text (the Table and Heatmap sections). */
wide?: boolean
children: ReactNode
}
/**
* One screen of the report: a plain eyebrow, the headline and one supporting sentence on the left (the longer explanation sits
* under "More about this chart"), the figure with its caption on the right (about 45 / 55 with a hairline between), and the
* Continue / Back controls under the text. Stacked text-first below 1024 px. Each screen is sized to the viewport (folio.css).
*/
export default function SectionShell({ ctx, id, headline, lead, text, controls, caption, table, wide, children }: Props) {
const ref = useRef(null)
const seen = useInViewOnce(ref)
const [el, setEl] = useState(null)
const name = SECTIONS.find((s) => s.id === id)?.name ?? id
const opens = CHAPTERS.some((c) => c.sections[0] === id)
const open = ctx.sp.get('table') === id
const spec = useMemo(() => (open && table ? table() : null), [open, table])
return (
{
const holder = ref as { current: HTMLElement | null }
holder.current = n
if (n !== el) setEl(n)
}}
className={`folio-section${seen ? ' is-in' : ''}${wide ? ' folio-section-wide' : ''}${opens ? ' has-opener' : ''}`}
aria-labelledby={`${id}-h`}
data-section={id}
>