import React, { useEffect, useRef, useState } from 'react'; import dynamic from 'next/dynamic'; const Chart = dynamic(() => import('chart.js/auto').then((mod) => mod.Chart), { ssr: false, }); interface SidebarProps { socket: any; } const Sidebar: React.FC = ({ socket }) => { const [isCollapsed, setIsCollapsed] = useState(false); const chartRefs = useRef<{ [key: string]: any }>({ cpu: null, memory: null, disk: null, gpu: null, gpuMemory: null, }); useEffect(() => { if (socket) { socket.on('system_resources', (data: any) => { updateCharts(data); }); } return () => { if (socket) { socket.off('system_resources'); } }; }, [socket]); useEffect(() => { const initCharts = async () => { const ChartJS = await Chart; initializeCharts(ChartJS); }; initCharts(); return () => { Object.values(chartRefs.current).forEach(chart => chart?.destroy()); }; }, []); const initializeCharts = (ChartJS: any) => { const chartConfig = { type: 'line', options: { responsive: true, maintainAspectRatio: false, scales: { x: { type: 'time', time: { unit: 'second', }, }, y: { beginAtZero: true, max: 100, }, }, animation: false, }, data: { datasets: [{ data: [], borderColor: 'rgb(75, 192, 192)', tension: 0.1, }], }, }; ['cpu', 'memory', 'disk', 'gpu', 'gpuMemory'].forEach(chartName => { const ctx = document.getElementById(`${chartName}Chart`) as HTMLCanvasElement; if (ctx) { chartRefs.current[chartName] = new ChartJS(ctx, chartConfig); } }); }; const updateCharts = (data: any) => { const now = new Date(); Object.entries(data).forEach(([key, value]) => { const chartName = key.replace('_', '').toLowerCase(); const chart = chartRefs.current[chartName]; if (chart) { chart.data.datasets[0].data.push({x: now, y: value}); chart.update('none'); } }); }; return (

CPU Load

Memory Usage

Disk I/O

GPU Load

GPU Memory

); }; export default Sidebar;