import type { Metadata } from 'next'; import { t } from '@/i18n'; import { isNotBuilt, safe } from '@/lib/api'; import { apiAnalytics } from '@/lib/api-analytics'; import { apiExplore } from '@/lib/api-explore'; import { routes } from '@/lib/site'; import { NotBuiltState } from '@/components/data/empty-state'; import { DEFAULT_TRAJ, explorerIndicators, firstParam, parseYearParam, slugParam, type SP } from '@/components/explorer/options'; import { ScatterView } from '@/components/explorer/scatter-view'; export const revalidate = 900; function parseState(sp: SP) { const log = (firstParam(sp, 'log') ?? '').toLowerCase(); const country = (firstParam(sp, 'country') ?? '').toLowerCase(); return { x: slugParam(firstParam(sp, 'x'), DEFAULT_TRAJ.x)!, y: slugParam(firstParam(sp, 'y'), DEFAULT_TRAJ.y)!, size: slugParam(firstParam(sp, 'size'), DEFAULT_TRAJ.size)!, year: parseYearParam(firstParam(sp, 'year')), group: slugParam(firstParam(sp, 'group'), 'world')!, log: /^(x|y|x,y|y,x|none)$/.test(log) ? log : null, fit: firstParam(sp, 'fit') === '1', country: /^[a-z]{3}$/.test(country) ? country : null, }; } export async function generateMetadata({ searchParams }: { searchParams: Promise }): Promise { const sp = await searchParams; return { title: t('scatter.metaTitle'), description: t('scatter.description'), alternates: { canonical: routes.scatter() }, robots: Object.keys(sp).length ? { index: false, follow: true } : undefined, openGraph: { title: `${t('scatter.metaTitle')} — ${t('site.name')}`, description: t('scatter.description'), url: routes.scatter(), type: 'website' }, }; } export default async function ScatterPage({ searchParams }: { searchParams: Promise }) { const state = parseState(await searchParams); let indicatorsRes; try { indicatorsRes = await apiExplore.indicators({ with_data: true }); } catch (e) { if (isNotBuilt(e)) return ; throw e; } const logX = state.log == null ? null : state.log.includes('x'); const logY = state.log == null ? null : state.log.includes('y'); const [groupsRes, scatter, related] = await Promise.all([ safe(apiExplore.regions()), safe(apiAnalytics.scatter({ x: state.x, y: state.y, size: state.size, year: state.year, group: state.group !== 'world' ? state.group : null, log_x: logX == null ? null : String(logX), log_y: logY == null ? null : String(logY) })), safe(apiAnalytics.indicatorRelated(state.x, { limit: 8 })), ]); return ; }