"use client"; import { useEffect, useState, useRef } from "react"; import { useDictionary } from "@/lib/dictionary"; export default function TenantTelemetry() { const { t } = useDictionary(); const [logs, setLogs] = useState<{ id: string; time: string; action: string; target: string; status: string }[]>([]); const [activeConnections, setActiveConnections] = useState(342); const [bandwidth, setBandwidth] = useState(1.2); const [targetKillId, setTargetKillId] = useState(""); const canvasRef = useRef(null); // WebGL-Style Waveform Simulation using Canvas for Zero-Error absolute performance useEffect(() => { const canvas = canvasRef.current; if (!canvas) return; const ctx = canvas.getContext("2d"); if (!ctx) return; let animationFrameId: number; let time = 0; const resize = () => { canvas.width = canvas.clientWidth; canvas.height = canvas.clientHeight; }; window.addEventListener("resize", resize); resize(); const draw = () => { time += 0.05; ctx.clearRect(0, 0, canvas.width, canvas.height); const width = canvas.width; const height = canvas.height; const centerY = height / 2; // Draw multiple waves for (let i = 0; i < 2; i++) { ctx.beginPath(); ctx.moveTo(0, centerY); for (let x = 0; x < width; x++) { const frequency = 0.015 + (i * 0.005); const amplitude = 20 + (i * 10) + Math.sin(time + x * 0.02) * 5; const y = Math.sin(x * frequency + time + i * 2) * amplitude; ctx.lineTo(x, centerY + y); } // WhatsApp Emerald to Zoom Blue Gradient const gradient = ctx.createLinearGradient(0, 0, width, 0); gradient.addColorStop(0, "rgba(37, 211, 102, 0.5)"); // WhatsApp Green gradient.addColorStop(1, "rgba(11, 92, 255, 0.5)"); // Zoom Blue ctx.strokeStyle = gradient; ctx.lineWidth = 2 + i; ctx.stroke(); } animationFrameId = requestAnimationFrame(draw); }; draw(); return () => { window.removeEventListener("resize", resize); cancelAnimationFrame(animationFrameId); }; }, []); // Fetch real telemetry data from PANOPTICON backend useEffect(() => { const fetchTelemetry = async () => { try { const res = await fetch("/api/admin/telemetry"); if (res.ok) { const data = await res.json(); // Transform quantum logs into UI log format const formattedLogs = data.logs.map((log: any) => ({ id: log.id, time: log.nanoTimestamp ? new Date(log.nanoTimestamp).toISOString().split("T")[1].substring(0, 8) : "00:00:00", action: log.action, target: log.targetId, status: log.action.includes("KILL") ? "TERMINATED" : "OK", })); setLogs(formattedLogs); setActiveConnections(data.stats.totalTelemetryRecords); setBandwidth(parseFloat(data.stats.totalBandwidthBytes) / 1024 / 1024 / 1024); } } catch (err) { console.error("PANOPTICON Sync Error:", err); } }; fetchTelemetry(); const interval = setInterval(fetchTelemetry, 2000); return () => clearInterval(interval); }, []); const executeLiveKill = async () => { if (!targetKillId) return; // Spectactular Kill Trigger Effect document.body.style.animation = "shake 0.5s cubic-bezier(.36,.07,.19,.97) both"; setTimeout(() => document.body.style.animation = "", 500); const res = await fetch("/api/telemetry/live-kill", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ targetType: "USER", targetId: targetKillId, reason: "TENANT_ADMIN_OVERRIDE" }) }); if (res.ok) { setLogs(prev => [{ id: "KILL_" + Date.now(), time: new Date().toISOString().split("T")[1].substring(0, 8), action: "LIVE_KILL_EXECUTE", target: targetKillId, status: "TERMINATED" }, ...prev]); setTargetKillId(""); alert("XCU MoQ Ejection Berhasil. Sesi Staf Dihanguskan."); } else { alert("Akses Ditolak: Hanya Berlaku Untuk Grup Internal."); } }; return (