import { getContent } from './content'; import { getPaperList } from './content-papers'; import { findNode } from './content-tree'; import { getToolTable } from './tool-taxonomy'; import type { PageBlock, Resolved, StatItem } from './blocks'; type Content = Awaited>; type List = Awaited>; /** Fill the dynamic blocks of a page (stats, tables) with live numbers. Static blocks pass through unchanged. */ export async function resolveBlocks(blocks: PageBlock[]): Promise { const contents = new Map>(); const lists = new Map>(); const content = (id: string) => { const n = findNode(id); return contents.get(id) ?? contents.set(id, n?.kind === 'tools' ? getContent(n) : Promise.resolve(null)).get(id)!; }; const list = (id: string) => { const n = findNode(id); return lists.get(id) ?? lists.set(id, n?.kind === 'papers' ? getPaperList(n) : Promise.resolve(null)).get(id)!; }; let tools: ReturnType | null = null; return Promise.all(blocks.map(async (pb): Promise => { const b = pb.block; let resolved: Resolved | undefined; switch (b.type) { case 'stats': { const [c, l] = await Promise.all([content(b.data.node), list(b.data.node)]); const items: StatItem[] = []; for (const it of b.data.items) { const fill = (name: string) => it.label.replace('{name}', name); switch (it.metric) { case 'area-papers': if (c) items.push({ value: c.total, label: fill('') }); break; case 'benchmark-count': if (c) items.push({ value: c.benchmarks.length, label: fill('') }); break; case 'taxonomy-papers': { const t = c?.taxonomy.find((x) => x.id === it.arg); if (t) items.push({ value: t.n, label: fill(t.label), href: it.href ?? `/papers?node=${t.id}` }); break; } case 'group-papers': if (l) items.push({ value: l.total, label: fill('') }); break; case 'group-analysed': if (l) items.push({ value: l.analysed.length, label: fill('') }); break; case 'group-venues': if (l) for (const v of l.byVenue.slice(0, Number(it.arg) || 4)) items.push({ value: v.n, label: fill(v.conf_short) }); break; default: if (it.value !== undefined) items.push({ value: it.value, label: fill(''), href: it.href }); } } resolved = { type: 'stats', items }; break; } case 'tool-table': resolved = { type: 'tool-table', table: await (tools ??= getToolTable()) }; break; case 'paper-table': { const l = await list(b.data.node); resolved = { type: 'paper-table', papers: l?.papers ?? [] }; break; } case 'benchmark-table': { if (b.data.scope === 'areas') resolved = { type: 'benchmark-table', rows: (await content(b.data.node))?.benchmarks ?? [], showNew: true }; else resolved = { type: 'benchmark-table', rows: (await list(b.data.node))?.benchmarks ?? [], showNew: false }; break; } } return resolved ? { ...pb, resolved } : pb; })); }