/** * FeaturedCase — single big editorial card pinned at the top of the * homepage. Magazine cover-story treatment. * * Pulls the most recent case_report from disk and renders it as a * full-bleed editorial slab. Hero image is a real declassified page. */ import Link from "next/link"; import { readdir, readFile, stat } from "node:fs/promises"; import path from "node:path"; interface FeaturedCaseData { slug: string; topic: string; topic_pt_br: string | null; opening: string; hero_doc_id: string | null; hero_page: number | null; /** When present, points at /api/static/processing/case-art/.png — * a painterly editorial illustration generated for this case. Prefer * this over the doc-page thumbnail. */ hero_illustration: string | null; } const CASE_ROOT = process.env.CASE_ROOT || "/data/ufo/case"; function parseFrontmatter(md: string): { fm: Record; body: string } { const m = md.match(/^---\n([\s\S]+?)\n---\n([\s\S]*)$/); if (!m) return { fm: {}, body: md }; const fm: Record = {}; for (const line of m[1].split("\n")) { const kv = line.match(/^([a-z_]+):\s*(.+)$/); if (!kv) continue; let v = kv[2].trim(); if (v.startsWith('"') && v.endsWith('"')) v = v.slice(1, -1); fm[kv[1]] = v; } return { fm, body: m[2] }; } function pickOpening(body: string, locale: "pt-br" | "en"): string { const wantPt = locale === "pt-br"; // Look for the first prose paragraph under either the (EN) or (PT-BR) sub-section. const marker = wantPt ? "(PT-BR)" : "(EN)"; const idx = body.indexOf(marker); const slice = idx >= 0 ? body.slice(idx) : body; const m = slice.match(/\n\n([^\n#>|`-][^\n]+(?:\n[^\n#>|`-][^\n]+)*)/); return (m?.[1] ?? "").replace(/\s+/g, " ").trim().slice(0, 500); } function extractFirstDocRef(body: string): { doc_id: string; page: number } | null { // Find the first [[doc-id/pNNN#cNNNN]] reference and return doc/page. const m = body.match(/\[\[([a-z0-9][a-z0-9-]*)\/p(\d{3})/); if (!m) return null; return { doc_id: m[1], page: parseInt(m[2], 10) }; } const UFO_ROOT = process.env.UFO_ROOT || "/data/ufo"; async function illustrationFor(slug: string): Promise { const fs = await import("node:fs/promises"); const p = path.join(UFO_ROOT, "processing", "case-art", `${slug}.png`); try { await fs.stat(p); return `/api/static/processing/case-art/${slug}.png`; } catch { return null; } } async function loadFeatured(locale: "pt-br" | "en"): Promise { const dir = path.join(CASE_ROOT, "reports"); try { const files = await readdir(dir); const items: Array = []; for (const f of files) { if (!f.endsWith(".md")) continue; const full = path.join(dir, f); const md = await readFile(full, "utf-8"); const st = await stat(full); const { fm, body } = parseFrontmatter(md); const docRef = extractFirstDocRef(body); const slug = f.replace(/\.md$/, ""); items.push({ slug, topic: fm.topic ?? f, topic_pt_br: fm.topic_pt_br ?? null, opening: pickOpening(body, locale), hero_doc_id: docRef?.doc_id ?? null, hero_page: docRef?.page ?? null, hero_illustration: await illustrationFor(slug), mtimeMs: st.mtimeMs, }); } items.sort((a, b) => b.mtimeMs - a.mtimeMs); return items[0] ?? null; } catch { return null; } } export async function FeaturedCase({ locale }: { locale: "pt-br" | "en" }) { const c = await loadFeatured(locale); if (!c) return null; const title = locale === "pt-br" ? (c.topic_pt_br ?? c.topic) : c.topic; // Prefer the painterly illustration; fall back to a declassified-page // thumbnail when we haven't generated art for this case yet. const heroImg = c.hero_illustration ?? (c.hero_doc_id && c.hero_page ? `/api/static/processing/png/${c.hero_doc_id}/p-${String(c.hero_page).padStart(3, "0")}.png` : null); const heroIsArt = c.hero_illustration !== null; return (
{/* Editorial text column */}
{locale === "en" ? "Featured case · cover story" : "Caso em destaque · matéria de capa"}

{title}

{c.opening && (

{c.opening}

)}
{locale === "en" ? "Read the case file" : "Ler o caso completo"}
{/* Hero image column */}
{heroImg ? ( // eslint-disable-next-line @next/next/no-img-element {title} ) : (
)}
{heroIsArt ? (
{locale === "en" ? "Editorial illustration" : "Ilustração editorial"}
) : c.hero_doc_id && (
{locale === "en" ? "Source · " : "Fonte · "}{c.hero_doc_id} / p{String(c.hero_page).padStart(3, "0")}
)}
); }