import React, { useState, useEffect, useCallback } from 'react'; import { Table, Button, Space, Tag, Tooltip, Popconfirm, Empty, Spin, Badge } from 'antd'; import { PlusOutlined, EditOutlined, DeleteOutlined, ReloadOutlined, ApiOutlined, } from '@ant-design/icons'; import axios from 'axios'; import PortCreateModal from './PortCreateModal'; import { designTokens } from '../config/theme'; function PortManagementPanel({ deviceId, deviceName, onRefresh }) { const [ports, setPorts] = useState([]); const [loading, setLoading] = useState(false); const [createModalVisible, setCreateModalVisible] = useState(false); const fetchPorts = useCallback(async () => { if (!deviceId) return; try { setLoading(true); const response = await axios.get(`/api/device-ports/device/${deviceId}`); setPorts(response.data || []); } catch (error) { console.error('获取端口列表失败:', error); } finally { setLoading(false); } }, [deviceId]); useEffect(() => { fetchPorts(); }, [fetchPorts]); const handleDelete = useCallback( async port => { try { await axios.delete(`/api/device-ports/${port.portId}`); import('antd').then(({ message }) => message.success('端口删除成功')); fetchPorts(); onRefresh?.(); } catch (error) { import('antd').then(({ message }) => message.error('端口删除失败')); } }, [fetchPorts, onRefresh] ); const handleCreateSuccess = useCallback(() => { fetchPorts(); onRefresh?.(); }, [fetchPorts, onRefresh]); const getStatusTag = status => { const config = { free: { color: 'success', text: '空闲' }, occupied: { color: 'processing', text: '占用' }, fault: { color: 'error', text: '故障' }, }; const { color, text } = config[status] || { color: 'default', text: status }; return {text}; }; const getTypeTag = type => { const config = { RJ45: { color: 'blue', text: 'RJ45' }, SFP: { color: 'green', text: 'SFP' }, 'SFP+': { color: 'cyan', text: 'SFP+' }, SFP28: { color: 'purple', text: 'SFP28' }, QSFP: { color: 'orange', text: 'QSFP' }, QSFP28: { color: 'red', text: 'QSFP28' }, }; const { color, text } = config[type] || { color: 'default', text: type }; return {text}; }; const columns = [ { title: '端口名称', dataIndex: 'portName', key: 'portName', width: 120, render: text => ( {text} ), }, { title: '类型', dataIndex: 'portType', key: 'portType', width: 90, render: type => getTypeTag(type), }, { title: '速率', dataIndex: 'portSpeed', key: 'portSpeed', width: 80, }, { title: '状态', dataIndex: 'status', key: 'status', width: 80, render: status => getStatusTag(status), }, { title: 'VLAN', dataIndex: 'vlanId', key: 'vlanId', width: 70, render: vlanId => vlanId || '-', }, { title: '操作', key: 'action', width: 100, fixed: 'right', render: (_, record) => ( handleDelete(record)} okText="确定" cancelText="取消" > ), }, ]; const freeCount = ports.filter(p => p.status === 'free').length; const occupiedCount = ports.filter(p => p.status === 'occupied').length; const faultCount = ports.filter(p => p.status === 'fault').length; if (loading) { return (
); } return (
空闲 占用 故障
{ports.length === 0 ? (
该设备暂无端口
} >
) : ( )} setCreateModalVisible(false)} onSuccess={handleCreateSuccess} /> ); } export default React.memo(PortManagementPanel);