'use client'; import { useState, useEffect } from 'react'; import api from '@/lib/api'; import { Play, Square, RotateCw, RefreshCw, FileText, AlertCircle } from 'lucide-react'; interface ServerConfig { host: string; port: number; username: string; password?: string; key_file?: string; project_path: string; } interface ContainerStatus { name: string; status: string; image: string; } export default function DockerControl({ serverConfig, serverId, projectPath }: { serverConfig: ServerConfig; serverId?: string; projectPath?: string; }) { const [containers, setContainers] = useState>({}); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const fetchStatus = async () => { setLoading(true); setError(null); try { const url = serverId ? '/api/remote/server/docker/status' : '/api/remote/docker/status'; const payload = serverId ? { server_id: serverId, project_path: projectPath } : { ...serverConfig, project_path: projectPath }; const response = await api.post(url, payload); if (response.data.code === 0) { setContainers(response.data.data.containers || {}); } else { setError(response.data.message || '获取状态失败'); } } catch (err: any) { setError(err.response?.data?.message || err.message || '请求失败'); } finally { setLoading(false); } }; useEffect(() => { fetchStatus(); }, [serverId, projectPath]); const handleDockerAction = async (action: 'start' | 'stop' | 'restart', containerName: string) => { setLoading(true); setError(null); try { const url = serverId ? `/api/remote/server/docker/${action}` : `/api/remote/docker/${action}`; const payload = serverId ? { server_id: serverId, container_name: containerName, project_path: projectPath } : { ...serverConfig, container_name: containerName, project_path: projectPath }; const response = await api.post(url, payload); if (response.data.code === 0) { alert('操作成功'); await fetchStatus(); } else { setError(response.data.message || '操作失败'); } } catch (err: any) { setError(err.response?.data?.message || err.message || '操作失败'); } finally { setLoading(false); } }; const handleComposeAction = async (action: 'up' | 'down') => { setLoading(true); setError(null); try { const url = serverId ? `/api/remote/server/docker/${action}` : `/api/remote/docker/${action}`; const payload = serverId ? { server_id: serverId, project_path: projectPath } : { ...serverConfig, project_path: projectPath }; const response = await api.post(url, payload); if (response.data.code === 0) { alert('操作成功'); await fetchStatus(); } else { setError(response.data.message || '操作失败'); } } catch (err: any) { setError(err.response?.data?.message || err.message || '操作失败'); } finally { setLoading(false); } }; return (
{/* 操作栏 */}

Docker 容器管理

{error && (
{error}
)} {/* 容器列表 */}
{/* 桌面端表格视图 */}
{Object.keys(containers).length === 0 ? ( ) : ( Object.entries(containers).map(([name, container]) => ( )) )}
容器名称 状态 镜像 操作
{loading ? '加载中...' : '暂无容器'}
{name} {container.status} {container.image}
{/* 移动端卡片视图 */}
{Object.keys(containers).length === 0 ? (
{loading ? '加载中...' : '暂无容器'}
) : ( Object.entries(containers).map(([name, container]) => (
{name} {container.status}
镜像: {container.image}
)) )}
); }