"use client"; import { useEffect, useMemo, useState } from "react"; import { Header } from "@/components/header"; import { useT } from "@/components/locale-provider"; import { useReveal } from "@/hooks/use-reveal"; import { CellProvenance, PROVENANCE_LEGEND } from "@/components/provenance-badge"; import { LoadingSkeleton, EmptyState, ErrorState } from "@/components/states"; import type { ProvLevel } from "@/lib/data"; // Shape of GET /api/sources (docs/04). Legal sourcing only — the transparency moat. interface Source { source_id: string; name: string; market: string; access: string; // api | bulk | authorized_web | licensed licence: string; cadence: string; // daily | weekly | quarterly | ... confidence: number; // 0..1 } // Human, terse labels for the access ladder (CLAUDE.md §1 priority order). const ACCESS_LABEL: Record = { api: { en: "Official API", sv: "Officiellt API" }, bulk: { en: "Bulk / open data", sv: "Bulk / öppen data" }, authorized_web: { en: "Authorized web", sv: "Auktoriserad webb" }, licensed: { en: "Licensed partner", sv: "Licensierad partner" }, }; // Confidence (0..1) → provenance level so each row carries an honest square. function confidenceLevel(c: number): ProvLevel { if (c >= 0.95) return 3; if (c >= 0.9) return 2; if (c > 0) return 1; return 0; } // Sortable columns mapped to the request: Source · Market · Access · Licence · Freshness · Confidence. type SortKey = "name" | "market" | "access" | "licence" | "cadence" | "confidence"; const COLUMNS: { key: SortKey; label: { en: string; sv: string }; mono?: boolean }[] = [ { key: "name", label: { en: "Source", sv: "Källa" } }, { key: "market", label: { en: "Market", sv: "Marknad" }, mono: true }, { key: "access", label: { en: "Access", sv: "Åtkomst" }, mono: true }, { key: "licence", label: { en: "Licence", sv: "Licens" } }, { key: "cadence", label: { en: "Freshness", sv: "Färskhet" }, mono: true }, { key: "confidence", label: { en: "Confidence", sv: "Konfidens" }, mono: true }, ]; const ALL = "__all__"; const ALL_LABEL = { en: "All", sv: "Alla" }; export default function SourcesPage() { const t = useT(); const [sources, setSources] = useState(null); const [status, setStatus] = useState<"loading" | "ready" | "error">("loading"); const [reloadKey, setReloadKey] = useState(0); const [market, setMarket] = useState(ALL); const [access, setAccess] = useState(ALL); const [sort, setSort] = useState<{ key: SortKey; dir: 1 | -1 }>({ key: "confidence", dir: -1 }); const ref = useReveal(); useEffect(() => { let cancelled = false; setStatus("loading"); fetch("/api/sources") .then((r) => { if (!r.ok) throw new Error(String(r.status)); return r.json(); }) .then((data: Source[]) => { if (cancelled) return; setSources(Array.isArray(data) ? data : []); setStatus("ready"); }) .catch(() => { if (cancelled) return; setSources(null); setStatus("error"); }); return () => { cancelled = true; }; }, [reloadKey]); const markets = useMemo(() => { if (!sources) return [ALL]; return [ALL, ...Array.from(new Set(sources.map((s) => s.market)))]; }, [sources]); const accesses = useMemo(() => { if (!sources) return [ALL]; return [ALL, ...Array.from(new Set(sources.map((s) => s.access)))]; }, [sources]); const rows = useMemo(() => { if (!sources) return []; const filtered = sources.filter( (s) => (market === ALL || s.market === market) && (access === ALL || s.access === access), ); const { key, dir } = sort; return [...filtered].sort((a, b) => { const av = a[key]; const bv = b[key]; if (typeof av === "number" && typeof bv === "number") return (av - bv) * dir; return String(av).localeCompare(String(bv), "sv") * dir; }); }, [sources, market, access, sort]); const setSortKey = (key: SortKey) => setSort((p) => (p.key === key ? { key, dir: (p.dir * -1) as 1 | -1 } : { key, dir: key === "confidence" ? -1 : 1 })); const Pill = ({ active, onClick, children, }: { active: boolean; onClick: () => void; children: React.ReactNode; }) => ( ); return (
{/* masthead — oversized faint numeral as graphic texture */}
05

{t({ en: "Source map", sv: "Källkarta" })}

{t({ en: "Every field.", sv: "Varje fält." })}
{t({ en: "Every source.", sv: "Varje källa." })}

{t({ en: "Official first. Then open data. Then licence. Never guessed.", sv: "Officiellt först. Sen öppen data. Sen licens. Aldrig gissat." })}

{/* legend — the 4 provenance squares */}
{t({ en: "Provenance", sv: "Härkomst" })} {PROVENANCE_LEGEND.map((l) => ( {t(l.text)} ))}
{/* filters — pills by Market / Access */} {status === "ready" && sources && sources.length > 0 ? (

{t({ en: "Market", sv: "Marknad" })}

{markets.map((m) => ( setMarket(m)}> {m === ALL ? t(ALL_LABEL) : m === "global" ? "Global" : m} ))}

{t({ en: "Access type", sv: "Typ av åtkomst" })}

{accesses.map((a) => ( setAccess(a)}> {a === ALL ? t(ALL_LABEL) : ACCESS_LABEL[a] ? t(ACCESS_LABEL[a]) : a} ))}
) : null} {/* states + table */} {status === "loading" ? : null} {status === "error" ? ( setReloadKey((k) => k + 1)} /> ) : null} {status === "ready" && (!sources || sources.length === 0) ? ( ) : null} {status === "ready" && sources && sources.length > 0 && rows.length === 0 ? ( ) : null} {status === "ready" && rows.length > 0 ? (
{COLUMNS.map((c) => { const isSorted = sort.key === c.key; return ( ); })} {rows.map((s) => { const level = confidenceLevel(s.confidence); return ( ); })}
{s.name} {s.source_id} {s.market === "global" ? "Global" : s.market} {ACCESS_LABEL[s.access] ? t(ACCESS_LABEL[s.access]) : s.access} {s.licence} {s.cadence} {Math.round(s.confidence * 100)}%

{rows.length} {t({ en: "sources · every field carries its provenance", sv: "källor · varje fält bär sin härkomst" })}

{t({ en: "Licence terms verified before use", sv: "Licensvillkor verifieras innan användning" })}

) : null}
); }