import React, { useState, useEffect } from 'react'; import { RotateCw } from 'lucide-react'; import { IOPoint } from '../types'; import { comms } from '../communication'; interface IOMonitorPageProps { onToggle: (id: number, type: 'input' | 'output') => void; } export const IOMonitorPage: React.FC = ({ onToggle }) => { const [ioPoints, setIoPoints] = useState([]); const [isLoading, setIsLoading] = useState(true); const [activeIOTab, setActiveIOTab] = useState<'in' | 'out'>('in'); // Fetch initial IO list when page mounts useEffect(() => { const fetchIOList = async () => { setIsLoading(true); try { const ioStr = await comms.getIOList(); const ioData: IOPoint[] = JSON.parse(ioStr); setIoPoints(ioData); } catch (e) { console.error('Failed to fetch IO list:', e); } setIsLoading(false); }; fetchIOList(); // Subscribe to real-time IO updates const unsubscribe = comms.subscribe((msg: any) => { if (msg?.type === 'STATUS_UPDATE' && msg.ioState) { setIoPoints(prev => { const newIO = [...prev]; msg.ioState.forEach((update: { id: number, type: string, state: boolean }) => { const idx = newIO.findIndex(p => p.id === update.id && p.type === update.type); if (idx >= 0) newIO[idx] = { ...newIO[idx], state: update.state }; }); return newIO; }); } }); return () => { unsubscribe(); }; }, []); const points = ioPoints.filter(p => p.type === (activeIOTab === 'in' ? 'input' : 'output')); return (
{/* Local Header / Controls */}

SYSTEM I/O MONITOR

TOTAL POINTS: {ioPoints.length}
{isLoading ? (
LOADING IO POINTS...
) : (
{points.map(p => (
onToggle(p.id, p.type)} className={` flex items-center gap-4 px-4 py-3 cursor-pointer transition-all border clip-tech-sm group hover:translate-x-1 ${p.state ? (p.type === 'output' ? 'bg-neon-green/10 border-neon-green text-neon-green shadow-[0_0_15px_rgba(10,255,0,0.2)]' : 'bg-neon-yellow/10 border-neon-yellow text-neon-yellow shadow-[0_0_15px_rgba(255,230,0,0.2)]') : 'bg-slate-900/40 border-slate-800 text-slate-500 hover:border-slate-600 hover:bg-slate-800/40'} `} > {/* LED Indicator */}
{/* ID Badge */}
{p.type === 'input' ? 'I' : 'Q'}{p.id.toString().padStart(2, '0')}
{/* Name */}
{p.name}
))}
)}
); };