"use client"; import { LayoutTemplateIcon, SparklesIcon, WorkflowIcon, WrenchIcon } from "lucide-react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useState, type FormEvent } from "react"; import { toast } from "sonner"; import { EmptyState, ErrorState, PageHeader } from "@/components/shell/page"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Label } from "@/components/ui/label"; import { Skeleton } from "@/components/ui/skeleton"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { Textarea } from "@/components/ui/textarea"; import { api, ApiError } from "@/lib/api"; import { formatDateTime } from "@/lib/format"; import { useI18n } from "@/lib/i18n"; import { useApi } from "@/lib/use-api"; import { issuesFromBody, type IssueReport } from "./dsl"; import { IssueList, WfBadge } from "./parts"; import type { Proposal, Template, WorkflowDetail, WorkflowList } from "./types"; /** POST /workflows; returns the created workflow or throws with the 422 issue report. */ export async function createWorkflow( body: { text: string } | { template: string }, ): Promise<{ ok: true; wf: WorkflowDetail } | { ok: false; report: IssueReport | null; error: Error }> { try { const wf = await api("/workflows", { method: "POST", body }); return { ok: true, wf }; } catch (e) { const err = e instanceof Error ? e : new Error(String(e)); return { ok: false, report: e instanceof ApiError ? issuesFromBody(e.body) : null, error: err }; } } function NewWorkflowForm() { const { t } = useI18n(); const router = useRouter(); const [text, setText] = useState(""); const [busy, setBusy] = useState(false); const [report, setReport] = useState(null); const submit = async (e: FormEvent) => { e.preventDefault(); if (!text.trim()) return; setBusy(true); setReport(null); const res = await createWorkflow({ text: text.trim() }); setBusy(false); if (res.ok) { toast.success(t("workflows.created")); router.push(`/workflows/${res.wf.id}`); } else { setReport(res.report); if (!res.report) toast.error(res.error.message || t("common.error")); } }; return (

{t("workflows.newHint")}