import { useState, useEffect } from 'react' import { useParams, useNavigate, Link } from 'react-router-dom' import { api } from '../api/client' import type { ToolInstance, ToolDefinition } from '../types/api' export default function ToolInstanceDetailPage() { const { projectId, instanceId } = useParams<{ projectId: string; instanceId: string }>() const navigate = useNavigate() const [instance, setInstance] = useState(null) const [toolDef, setToolDef] = useState(null) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [actionLoading, setActionLoading] = useState(false) useEffect(() => { if (!projectId || !instanceId) return loadInstance() }, [projectId, instanceId]) const loadInstance = async () => { if (!projectId || !instanceId) return try { setLoading(true) const data = await api.getToolInstance(projectId, instanceId) setInstance(data) const registry = await api.getToolRegistry() const tool = registry.find((t) => t.id === data.tool_definition_id) setToolDef(tool || null) setError(null) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load instance') } finally { setLoading(false) } } const handleStop = async () => { if (!projectId || !instanceId) return setActionLoading(true) try { const data = await api.stopToolInstance(projectId, instanceId) setInstance(data) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to stop instance') } finally { setActionLoading(false) } } const handleStart = async () => { if (!projectId || !instanceId) return setActionLoading(true) try { const data = await api.startToolInstance(projectId, instanceId) setInstance(data) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to start instance') } finally { setActionLoading(false) } } const handleDelete = async () => { if (!projectId || !instanceId) return if (!confirm('Are you sure you want to delete this instance?')) return setActionLoading(true) try { await api.deleteToolInstance(projectId, instanceId) navigate(`/projects/${projectId}`) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to delete instance') } finally { setActionLoading(false) } } const getStatusColor = (status: string) => { switch (status) { case 'running': return 'text-green-400' case 'creating': return 'text-yellow-400' case 'stopped': return 'text-gray-400' case 'error': return 'text-red-400' default: return 'text-gray-400' } } if (loading) { return (
Loading instance...
) } if (error || !instance) { return (
{error || 'Instance not found'}
← Back to project
) } return (
← Back to project

{instance.name}

{instance.status} {toolDef && ( {toolDef.name} )}
{error && (
{error}
)}

Instance Details

Status: {instance.status}
Tool: {toolDef?.name || instance.tool_definition_id}
{instance.subdomain && ( )} {instance.container_id && (
Container: {instance.container_id.slice(0, 12)}
)}
Created: {new Date(instance.created_at).toLocaleString()}

Actions

{instance.status === 'running' && instance.subdomain && ( Open Tool )} {instance.status === 'running' ? ( ) : ( )}
) }