"use client";
import * as React from "react";
import { BuildPipeline, type PipelineRun, type PipelineStage, type PipelineLog } from "@/components/ui/build-pipeline";
const logs = (messages: string[], error = false): PipelineLog[] => messages.map((message, index) => ({ time: `09:12:${String(index * 2).padStart(2, "0")}`, message, level: error && index === messages.length - 1 ? "error" : "info" }));
const stage = (id: string, name: string, kind: PipelineStage["kind"], durationMs: number, messages: string[]): PipelineStage => ({ id, name, kind, durationMs, status: "passed", logs: logs(messages) });
const successful: PipelineStage[] = [
stage("checkout", "Checkout", "checkout", 2200, ["Fetching main branch", "Resolved commit a1c8e42", "Workspace ready"]),
stage("build", "Build", "build", 24300, ["Installing cached dependencies", "Compiling production bundle", "Checking types", "Build completed: 128 modules"]),
stage("tests", "Tests", "test", 18700, ["Running unit suite", "Verifying keyboard navigation", "Checking mobile layouts", "48 tests passed"]),
stage("deploy", "Deploy", "deploy", 8100, ["Uploading build artifacts", "Promoting preview to production", "Health checks passed", "Deployment ready"]),
];
const demoRuns: PipelineRun[] = [
{ id: "128", branch: "main", commit: "a1c8e42d90", startedAt: "2026-10-08T09:12:00Z", stages: successful },
{ id: "127", branch: "feat/search", commit: "f49b126e11", startedAt: "2026-10-08T08:48:00Z", stages: successful.map(item => item.id === "tests" ? { ...item, status: "failed", durationMs: 6900, logs: logs(["Running unit suite", "47 tests passed, 1 failed", "Search input: expected focus after opening"], true) } : item.id === "deploy" ? { ...item, status: "skipped", durationMs: 0, logs: [] } : item) },
{ id: "126", branch: "preview", commit: "92d671fab3", startedAt: "2026-10-08T08:30:00Z", stages: successful.map(item => item.id === "tests" ? { ...item, status: "running", durationMs: 4100, logs: logs(["Running unit suite", "Checking component interactions…"]) } : item.id === "deploy" ? { ...item, status: "queued", durationMs: 0, logs: [] } : item) },
];
export default function BuildPipelineDemo() {
const [runs, setRuns] = React.useState(demoRuns);
const [activeId, setActiveId] = React.useState("128");
const [replaying, setReplaying] = React.useState<string | null>(null);
React.useEffect(() => {
if (!replaying) return;
const run = runs.find(item => item.id === replaying), snapshot = demoRuns.find(item => item.id === replaying);
if (!run || !snapshot) { setReplaying(null); return; }
const index = run.stages.findIndex(item => item.status === "running");
if (index < 0) { setReplaying(null); return; }
const timer = setTimeout(() => {
const terminal = snapshot.stages[index].status !== "passed";
setRuns(previous => previous.map(item => item.id !== replaying ? item : { ...item, stages: item.stages.map((current, i) => {
if (i === index) return { ...snapshot.stages[i] };
if (terminal && i > index) return { ...snapshot.stages[i] };
if (i === index + 1) return { ...current, status: "running", logs: [{ time: "09:12:00", message: "Starting stage…", level: "info" }] };
return current;
}) }));
if (terminal || index === run.stages.length - 1) setReplaying(null);
}, 850);
return () => clearTimeout(timer);
}, [runs, replaying]);
const replay = (run: PipelineRun) => { setRuns(previous => previous.map(item => item.id !== run.id ? item : { ...item, stages: item.stages.map((current, index) => ({ ...current, status: index === 0 ? "running" : "queued", durationMs: 0, logs: index === 0 ? [{ time: "09:12:00", message: "Starting stage…", level: "info" }] : [] })) })); setReplaying(run.id); };
const cancel = (run: PipelineRun) => { if (replaying === run.id) setReplaying(null); setRuns(previous => previous.map(item => item.id !== run.id ? item : { ...item, stages: item.stages.map(current => current.status === "running" ? { ...current, status: "canceled", logs: [...current.logs, { time: "09:12:00", message: "Canceled by user", level: "warning" }] } : current.status === "queued" ? { ...current, status: "skipped" } : current) })); };
return <div style={{ width: "100%", maxWidth: "800px", margin: "0 auto", padding: "40px 20px" }}><BuildPipeline runs={runs} selectedRunId={activeId} onRunChange={run => setActiveId(run.id)} onRerun={replay} onCancel={cancel} busyRunId={replaying} rerunLabel="Replay" /><p style={{ margin: "12px 0 0", textAlign: "center", fontFamily: "system-ui, sans-serif", fontSize: 11, color: "light-dark(#777, #999)" }}>Sample runs · Replay demonstrates local stage progression</p></div>;
}