import React, { useEffect, useState } from 'react'; import styles from './style/index.module.less'; import { Button, Input, Space, Select, Divider, Table, TableColumnProps, Message, Modal, Notification, Tag, Tooltip } from '@arco-design/web-react'; import { getComponentClassify } from '@/api/componentClassify'; import { IconSearch } from '@arco-design/web-react/icon'; import ContainerModal from './containerModal'; import AddModal from './addModal'; import { getEnvConfigList, deleteEnvConfig, testEnv } from '@/api/componentDeployEnv'; import { startContainer, stopContainer } from '@/api/userContainer'; const Option = Select.Option; const ComponentEnv = () => { const [envType, setEnvType] = useState([]); // 环境类型 const [architectureType, setArchitectureType] = useState(['x86_64', 'aarch64']); // 结构类型 const [visible, setVisible] = useState(false); const [visible1, setVisible1] = useState(false); const [data, setData] = useState([]); const [editingRecord, setEditingRecord] = useState(null); const [selectedEnvType, setSelectedEnvType] = useState(null); // 选中的环境类型 const [selectedArch, setSelectedArch] = useState(null); // 选中的架构类型 const [searchText, setSearchText] = useState(''); // 搜索文本 const [addItem, setAddItem] = useState(null); // 点击环境配置按钮时记录当前信息 const [loadingActions, setLoadingActions] = useState<{ id: number | null; action: string | null }>({ id: null, action: null }); // 按钮loading状态 const columns: TableColumnProps[] = [ { title: '环境IP', dataIndex: 'ip' }, { title: 'docker端口', dataIndex: 'dockerTcpPort' }, { title: '环境类型', dataIndex: 'type' }, { title: '架构类型', dataIndex: 'arch' }, { title: '环境别名', dataIndex: 'name' }, { title: '实例数量', dataIndex: 'instanceCount' }, { title: '用户容器状态', dataIndex: 'isEnable', render: (_, record: any) => (
{record.isEnable === 'NOT_EXIST' && ( 未注册 )} {record.isEnable === 'RUNNING' && ( 运行 )} {record.isEnable === 'STOPPED' && ( 停止 )}
) }, { title: '测试状态', dataIndex: 'available', render: (_, record: any) => (
{record.available == '0' && ( 未测试 )} {record.available == '1' && ( 测试通过 )} {record.available == '-1' && ( 测试失败 )}
) }, { title: '备注', dataIndex: 'description', width: 200, ellipsis: true, render: (text) => ( text ? ( {text} ) : '-' ) }, { title: '操作', width: 450, align: 'center', fixed: 'right', render: (_, record: any) => ( {record.isEnable === 'RUNNING' && ( )} {record.isEnable === 'STOPPED' && ( )} {record.isEnable !== 'RUNNING' && record.available == '1' && ( )} {record.isEnable !== 'RUNNING' && } ) } ]; const handleStop = async (id) => { setLoadingActions({ id, action: 'stop' }); const loadingMessage = Message.loading('正在停止容器,请稍候...'); try { const res: any = await stopContainer(id); loadingMessage(); if (res.code === 200) { Message.success('停止成功'); getEnvList(); } else { Message.error('停止失败: ' + res.message); } } catch (error) { loadingMessage(); Message.error('停止失败: ' + error.message); } finally { setLoadingActions({ id: null, action: null }); } }; const handleStart = async (id) => { setLoadingActions({ id, action: 'start' }); const loadingMessage = Message.loading('正在启动容器,请稍候...'); try { const res: any = await startContainer(id); loadingMessage(); if (res.code === 200) { Message.success('启动成功'); getEnvList(); } else { Message.error('启动失败: ' + res.message); } } catch (error) { loadingMessage(); Message.error('启动失败: ' + error.message); } finally { setLoadingActions({ id: null, action: null }); } }; // 环境测试处理函数 const handleTestEnv = async (record: any) => { setLoadingActions({ id: record.id, action: 'test' }); Message.loading({ content: `正在测试环境 ${record.name}...`, duration: 0 }); try { const res: any = await testEnv(record.id); if (res.code === 200 && res.data) { Notification.success({ closable: false, title: '测试成功', content: `环境 【${record.name}】 测试成功` }); } else { Notification.error({ closable: false, title: '测试失败', content: `环境 【${record.name}】 测试失败: ${res.message}` }); } } finally { Message.clear(); setLoadingActions({ id: null, action: null }); } }; // 环境配置处理函数 const handleConfigEnv = (record: any) => { // 设置编辑记录 setEditingRecord(record); // 打开模态框 setVisible(true); }; // 删除环境处理函数 const handleDeleteEnv = (record: any) => { Modal.confirm({ title: '删除环境', content: (

删除环境后,组件实例将无法继续部署到该环境下。

确认删除环境 {record.name}?

请输入环境名称 【{record.name}】 以确认删除:

), okButtonProps: { status: 'danger' }, onOk: async () => { // 获取用户输入的环境名称 const confirmInput = document.getElementById('confirm-env-name') as HTMLInputElement; if (!confirmInput || confirmInput.value !== record.name) { Message.error('环境名称输入不匹配,请重新输入'); return Promise.reject(); } try { const res: any = await deleteEnvConfig(record.id); if (res.code === 200) { Message.success('环境删除成功'); // 重新加载数据 getEnvList(); } else { Message.error('环境删除失败: ' + res.message); } } catch (error) { Message.error('环境删除失败: ' + error.message); } } }); }; // 获取环境列表,根据选择的类型和架构进行过滤 const getEnvList = async (filterParams?: any) => { // 构造查询参数 const params: any = { current: 1, size: 10 }; // 合并筛选参数 if (filterParams) { Object.assign(params, filterParams); } else { // 如果没有传入参数,使用当前状态值 if (selectedEnvType) params.type = selectedEnvType; if (selectedArch) params.arch = selectedArch; if (searchText) params.name = searchText; } const res: any = await getEnvConfigList(params); if (res.code === 200) setData(res.data.list); }; const getEnvType = async () => { const res: any = await getComponentClassify('docker-env'); if (res.code === 200) setEnvType(res.data); }; useEffect(() => { getEnvType(); getEnvList(); }, []); return ( <>
环境类型:
架构类型:
}>
} placeholder={'搜索'} style={{ width: 236, marginRight: 5 }} value={searchText} onChange={(value) => setSearchText(value)} onPressEnter={() => { // 构建查询参数,包含所有当前筛选条件 const params: any = {}; if (selectedEnvType) params.type = selectedEnvType; if (selectedArch) params.arch = selectedArch; if (searchText) params.name = searchText; getEnvList(params); }} />
{ setEditingRecord(value); getEnvList(); // 新增完成后刷新列表 }} record={editingRecord} /> ); }; export default ComponentEnv;