'use client'; import { useState } from 'react'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Loader2, Rocket } from 'lucide-react'; interface LaunchSwarmDialogProps { projectRoot: string; onSuccess?: () => void; } interface Formula { name: string; description?: string; } export function LaunchSwarmDialog({ projectRoot, onSuccess }: LaunchSwarmDialogProps) { const [open, setOpen] = useState(false); const [loading, setLoading] = useState(false); const [formulas, setFormulas] = useState([]); const [selectedFormula, setSelectedFormula] = useState(''); const [title, setTitle] = useState(''); const [error, setError] = useState(null); const fetchFormulas = async () => { setLoading(true); setError(null); try { const res = await fetch(`/api/swarm/formulas?projectRoot=${encodeURIComponent(projectRoot)}`); const json = await res.json(); if (json.ok) { setFormulas(json.data ?? []); } else { setError(json.error); } } catch { setError('Failed to fetch formulas'); } finally { setLoading(false); } }; const handleOpenChange = (isOpen: boolean) => { setOpen(isOpen); if (isOpen && formulas.length === 0) { fetchFormulas(); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!title || !selectedFormula) return; setLoading(true); setError(null); try { const res = await fetch('/api/swarm/launch', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ projectRoot, title, proto: selectedFormula, }), }); const json = await res.json(); if (json.ok) { setOpen(false); setTitle(''); setSelectedFormula(''); onSuccess?.(); } else { setError(json.error); } } catch { setError('Failed to launch swarm'); } finally { setLoading(false); } }; return ( Launch New Swarm Instantiate a new molecule from a template proto.
setTitle(e.target.value)} className="bg-slate-900 border-slate-700 text-slate-200" placeholder="e.g. Release v1.2" disabled={loading} />
{error && (
{error}
)}
); }