import { useCallback, useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; import { Icon } from "./icon"; import type { ToolInstance } from "../api/sessions"; import { createInstance, deleteInstance, listInstances, restartInstance, startInstance, stopInstance, } from "../api/sessions"; import type { ToolType } from "../api/tool_types"; const API_BASE_URL = import.meta.env.VITE_API_BASE_URL ?? "http://localhost:8000"; interface InstanceListProps { projectId: string; repoId: string; toolTypes: ToolType[]; } export const InstanceList = ({ projectId, repoId, toolTypes }: InstanceListProps) => { const navigate = useNavigate(); const [instances, setInstances] = useState([]); const [loading, setLoading] = useState(false); const [showCreate, setShowCreate] = useState(false); const [selectedToolType, setSelectedToolType] = useState(""); const [displayName, setDisplayName] = useState(""); const [error, setError] = useState(null); const loadInstances = useCallback(async () => { setLoading(true); try { const data = await listInstances(projectId, repoId); setInstances(data); } catch { setError("Failed to load instances"); } finally { setLoading(false); } }, [projectId, repoId]); useEffect(() => { void loadInstances(); }, [loadInstances]); const handleCreate = async () => { if (!selectedToolType) return; setError(null); try { await createInstance(projectId, repoId, selectedToolType, displayName || undefined); setShowCreate(false); setSelectedToolType(""); setDisplayName(""); await loadInstances(); } catch { setError("Failed to create instance"); } }; const handleStart = async (instanceId: string) => { try { await startInstance(projectId, repoId, instanceId); await loadInstances(); } catch { setError("Failed to start instance"); } }; const handleStop = async (instanceId: string) => { try { await stopInstance(projectId, repoId, instanceId); await loadInstances(); } catch { setError("Failed to stop instance"); } }; const handleRestart = async (instanceId: string) => { try { await restartInstance(projectId, repoId, instanceId); await loadInstances(); } catch { setError("Failed to restart instance"); } }; const handleDelete = async (instanceId: string) => { if (!confirm("Are you sure you want to delete this instance?")) return; try { await deleteInstance(projectId, repoId, instanceId); await loadInstances(); } catch { setError("Failed to delete instance"); } }; const getStatusColor = (status: string) => { switch (status) { case "running": return "var(--success)"; case "error": return "var(--danger)"; case "pending": case "building": return "var(--warning)"; default: return "var(--muted)"; } }; return (

Tool Instances

{error && (
{error}
)} {loading ? (

Loading instances...

) : instances.length === 0 ? (

No instances yet. Launch a tool to get started.

) : (
{instances.map((instance) => (
{instance.display_name}
{instance.status}
{instance.status === "running" && instance.url && ( Open )} {instance.status === "running" && ( )} {instance.status !== "running" && ( )} {instance.status === "running" && ( <> )}
))}
)} {showCreate && (

Launch Tool

)}
); };