"use client"; import { CheckIcon, DownloadIcon, ExternalLinkIcon, EyeIcon, LoaderCircleIcon, SendIcon, Trash2Icon } from "lucide-react"; import { useCallback, useEffect, useState } from "react"; import { toast } from "sonner"; import { CopyButton } from "@/components/copy-button"; import { LocalTime } from "@/components/local-time"; import { Button, buttonVariants } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Checkbox } from "@/components/ui/checkbox"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import type { L80Status, PayloadPreview, PublicationRow } from "@/lib/publications"; type Pub = Omit & { createdAt: string; previewedAt: string | null; confirmedAt: string | null; publishedAt: string | null; }; const RETENTION_DAYS = 5; const textareaClass = "min-h-24 w-full rounded-lg border border-input bg-transparent px-2.5 py-2 text-sm outline-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 dark:bg-input/30"; async function send(url: string, method: string, body?: unknown) { const res = await fetch(url, { method, ...(body ? { headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) } : {}) }); const json = await res.json().catch(() => ({})); if (!res.ok) toast.error(json.error?.message ?? `Request failed (${res.status})`); return res.ok ? json : null; } /** * Publish to launch80 as a double opt-in, the way the L80 docs describe it: first a dry run that * builds the exact payload (nothing is sent), then an explicit confirmation that publishes that file. */ export function PublishCard({ runId, defaultTitle, defaultSummary }: { runId: string; defaultTitle: string; defaultSummary: string }) { const [status, setStatus] = useState(null); const [pubs, setPubs] = useState(null); const [title, setTitle] = useState(defaultTitle.slice(0, 120)); const [summary, setSummary] = useState(defaultSummary.slice(0, 600)); const [preview, setPreview] = useState<{ id: string; data: PayloadPreview | null } | null>(null); const [ack, setAck] = useState(false); const [busy, setBusy] = useState(false); const load = useCallback(async () => { const res = await fetch(`/api/benchmarks/${runId}/publications`, { cache: "no-store" }).catch(() => null); if (!res?.ok) return; const json = (await res.json()) as { status: L80Status; publications: Pub[] }; setStatus(json.status); setPubs(json.publications); }, [runId]); const current = pubs?.find((p) => p.state !== "published") ?? null; const pending = !!pubs?.some((p) => p.state === "preview" || p.state === "publish"); useEffect(() => { const first = setTimeout(load, 0); const t = setInterval(load, pending ? 1_500 : 20_000); return () => { clearTimeout(first); clearInterval(t); }; }, [load, pending]); // Load what the previewed payload contains once its preview is ready. const previewId = current?.state === "previewed" ? current.id : null; useEffect(() => { if (!previewId || preview?.id === previewId) return; let stop = false; void fetch(`/api/benchmarks/${runId}/publications/${previewId}`, { cache: "no-store" }) .then((r) => (r.ok ? r.json() : null)) .then((j) => !stop && setPreview({ id: previewId, data: j?.preview ?? null })); return () => { stop = true; }; }, [previewId, preview?.id, runId]); async function prepare() { setBusy(true); setAck(false); try { if (await send(`/api/benchmarks/${runId}/publications`, "POST", { title: title.trim() || null, summary: summary.trim() || null })) await load(); } finally { setBusy(false); } } async function publish(id: string) { setBusy(true); try { if (await send(`/api/benchmarks/${runId}/publications/${id}`, "POST", { confirm: true })) await load(); } finally { setBusy(false); } } async function discard(id: string) { const res = await fetch(`/api/benchmarks/${runId}/publications/${id}`, { method: "DELETE" }); if (!res.ok) toast.error(`Could not discard (${res.status})`); setPreview(null); setAck(false); await load(); } const published = pubs?.filter((p) => p.state === "published") ?? []; const p = preview?.id === current?.id ? preview?.data : null; return ( Publish to launch80 {pubs === null ? (

Loading…

) : !status?.version ? (

The L80 CLI isn't available in this installation, so runs can't be published.

) : ( <> {published.map((x) => (
Published {x.url} Public until about {x.confirmedBy && ` · confirmed by ${x.confirmedBy}`}
))} {!current && ( <>

Step 1 of 2: prepare a preview. The L80 CLI builds the exact page payload from this run's results.json (endpoint and host removed). Nothing is sent yet.

setTitle(e.target.value)} maxLength={120} />