import React, { useState, useEffect, useCallback, useRef } from 'react'; import { Activity, Terminal, Database, ShieldAlert, Cpu, Radio, Zap, Flame, Lock, Server, Layers, ArrowLeft, HardDrive, Globe, Wifi } from 'lucide-react'; import { Incident, IncidentStatus, ExposurePoint } from '../types'; import { geminiService } from '../services/geminiService'; import { datadog, ConfluentStreamClient } from '../services/externalServices'; import ExposureChart from './ExposureChart'; import Orb from './Orb'; const NORMAL_MEAN = 15000; const DRIFT_MEAN = 32000; const STABLE_DEPLOY_URL = "https://silentloss-commander-v2-0-1047953835170.us-west1.run.app"; const generateDataPoint = (mode: 'NORMAL' | 'CHAOS', timeOffset: number = 0): ExposurePoint => { const time = new Date(Date.now() - timeOffset).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit' }); const mean = mode === 'NORMAL' ? NORMAL_MEAN : DRIFT_MEAN; return { time, exposure: mean + Math.random() * 2000 - 1000, threshold: 25000, compliance: mode === 'NORMAL' ? 98 + Math.random() * 2 : 45 + Math.random() * 15, reputation: mode === 'NORMAL' ? 95 + Math.random() * 5 : 60 + Math.random() * 10 }; }; interface DashboardProps { onBack: () => void; } const Dashboard: React.FC = ({ onBack }) => { const [isChaosMode, setIsChaosMode] = useState(false); const [incident, setIncident] = useState(null); const [exposureData, setExposureData] = useState([]); const [isSpeaking, setIsSpeaking] = useState(false); const [auditLog, setAuditLog] = useState<{time: string, msg: string, type: 'info' | 'warn' | 'error', service: string}[]>([]); const [savedLoss, setSavedLoss] = useState(124800); const audioContextRef = useRef(null); const activeSourceRef = useRef(null); const consoleRef = useRef(null); const stopAudio = useCallback(() => { if (activeSourceRef.current) { try { activeSourceRef.current.stop(); } catch (e) {} activeSourceRef.current = null; } setIsSpeaking(false); }, []); const addLog = useCallback((msg: string, type: 'info' | 'warn' | 'error' = 'info', service: string = 'system') => { const time = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit' }); setAuditLog(prev => [{ time, msg, type, service }, ...prev].slice(0, 30)); datadog.log(`[${service}] ${msg}`, type); }, []); useEffect(() => { datadog.init(); const stream = new ConfluentStreamClient(); const sub = stream.subscribe("lending.decisions.v1", (msg) => { if (!isChaosMode) addLog(`Kafka Packet Received (P:${msg.partition} O:${msg.offset})`, 'info', 'confluent'); }); const initial = Array.from({ length: 20 }).map((_, i) => generateDataPoint('NORMAL', (19 - i) * 5000)); setExposureData(initial); return () => { clearInterval(sub); stopAudio(); }; }, [isChaosMode, addLog, stopAudio]); const decodeAndPlay = async (base64Audio: string) => { if (!isChaosMode) return; stopAudio(); if (!audioContextRef.current) audioContextRef.current = new (window.AudioContext || (window as any).webkitAudioContext)({ sampleRate: 24000 }); if (audioContextRef.current.state === 'suspended') await audioContextRef.current.resume(); setIsSpeaking(true); const bytes = Uint8Array.from(atob(base64Audio), c => c.charCodeAt(0)); const dataInt16 = new Int16Array(bytes.buffer); const buffer = audioContextRef.current.createBuffer(1, dataInt16.length, 24000); const channelData = buffer.getChannelData(0); for (let i = 0; i < dataInt16.length; i++) channelData[i] = dataInt16[i] / 32768.0; const source = audioContextRef.current.createBufferSource(); source.buffer = buffer; source.connect(audioContextRef.current.destination); source.onended = () => { setIsSpeaking(false); activeSourceRef.current = null; }; activeSourceRef.current = source; source.start(); }; const triggerDriftAlert = useCallback(async () => { const newIncident: Incident = { id: `INC-${Math.floor(Math.random() * 10000)}`, timestamp: new Date().toISOString(), type: 'Compliance Variance Drift', severity: 'CRITICAL', status: IncidentStatus.ACTIVE, segment: 'TX-CLUSTER / FAIR_LENDING', summary_text: 'Analyzing multi-vector risk...', exposure_delta: 14250.88, impact_vectors: { economic: 88, regulatory: 92, reputation: 65 } }; setIncident(newIncident); addLog("CRITICAL: High-risk variance detected in lending logic", 'error', 'ai-judge'); const analysis = await geminiService.analyzeIncident(`${newIncident.type}. Predicted regulatory fine: $4.2M.`); if (analysis && isChaosMode) { setIncident(prev => prev ? { ...prev, summary_text: analysis } : null); addLog(`Judge Recommendation Rendered`, 'warn', 'gemini-3'); const audioData = await geminiService.speakAnalysis(analysis); if (audioData) await decodeAndPlay(audioData); } }, [addLog, isChaosMode]); useEffect(() => { const interval = setInterval(() => { const mode = isChaosMode ? 'CHAOS' : 'NORMAL'; const newPoint = generateDataPoint(mode); setExposureData(prev => [...prev.slice(1), newPoint]); if (isChaosMode) { setSavedLoss(prev => prev + 450); if (newPoint.exposure > 25000 && !incident) triggerDriftAlert(); } }, 3000); return () => clearInterval(interval); }, [isChaosMode, incident, triggerDriftAlert]); return (

SILENTLOSS COMMANDER v2.0

Economic Integrity Monitor

TACTICAL AUDIT LOG
{auditLog.map((log, i) => (
[{log.time}] [{log.service.toUpperCase()}] {log.msg}
))}

INFRASTRUCTURE MESH

{["Kafka Stream", "Datadog Agent", "Gemini Node", "Policy Engine"].map((n, i) => (
{n}
STABLE
))}
{incident ? (

"{incident.summary_text}"

) : (

Awaiting Variance Signal...

)}
); }; export default Dashboard;