import React, { useState, useEffect, useRef } from 'react'; import { motion } from 'framer-motion'; import { Activity, X, Server, Database, TrendingUp, AlertTriangle } from 'lucide-react'; import { useToast } from '@/contexts/ToastContext'; import { useUserStore } from '@/store/userStore'; interface LiveData { timestamp: string; total_rows?: number; rows_per_sec?: number; cpu_usage?: number; error_rate?: number; connector_source: string; status: string; } interface Props { source: string; connectionId: string; onClose: () => void; } export const LiveStreamingDashboard: React.FC = ({ source, connectionId, onClose }) => { const { isDark } = useUserStore(); const [dataStream, setDataStream] = useState([]); const [isConnected, setIsConnected] = useState(false); const [connectionError, setConnectionError] = useState(''); const ws = useRef(null); const toast = useToast(); useEffect(() => { const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; const host = window.location.host; const wsUrl = `${protocol}//${host}/api/v1/ws/live-data/${connectionId}`; let reconnectTimer: ReturnType | undefined; let disposed = false; const connectWs = () => { try { ws.current = new WebSocket(wsUrl); ws.current.onopen = () => { setIsConnected(true); toast.success(`Connected to ${source} Live Stream`); }; ws.current.onmessage = (event) => { try { const data = JSON.parse(event.data); if (data.error) { setConnectionError(data.error); setIsConnected(false); return; } const entry: LiveData = { timestamp: new Date().toISOString(), total_rows: data.total_rows, rows_per_sec: data.rows_per_sec, cpu_usage: data.cpu_usage, error_rate: data.error_rate, connector_source: source, status: data.status || 'OK' }; setDataStream(prev => [...prev.slice(-99), entry]); } catch (e) { console.error('Failed to parse WS data', e); } }; ws.current.onerror = () => { setConnectionError('Connection error. Retrying...'); }; ws.current.onclose = () => { setIsConnected(false); if (!disposed) reconnectTimer = setTimeout(connectWs, 3000); }; } catch (e) { setConnectionError('Failed to establish WebSocket'); } }; connectWs(); return () => { disposed = true; if (reconnectTimer) clearTimeout(reconnectTimer); ws.current?.close(); }; }, [source, connectionId]); const latestData = dataStream.length > 0 ? dataStream[dataStream.length - 1] : null; // Theme const bg = isDark ? 'bg-[#111]' : 'bg-white'; const bgHeader = isDark ? 'bg-[#1a1a1a]' : 'bg-gray-50'; const border = isDark ? 'border-gray-800' : 'border-gray-200'; const textPrimary = isDark ? 'text-white' : 'text-gray-900'; const textSecondary = isDark ? 'text-gray-400' : 'text-gray-600'; const textMuted = isDark ? 'text-gray-500' : 'text-gray-400'; const cardBg = isDark ? 'bg-[#1a1a1a]' : 'bg-gray-50'; const logBg = isDark ? 'bg-black' : 'bg-gray-900'; return (
{/* Header */}

{source} Live Stream {isConnected && }

{isConnected ? 'Receiving real-time telemetry' : connectionError ? connectionError : 'Connecting...'}

{/* Dashboard Content */}
{source === 'DataVision API Push' && (

API Push is Active!

Your unique Push URL:

{`${window.location.protocol}//${window.location.host}/api/v1/push/${connectionId}`}

Run your generated Python script locally. Data will appear here in real-time.

)} {/* Top KPIs */}
{[ { icon: Database, label: 'Total Rows', value: latestData?.total_rows?.toLocaleString() ?? '--' }, { icon: TrendingUp, label: 'Rows Added/sec', value: latestData?.rows_per_sec?.toLocaleString() ?? '--' }, { icon: Server, label: 'CPU Usage (Est)', value: latestData?.cpu_usage !== undefined ? `${latestData.cpu_usage.toFixed(1)}%` : '--' }, { icon: AlertTriangle, label: 'Error Rate', value: latestData?.error_rate !== undefined ? `${latestData.error_rate.toFixed(2)}%` : '--', isError: latestData && (latestData.error_rate ?? 0) > 1.5 } ].map((kpi, i) => (
{kpi.label}
{kpi.value}
))}
{/* Terminal / Log View — always dark for terminal feel */}
Live Event Log (Agentic Feed)
{[...dataStream].reverse().map((data, idx) => (
[{new Date(data.timestamp).toLocaleTimeString()}] {data.status === 'Anomaly Detected' ? `CRITICAL: Anomaly detected! CPU: ${data.cpu_usage}%, Errors: ${data.error_rate}%. Agent Swarm deployed for auto-remediation.` : `INFO: Telemetry OK. Rows: ${data.total_rows ?? '--'}, Velocity: ${data.rows_per_sec ?? '--'}/s`}
))} {dataStream.length === 0 && (
Waiting for telemetry data...
)}
); };