import { t } from '@/i18n'; import type { CompareState } from '@/lib/compare-state'; import type { CompareSeries, CompareSnapshotRow, CountryLite } from '@/lib/types-compare'; import { subtopicAnchor } from '@/lib/anchors'; import { CollapsibleGroup, SubtopicJumpNav } from '@/components/data/collapsible-group'; import { CompareChart } from './compare-chart'; import { SnapshotCompact } from './snapshot-table'; /** Above this many charts a topic tab is split into collapsible subtopic blocks (first one expanded). */ const SECTION_THRESHOLD = 8; export function ChartGrid({ rows, countries, state, eager, maxYear }: { rows: CompareSnapshotRow[]; countries: CountryLite[]; state: CompareState; eager: Map; maxYear: number }) { return (
{rows.map((row) => ( ))}
); } /** Group rows by `indicator.subtopic`, preserving first-seen order. */ export function groupBySubtopic(rows: T[]): Array<{ subtopic: string; rows: T[] }> { const m = new Map(); for (const r of rows) { const k = r.indicator.subtopic ?? t('common.other'); const cur = m.get(k); if (cur) cur.push(r); else m.set(k, [r]); } return Array.from(m, ([subtopic, rs]) => ({ subtopic, rows: rs })); } /** * Charts of a compare topic tab. Short tabs → plain grid. Long tabs (> 8 charts, ≥ 2 subtopics) → a jump nav + * one CollapsibleGroup per subtopic: the first expanded (charts), the others collapsed showing the latest * values inline (SnapshotCompact) so nothing is hidden, just deferred. Charts still lazy-mount on scroll. */ export function ChartSections({ rows, countries, slugs, state, eager, maxYear, anchorPrefix }: { rows: CompareSnapshotRow[]; countries: CountryLite[]; slugs: string[]; state: CompareState; eager: Map; maxYear: number; anchorPrefix: string }) { const groups = groupBySubtopic(rows); if (rows.length <= SECTION_THRESHOLD || groups.length < 2) return ; const items = groups.map((g) => ({ id: subtopicAnchor(anchorPrefix, g.subtopic), label: g.subtopic, count: g.rows.length })); return (
{groups.map((g, i) => ( }> ))}
); }