jacquardSnapshot

← snapshot

1166 bytes
"use client";

import { useRouter } from "next/navigation";
import { useState } from "react";
import { ApiError, api } from "@/lib/api";

/** One click into a woven repo: replays the demo arc server-side. */
export function SeedDemoButton() {
  const router = useRouter();
  const [pending, setPending] = useState(false);
  const [error, setError] = useState<string | null>(null);

  async function seed() {
    setPending(true);
    setError(null);
    try {
      const result = await api.seedDemo();
      router.push(`/repos/${result.repos[0] ?? "meridian-systems"}`);
      router.refresh();
    } catch (e) {
      if (e instanceof ApiError && e.status === 409) {
        // Already seeded — just go there.
        router.push("/repos/meridian-systems");
        return;
      }
      setError(e instanceof ApiError ? e.message : String(e));
    } finally {
      setPending(false);
    }
  }

  return (
    <>
      <button type="button" className="jac-btn" onClick={seed} disabled={pending}>
        {pending ? "Weaving…" : "Seed the demo"}
      </button>
      {error ? <span className="jac-hint jac-hint--error">{error}</span> : null}
    </>
  );
}