import { useState } from 'react'; import { Wifi, WifiOff, Activity, Clock, Filter, Globe } from 'lucide-react'; import { useWebSocketConnections, useWebSocketStats } from '../hooks/useWebSocketStatus'; import { Card, CardHeader, CardTitle, CardDescription, CardContent, Badge, Skeleton, Alert, } from './ui'; import { formatDistanceToNow } from 'date-fns'; interface WebSocketStatusCardProps { className?: string; showDetails?: boolean; } /** * Component to display WebSocket connection status and statistics */ export function WebSocketStatusCard({ className = '', showDetails = false }: WebSocketStatusCardProps) { const [expanded, setExpanded] = useState(showDetails); const { data: connections, isLoading: connectionsLoading } = useWebSocketConnections(); const { data: stats, isLoading: statsLoading } = useWebSocketStats(); const isLoading = connectionsLoading || statsLoading; if (isLoading) { return ( ); } if (!stats) { return ( Unable to load WebSocket status ); } const hasActiveConnections = stats.total_active > 0; return ( {hasActiveConnections ? ( ) : ( )} WebSocket Connections Real-time connection monitoring {stats.total_active} Active {/* Statistics Grid */} General Logs {stats.logs_connections} Security Logs {stats.cerberus_connections} {/* Oldest Connection */} {stats.oldest_connection && ( Oldest Connection {formatDistanceToNow(new Date(stats.oldest_connection), { addSuffix: true })} )} {/* Connection Details */} {expanded && connections?.connections && connections.connections.length > 0 && ( Active Connections {connections.connections.map((conn) => ( {conn.type === 'logs' ? 'General' : 'Security'} {conn.id.substring(0, 8)}... {conn.remote_addr && ( {conn.remote_addr} )} {conn.filters && ( {conn.filters} )} Connected {formatDistanceToNow(new Date(conn.connected_at), { addSuffix: true })} ))} )} {/* Toggle Details Button */} {connections?.connections && connections.connections.length > 0 && ( setExpanded(!expanded)} className="w-full pt-3 text-sm text-primary hover:text-primary/80 transition-colors" > {expanded ? 'Hide Details' : 'Show Details'} )} {/* No Connections Message */} {!hasActiveConnections && ( No active WebSocket connections )} ); }
{stats.logs_connections}
{stats.cerberus_connections}
{formatDistanceToNow(new Date(stats.oldest_connection), { addSuffix: true })}
Active Connections