Files
0pt/app/portal/page.tsx
T
adminandClaude Opus 4.8 f4fa1c0a5e feat(portal+admin): shared shell, customer portal (dashboard nav, subscriptions, settings) + admin portal (order queue w/ status actions, source registry) + admin/subscriptions APIs
PortalShell (sidebar + session bar via /api/auth/me + logout). Customer: /portal nav, /portal/subscriptions (create feed), /portal/settings (dev-login + DPA). Admin: /admin (metrics + order queue with inline status advance), /admin/sources. APIs: /api/admin/metrics, /api/subscriptions. tsc 0, build green.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-06-24 21:29:27 +02:00

279 lines
9.9 KiB
TypeScript

"use client";
import Link from "next/link";
import { useCallback, useEffect, useState } from "react";
import { Header } from "@/components/header";
import { EmptyState, ErrorState, LoadingSkeleton } from "@/components/states";
import { formatSEK } from "@/lib/data";
// Dashboard "Mina beställningar". Fetches GET /api/orders and renders the
// customer's order ledger. Money arrives in öre; status follows the canonical
// lifecycle (draft → … → in_production → review → accepted → delivered).
interface OrderRow {
order_id: string;
flow: string;
status: string;
total_ore: number;
created_at: string;
}
// Swedish labels for the canonical lifecycle states.
const STATUS_LABEL: Record<string, string> = {
draft: "Utkast",
quoted: "Offererad",
deposit_pending: "Väntar förskott",
in_production: "I produktion",
review: "Granskas",
revision: "Revidering",
accepted: "Godkänd",
delivered: "Levererad",
};
const PRODUCTION_STATES = new Set(["in_production", "review", "revision"]);
const DELIVERED_STATES = new Set(["accepted", "delivered"]);
function fmtDate(iso: string) {
if (!iso) return "—";
const d = new Date(iso);
if (Number.isNaN(d.getTime())) return "—";
return new Intl.DateTimeFormat("sv-SE", { year: "numeric", month: "2-digit", day: "2-digit" }).format(d);
}
function shortId(id: string) {
// Org-friendly truncation for long UUIDs; leave short ids untouched.
return id.length > 12 ? `${id.slice(0, 8)}…` : id;
}
export default function PortalPage() {
const [orders, setOrders] = useState<OrderRow[] | null>(null);
const [error, setError] = useState(false);
const load = useCallback(async () => {
setError(false);
setOrders(null);
try {
const res = await fetch("/api/orders", { cache: "no-store" });
if (!res.ok) throw new Error(String(res.status));
const data = (await res.json()) as OrderRow[];
setOrders(Array.isArray(data) ? data : []);
} catch {
setError(true);
}
}, []);
useEffect(() => {
void load();
}, [load]);
const total = orders?.length ?? 0;
const inProduction = orders?.filter((o) => PRODUCTION_STATES.has(o.status)).length ?? 0;
const delivered = orders?.filter((o) => DELIVERED_STATES.has(o.status)).length ?? 0;
return (
<main className="min-h-screen bg-background text-foreground">
<Header />
<div className="mx-auto max-w-[80rem] px-6">
{/* masthead — oversized numeral as graphic texture */}
<header className="relative border-b border-border py-16 md:py-20">
<span
aria-hidden
className="pointer-events-none absolute -top-6 right-0 select-none font-mono text-[clamp(7rem,18vw,15rem)] font-medium leading-none tracking-[-0.04em] text-foreground/[0.04]"
>
00
</span>
<p className="font-mono text-[11px] uppercase tracking-[0.18em] text-muted-foreground">
Portal — Översikt
</p>
<h1 className="mt-5 max-w-[16ch] text-[clamp(2.6rem,7vw,5.5rem)] font-medium leading-[0.95] tracking-[-0.035em]">
Mina beställningar.
</h1>
<p className="mt-5 font-mono text-sm text-muted-foreground">
Allt på ett ställe.
</p>
</header>
<nav className="flex flex-wrap gap-2 border-b border-border py-4">
{[
["Översikt", "/portal"],
["Prenumerationer", "/portal/subscriptions"],
["Filer", "/portal/files"],
["Integrationer", "/portal/integrationer"],
["Inställningar", "/portal/settings"],
["Admin", "/admin"],
].map(([l, h]) => (
<Link
key={h}
href={h}
className="border border-border px-3 py-1.5 font-mono text-[10.5px] uppercase tracking-[0.1em] text-muted-foreground transition-colors hover:border-foreground hover:text-foreground"
>
{l}
</Link>
))}
</nav>
<Content
orders={orders}
error={error}
total={total}
inProduction={inProduction}
delivered={delivered}
onRetry={load}
/>
</div>
</main>
);
}
function Content({
orders,
error,
total,
inProduction,
delivered,
onRetry,
}: {
orders: OrderRow[] | null;
error: boolean;
total: number;
inProduction: number;
delivered: number;
onRetry: () => void;
}) {
if (error) {
return (
<div className="py-12">
<ErrorState onRetry={onRetry} />
</div>
);
}
if (orders === null) {
return (
<div className="py-12">
<LoadingSkeleton rows={6} />
</div>
);
}
if (orders.length === 0) {
return (
<div className="py-12">
<EmptyState
title="Inga beställningar än."
line="Sätt ihop ett datajobb — marknader, fält, härkomst per fält. Betala hälften för att starta."
ctaHref="/bestall"
ctaLabel="Bygg din första beställning"
/>
</div>
);
}
return (
<>
{/* stats row — shared hairline baseline, no fills */}
<div className="grid grid-cols-1 gap-px border-b border-border bg-border sm:grid-cols-3">
<Stat label="Antal" value={String(total)} />
<Stat label="I produktion" value={String(inProduction)} />
<Stat label="Levererade" value={String(delivered)} />
</div>
{/* orders ledger */}
<section className="border-b border-border py-12">
<div className="mb-6 flex items-baseline justify-between">
<h2 className="font-mono text-[11px] uppercase tracking-[0.14em] text-muted-foreground">
Beställningar
</h2>
<span className="font-mono text-[11px] tabular-nums text-muted-foreground">
{String(total).padStart(2, "0")}
</span>
</div>
{/* desktop table */}
<div className="hidden border border-border md:block">
<div className="grid grid-cols-[1.4fr_1.6fr_1fr_1fr_0.9fr] items-center border-b border-border bg-background px-5 py-3 font-mono text-[10px] uppercase tracking-[0.14em] text-muted-foreground">
<span>ID</span>
<span>Flöde</span>
<span>Status</span>
<span className="text-right">Summa</span>
<span className="text-right">Datum</span>
</div>
{orders.map((o) => (
<Link
key={o.order_id}
href={`/order/${o.order_id}`}
className="group grid grid-cols-[1.4fr_1.6fr_1fr_1fr_0.9fr] items-center border-b border-border px-5 py-4 transition-colors last:border-b-0 hover:bg-secondary/40 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset"
>
<span className="font-mono text-[12px] tabular-nums tracking-tight">
{shortId(o.order_id)}
</span>
<span className="truncate pr-4 text-[15px] tracking-[-0.01em]">{o.flow}</span>
<span className="flex items-center gap-2 font-mono text-[11px] uppercase tracking-[0.08em] text-muted-foreground">
<StatusDot status={o.status} />
{STATUS_LABEL[o.status] ?? o.status}
</span>
<span className="text-right font-mono text-[13px] tabular-nums">
{formatSEK(o.total_ore / 100)}
</span>
<span className="flex items-center justify-end gap-3 text-right font-mono text-[12px] tabular-nums text-muted-foreground">
{fmtDate(o.created_at)}
<span aria-hidden className="text-foreground/30 transition-all duration-300 group-hover:translate-x-1 group-hover:text-foreground">
→
</span>
</span>
</Link>
))}
</div>
{/* mobile stack */}
<div className="flex flex-col border border-border md:hidden">
{orders.map((o) => (
<Link
key={o.order_id}
href={`/order/${o.order_id}`}
className="flex flex-col gap-3 border-b border-border px-4 py-4 transition-colors last:border-b-0 hover:bg-secondary/40 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset"
>
<div className="flex items-baseline justify-between">
<span className="font-mono text-[12px] tabular-nums tracking-tight">{shortId(o.order_id)}</span>
<span className="font-mono text-[12px] tabular-nums">{formatSEK(o.total_ore / 100)}</span>
</div>
<span className="text-[15px] tracking-[-0.01em]">{o.flow}</span>
<div className="flex items-center justify-between font-mono text-[11px] uppercase tracking-[0.08em] text-muted-foreground">
<span className="flex items-center gap-2">
<StatusDot status={o.status} />
{STATUS_LABEL[o.status] ?? o.status}
</span>
<span className="tabular-nums">{fmtDate(o.created_at)}</span>
</div>
</Link>
))}
</div>
</section>
</>
);
}
function Stat({ label, value }: { label: string; value: string }) {
return (
<div className="bg-background px-6 py-8">
<p className="font-mono text-[10.5px] uppercase tracking-[0.14em] text-muted-foreground">{label}</p>
<p className="mt-3 text-[clamp(2.2rem,5vw,3rem)] font-medium leading-none tracking-[-0.03em] tabular-nums">
{value}
</p>
</div>
);
}
/** Square provenance-coloured state marker — no red here (reserved for the EmptyState CTA). */
function StatusDot({ status }: { status: string }) {
const color = DELIVERED_STATES.has(status)
? "var(--prov-3)"
: PRODUCTION_STATES.has(status)
? "var(--prov-2)"
: "var(--muted-foreground)";
return (
<span aria-hidden className="inline-block h-2 w-2 shrink-0" style={{ backgroundColor: color }} />
);
}