import { useState } from "react"; import { useTuskContainers, useStartContainer, useStopContainer, useRemoveContainer, useDockerStatus, } from "@/hooks/use-docker"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { toast } from "sonner"; import { ChevronDown, ChevronRight, Container, Play, Square, Trash2, Loader2, } from "lucide-react"; export function DockerContainersList() { const [expanded, setExpanded] = useState(true); const { data: dockerStatus } = useDockerStatus(); const { data: containers, isLoading } = useTuskContainers(); const startMutation = useStartContainer(); const stopMutation = useStopContainer(); const removeMutation = useRemoveContainer(); const dockerAvailable = dockerStatus?.installed && dockerStatus?.daemon_running; if (!dockerAvailable) { return null; } const handleStart = (name: string) => { startMutation.mutate(name, { onSuccess: () => toast.success(`Container "${name}" started`), onError: (err) => toast.error("Failed to start container", { description: String(err), }), }); }; const handleStop = (name: string) => { stopMutation.mutate(name, { onSuccess: () => toast.success(`Container "${name}" stopped`), onError: (err) => toast.error("Failed to stop container", { description: String(err), }), }); }; const handleRemove = (name: string) => { if ( !confirm( `Remove container "${name}"? This will delete the container and all its data.` ) ) { return; } removeMutation.mutate(name, { onSuccess: () => toast.success(`Container "${name}" removed`), onError: (err) => toast.error("Failed to remove container", { description: String(err), }), }); }; const isRunning = (status: string) => status.toLowerCase().startsWith("up"); return (
setExpanded(!expanded)} > {expanded ? ( ) : ( )} Docker Clones {containers && containers.length > 0 && ( {containers.length} )}
{expanded && (
{isLoading && (
Loading...
)} {containers && containers.length === 0 && (
No Docker clones yet. Right-click a database to clone it.
)} {containers?.map((container) => (
{container.name} {container.source_database && ( {container.source_database} )} :{container.host_port} {isRunning(container.status) ? "running" : "stopped"}
{isRunning(container.status) ? ( ) : ( )}
))}
)}
); }