"use client" import { use, useEffect, useState } from "react" import { useRouter } from "next/navigation" import { Loader2 } from "lucide-react" import { AutomationBuilder, fromServerSteps, type BuilderInitial, type ServerStepNode, } from "@/components/automations/automation-builder" import type { AutomationTriggerType } from "@/types" export default function EditAutomationPage({ params, }: { params: Promise<{ id: string }> }) { const { id } = use(params) const router = useRouter() const [initial, setInitial] = useState(null) const [error, setError] = useState(null) useEffect(() => { let cancelled = false async function load() { const res = await fetch(`/api/automations/${id}`) if (!res.ok) { if (!cancelled) setError(`Failed to load (${res.status})`) return } const body = await res.json() if (cancelled) return setInitial({ id: body.automation.id, name: body.automation.name ?? "", description: body.automation.description ?? "", trigger_type: body.automation.trigger_type as AutomationTriggerType, trigger_config: body.automation.trigger_config ?? {}, is_active: !!body.automation.is_active, steps: fromServerSteps((body.steps ?? []) as ServerStepNode[]), }) } load() return () => { cancelled = true } }, [id]) if (error) { return (

{error}

) } if (!initial) { return (
) } return }