"use client"; import Link from "next/link"; import { useParams, useSearchParams } from "next/navigation"; import { useEffect, useRef, useState } from "react"; import { toast } from "sonner"; import { OrderStepper } from "@/components/order-stepper"; import { useT } from "@/components/locale-provider"; import { useReveal } from "@/hooks/use-reveal"; import { getLocalOrder } from "@/lib/order-client"; const STAGES: { key: string; title: { en: string; sv: string }; meta: { en: string; sv: string } }[] = [ { key: "source", title: { en: "Source race", sv: "Källracing" }, meta: { en: "GLEIF · Brreg · CVR · PRH", sv: "GLEIF · Brreg · CVR · PRH" } }, { key: "extract", title: { en: "Extraction & OCR", sv: "Extraktion & OCR" }, meta: { en: "register-API · documents", sv: "register-API · dokument" } }, { key: "structure", title: { en: "Structuring & matching", sv: "Strukturering & matchning"}, meta: { en: "NER · entity resolution · LEI-link", sv: "NER · entity resolution · LEI-länk" } }, { key: "enrich", title: { en: "Enrichment & fusion", sv: "Berikning & fusion" }, meta: { en: "multi-source agreement", sv: "multi-source agreement" } }, { key: "quality", title: { en: "Quality control", sv: "Kvalitetskontroll" }, meta: { en: "expectations · dedup · threshold", sv: "expectations · dedup · tröskel" } }, ]; const MARKET_KEYS = [ { code: "SE", speed: 1.0 }, { code: "NO", speed: 0.85 }, { code: "DK", speed: 0.7 }, { code: "FI", speed: 0.5 }, ]; function prefersReduced() { return typeof window !== "undefined" && window.matchMedia?.("(prefers-reduced-motion: reduce)").matches; } export default function ProductionPage() { const t = useT(); const { id } = useParams<{ id: string }>(); const searchParams = useSearchParams(); const reveal = useReveal(); const [running, setRunning] = useState(false); const [done, setDone] = useState(false); const [progress, setProgress] = useState([0, 0, 0, 0, 0]); const [markets, setMarkets] = useState([0, 0, 0, 0]); const [conf, setConf] = useState(0); const [feed, setFeed] = useState<{ t: string; msg: string; kind: "ok" | "run" }[]>([]); const timer = useRef | null>(null); const tick = useRef(0); const [order, setOrder] = useState<{ markets: string[]; volume: number } | null>(null); useEffect(() => { if (searchParams.get("deposit") === "success") { toast.success(t({ en: "Deposit received. Production has started.", sv: "Förskott mottaget. Produktionen har startat." })); } // eslint-disable-next-line react-hooks/exhaustive-deps }, []); useEffect(() => { const fromLocal = () => { const c = getLocalOrder(id)?.config as { markets?: string[]; volume?: number } | undefined; if (c) setOrder({ markets: c.markets ?? [], volume: c.volume ?? 0 }); }; fetch(`/api/orders/${id}`) .then((r) => (r.ok ? r.json() : null)) .then((o) => { const c = o?.config as { markets?: string[]; volume?: number } | undefined; if (c) setOrder({ markets: c.markets ?? [], volume: c.volume ?? 0 }); else fromLocal(); }) .catch(fromLocal); }, [id]); const finish = () => { if (timer.current) { clearInterval(timer.current); timer.current = null; } setProgress([100, 100, 100, 100, 100]); setMarkets([100, 100, 100, 100]); setConf(94); setDone(true); setRunning(false); setFeed((f) => [{ t: stamp(), msg: t({ en: "Delivered. 94% confidence. 4,980 records.", sv: "Levererad. 94 % konfidens. 4 980 poster." }), kind: "ok" }, ...f]); }; const stamp = () => { const s = 10 + Math.floor(tick.current / 2); return `00:${String(s).padStart(2, "0")}`; }; const start = () => { if (running || done) return; if (prefersReduced()) { finish(); return; } setRunning(true); setFeed([{ t: "00:00", msg: t({ en: `Order ${id}. Deposit received.`, sv: `Order ${id}. Depå mottagen.` }), kind: "run" }]); timer.current = setInterval(() => { tick.current += 1; setProgress((prev) => { const next = [...prev]; const i = next.findIndex((p) => p < 100); if (i === -1) return prev; next[i] = Math.min(100, next[i] + 8 + Math.random() * 10); if (next[i] >= 100 && i < STAGES.length) { setFeed((f) => [{ t: stamp(), msg: t({ en: `${STAGES[i].title.en}. Done.`, sv: `${STAGES[i].title.sv}. Klar.` }), kind: "ok" as const }, ...f].slice(0, 12)); } return next; }); setMarkets((prev) => prev.map((m, idx) => Math.min(100, m + (6 + Math.random() * 6) * MARKET_KEYS[idx].speed))); setConf((c) => Math.min(94, c + 2.5 + Math.random() * 2)); }, 320); }; useEffect(() => { if (progress.every((p) => p >= 100) && running) finish(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [progress, running]); useEffect(() => () => { if (timer.current) clearInterval(timer.current); }, []); const overall = Math.round(progress.reduce((a, b) => a + b, 0) / STAGES.length); const status = done ? t({ en: "Delivered", sv: "Levererad" }) : running ? `${overall} %` : t({ en: "Waiting", sv: "Väntar" }); return (
{/* masthead — oversized numeral as graphic texture */}
03

{t({ en: "Step 03 — Production", sv: "Steg 03 — Produktion" })}

{t({ en: "Watch it happen.", sv: "Se det hända." })}

{order ? `${order.markets.join(" · ") || "—"} · ${new Intl.NumberFormat("sv-SE").format(order.volume)} ${t({ en: "records", sv: "poster" })}` : t({ en: "Live. Source by source.", sv: "Live. Källa för källa." })}

{/* control row — single shared baseline */}
{t({ en: "Order", sv: "Order" })} {id} {t({ en: "Status", sv: "Status" })} {status} {t({ en: "ETA", sv: "ETA" })} {t({ en: "≈ 4 days", sv: "≈ 4 dgr" })}
{/* stages — numbered ledger, hairline-separated */}
{STAGES.map((s, i) => { const p = Math.round(progress[i]); const isDone = p >= 100; const isRun = !isDone && (i === 0 || progress[i - 1] >= 100) && running; const accent = isDone ? "var(--prov-3)" : isRun ? "var(--accent-red)" : "var(--muted-foreground)"; return (
{/* state marker rail */}
{String(i + 1).padStart(2, "0")} {t(s.title)}
{p}%

{t(s.meta)}

); })}
{/* right column — gauge, markets, feed */}
{done ? (
{t({ en: "Review the result", sv: "Granska resultatet" })} →
) : null}
); } function ConfidenceRing({ value, unitLabel }: { value: number; unitLabel: string }) { const r = 52; const c = 2 * Math.PI * r; const off = c - (value / 100) * c; return ( {Math.round(value)} {unitLabel} ); }