import type { Metadata } from 'next'; import Link from 'next/link'; import { t, tOpt } from '@/i18n'; import { isNotBuilt, safe } from '@/lib/api'; import { apiExplore } from '@/lib/api-explore'; import { compact, formatValue, grouped, isNum } from '@/lib/format'; import { routes } from '@/lib/site'; import type { RegionItem, RegionResponse } from '@/lib/types-explore'; import { NotBuiltState } from '@/components/data/empty-state'; import { Section } from '@/components/data/section'; import { PageHeader } from '@/components/explore/page-header'; export const revalidate = 900; export const metadata: Metadata = { title: t('regions.title'), description: t('regions.sub'), alternates: { canonical: routes.regions() }, }; const KIND_ORDER = ['world', 'region', 'continent', 'income', 'org', 'custom'] as const; export default async function RegionsPage() { let items: RegionItem[] = []; let notBuilt = false; try { items = (await apiExplore.regions()).items; } catch (e) { if (isNotBuilt(e)) notBuilt = true; else throw e; } if (notBuilt) { return ( <> ); } // Third figure (median life expectancy) comes from the group detail — fetched in parallel, cached by ISR. const details = await Promise.all(items.map((g) => safe(apiExplore.region(g.slug ?? g.id)))); const detailById = new Map(items.map((g, i) => [g.id, details[i] ?? null])); const groups = KIND_ORDER.map((kind) => ({ kind, rows: items.filter((g) => (g.kind ?? 'custom') === kind) })).filter((s) => s.rows.length); const other = items.filter((g) => !KIND_ORDER.includes((g.kind ?? 'custom') as (typeof KIND_ORDER)[number])); if (other.length) groups.push({ kind: 'custom', rows: other }); return ( <> {groups.map(({ kind, rows }, gi) => (
    {rows.map((g) => { const d = detailById.get(g.id); const le = d?.aggregates['life-expectancy']; const gdppc = d?.aggregates['gdp-per-capita']; return (
  • {g.name} {t('regions.members', { n: g.n_members ?? d?.n_members ?? 0 })}
    {t('regions.population')} ({t('regions.sum')})
    {isNum(g.population_latest) ? compact(g.population_latest) : t('common.na')}
    {t('regions.gdp')} ({t('regions.sum')})
    {isNum(g.gdp_latest) ? `US$${compact(g.gdp_latest)}` : t('common.na')}
    {le ? `${t('regions.lifeExpectancy')} (${t('regions.median')})` : gdppc ? `${t('common.gdpPerCapita')} (${t('regions.weighted_mean')})` : t('regions.lifeExpectancy')}
    {le ? formatValue(le.value, le.indicator) : gdppc ? formatValue(gdppc.value, gdppc.indicator) : t('common.na')}
  • ); })}
))} ); }