"use client"; import { useEffect, useState } from "react"; import { useParams, useRouter } from "next/navigation"; import { SignInButton, SignedIn, SignedOut, useAuth } from "@clerk/nextjs"; import { DashboardSidebar } from "@/components/organisms/DashboardSidebar"; import { DashboardShell } from "@/components/templates/DashboardShell"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; const apiBase = process.env.NEXT_PUBLIC_API_URL?.replace(/\/+$/, "") || "http://localhost:8000"; type Agent = { id: string; name: string; }; export default function EditAgentPage() { const { getToken, isSignedIn } = useAuth(); const router = useRouter(); const params = useParams(); const agentIdParam = params?.agentId; const agentId = Array.isArray(agentIdParam) ? agentIdParam[0] : agentIdParam; const [agent, setAgent] = useState(null); const [name, setName] = useState(""); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const loadAgent = async () => { if (!isSignedIn || !agentId) return; setIsLoading(true); setError(null); try { const token = await getToken(); const response = await fetch(`${apiBase}/api/v1/agents/${agentId}`, { headers: { Authorization: token ? `Bearer ${token}` : "" }, }); if (!response.ok) { throw new Error("Unable to load agent."); } const data = (await response.json()) as Agent; setAgent(data); setName(data.name); } catch (err) { setError(err instanceof Error ? err.message : "Something went wrong."); } finally { setIsLoading(false); } }; useEffect(() => { loadAgent(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [isSignedIn, agentId]); const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); if (!isSignedIn || !agentId) return; const trimmed = name.trim(); if (!trimmed) { setError("Agent name is required."); return; } setIsLoading(true); setError(null); try { const token = await getToken(); const response = await fetch(`${apiBase}/api/v1/agents/${agentId}`, { method: "PATCH", headers: { "Content-Type": "application/json", Authorization: token ? `Bearer ${token}` : "", }, body: JSON.stringify({ name: trimmed }), }); if (!response.ok) { throw new Error("Unable to update agent."); } router.push(`/agents/${agentId}`); } catch (err) { setError(err instanceof Error ? err.message : "Something went wrong."); } finally { setIsLoading(false); } }; return (

Sign in to edit agents.

Edit agent

{agent?.name ?? "Agent"}

Status is controlled by agent heartbeat.

setName(event.target.value)} placeholder="e.g. Deploy bot" disabled={isLoading} />
{error ? (
{error}
) : null}
); }