"use client"; import React, { useState, useEffect, useRef, useMemo } from "react"; import { motion, AnimatePresence } from "framer-motion"; import { Plane, AlertTriangle, Activity, Satellite, Cctv, ChevronDown, ChevronUp, Ship, Eye, Anchor, Settings, Sun, Moon, BookOpen, Radio, Play, Pause, Globe, Flame, Wifi, Server, Shield, ToggleLeft, ToggleRight } from "lucide-react"; import packageJson from "../../package.json"; import { useTheme } from "@/lib/ThemeContext"; function relativeTime(iso: string | undefined): string { if (!iso) return ""; const diff = Date.now() - new Date(iso + "Z").getTime(); if (diff < 0) return "now"; const sec = Math.floor(diff / 1000); if (sec < 60) return `${sec}s ago`; const min = Math.floor(sec / 60); if (min < 60) return `${min}m ago`; const hr = Math.floor(min / 60); if (hr < 24) return `${hr}h ago`; return `${Math.floor(hr / 24)}d ago`; } // Map layer IDs to freshness keys from the backend source_timestamps dict const FRESHNESS_MAP: Record = { flights: "commercial_flights", private: "private_flights", jets: "private_jets", military: "military_flights", tracked: "military_flights", earthquakes: "earthquakes", satellites: "satellites", ships_important: "ships", ships_civilian: "ships", ships_passenger: "ships", ukraine_frontline: "frontlines", global_incidents: "gdelt", cctv: "cctv", gps_jamming: "commercial_flights", kiwisdr: "kiwisdr", firms: "firms_fires", internet_outages: "internet_outages", datacenters: "datacenters", }; // POTUS fleet ICAO hex codes for client-side filtering const POTUS_ICAOS: Record = { 'ADFDF8': { label: 'Air Force One (82-8000)', type: 'AF1' }, 'ADFDF9': { label: 'Air Force One (92-9000)', type: 'AF1' }, 'ADFEB7': { label: 'Air Force Two (98-0001)', type: 'AF2' }, 'ADFEB8': { label: 'Air Force Two (98-0002)', type: 'AF2' }, 'ADFEB9': { label: 'Air Force Two (99-0003)', type: 'AF2' }, 'ADFEBA': { label: 'Air Force Two (99-0004)', type: 'AF2' }, 'AE4AE6': { label: 'Air Force Two (09-0015)', type: 'AF2' }, 'AE4AE8': { label: 'Air Force Two (09-0016)', type: 'AF2' }, 'AE4AEA': { label: 'Air Force Two (09-0017)', type: 'AF2' }, 'AE4AEC': { label: 'Air Force Two (19-0018)', type: 'AF2' }, 'AE0865': { label: 'Marine One (VH-3D)', type: 'M1' }, 'AE5E76': { label: 'Marine One (VH-92A)', type: 'M1' }, 'AE5E77': { label: 'Marine One (VH-92A)', type: 'M1' }, 'AE5E79': { label: 'Marine One (VH-92A)', type: 'M1' }, }; const WorldviewLeftPanel = React.memo(function WorldviewLeftPanel({ data, activeLayers, setActiveLayers, onSettingsClick, onLegendClick, gibsDate, setGibsDate, gibsOpacity, setGibsOpacity, onEntityClick, onFlyTo }: { data: any; activeLayers: any; setActiveLayers: any; onSettingsClick?: () => void; onLegendClick?: () => void; gibsDate?: string; setGibsDate?: (d: string) => void; gibsOpacity?: number; setGibsOpacity?: (o: number) => void; onEntityClick?: (entity: { type: string; id: number; extra?: any }) => void; onFlyTo?: (lat: number, lng: number) => void }) { const [isMinimized, setIsMinimized] = useState(false); const { theme, toggleTheme } = useTheme(); const [gibsPlaying, setGibsPlaying] = useState(false); const [potusEnabled, setPotusEnabled] = useState(true); const gibsIntervalRef = useRef | null>(null); // GIBS time slider play/pause animation useEffect(() => { if (!gibsPlaying || !setGibsDate) { if (gibsIntervalRef.current) clearInterval(gibsIntervalRef.current); gibsIntervalRef.current = null; return; } gibsIntervalRef.current = setInterval(() => { if (!gibsDate) return; const d = new Date(gibsDate + 'T00:00:00'); d.setDate(d.getDate() + 1); const yesterday = new Date(); yesterday.setDate(yesterday.getDate() - 1); if (d > yesterday) { const start = new Date(); start.setDate(start.getDate() - 30); setGibsDate(start.toISOString().slice(0, 10)); } else { setGibsDate(d.toISOString().slice(0, 10)); } }, 1500); return () => { if (gibsIntervalRef.current) clearInterval(gibsIntervalRef.current); }; }, [gibsPlaying, gibsDate, setGibsDate]); // Compute ship category counts (memoized — ships array can be 1000+ items) const { importantShipCount, passengerShipCount, civilianShipCount } = useMemo(() => { const ships = data?.ships; if (!ships || !ships.length) return { importantShipCount: 0, passengerShipCount: 0, civilianShipCount: 0 }; let important = 0, passenger = 0, civilian = 0; for (const s of ships) { const t = s.type; if (t === 'carrier' || t === 'military_vessel' || t === 'tanker' || t === 'cargo') important++; else if (t === 'passenger') passenger++; else civilian++; } return { importantShipCount: important, passengerShipCount: passenger, civilianShipCount: civilian }; }, [data?.ships]); // Find POTUS fleet planes currently airborne from tracked flights const potusFlights = useMemo(() => { const tracked = data?.tracked_flights; if (!tracked) return []; const results: { index: number; flight: any; meta: { label: string; type: string } }[] = []; for (let i = 0; i < tracked.length; i++) { const f = tracked[i]; const icao = (f.icao24 || '').toUpperCase(); if (POTUS_ICAOS[icao]) { results.push({ index: i, flight: f, meta: POTUS_ICAOS[icao] }); } } return results; }, [data?.tracked_flights]); const layers = [ { id: "flights", name: "Commercial Flights", source: "adsb.lol", count: data?.commercial_flights?.length || 0, icon: Plane }, { id: "private", name: "Private Flights", source: "adsb.lol", count: data?.private_flights?.length || 0, icon: Plane }, { id: "jets", name: "Private Jets", source: "adsb.lol", count: data?.private_jets?.length || 0, icon: Plane }, { id: "military", name: "Military Flights", source: "adsb.lol", count: data?.military_flights?.length || 0, icon: AlertTriangle }, { id: "tracked", name: "Tracked Aircraft", source: "Plane-Alert DB", count: data?.tracked_flights?.length || 0, icon: Eye }, { id: "earthquakes", name: "Earthquakes (24h)", source: "USGS", count: data?.earthquakes?.length || 0, icon: Activity }, { id: "satellites", name: "Satellites", source: data?.satellite_source === "celestrak" ? "CelesTrak SGP4" : data?.satellite_source === "tle_api" ? "TLE API · SGP4" : data?.satellite_source === "disk_cache" ? "Cached · SGP4 (est.)" : "CelesTrak SGP4", count: data?.satellites?.length || 0, icon: Satellite }, { id: "ships_important", name: "Carriers / Mil / Cargo", source: "AIS Stream", count: importantShipCount, icon: Ship }, { id: "ships_civilian", name: "Civilian Vessels", source: "AIS Stream", count: civilianShipCount, icon: Anchor }, { id: "ships_passenger", name: "Cruise / Passenger", source: "AIS Stream", count: passengerShipCount, icon: Anchor }, { id: "ukraine_frontline", name: "Ukraine Frontline", source: "DeepStateMap", count: data?.frontlines ? 1 : 0, icon: AlertTriangle }, { id: "global_incidents", name: "Global Incidents", source: "GDELT", count: data?.gdelt?.length || 0, icon: Activity }, { id: "cctv", name: "CCTV Mesh", source: "CCTV Mesh + Street View", count: data?.cctv?.length || 0, icon: Cctv }, { id: "gps_jamming", name: "GPS Jamming", source: "ADS-B NACp", count: data?.gps_jamming?.length || 0, icon: Radio }, { id: "gibs_imagery", name: "MODIS Terra (Daily)", source: "NASA GIBS", count: null, icon: Globe }, { id: "highres_satellite", name: "High-Res Satellite", source: "Esri World Imagery", count: null, icon: Satellite }, { id: "kiwisdr", name: "KiwiSDR Receivers", source: "KiwiSDR.com", count: data?.kiwisdr?.length || 0, icon: Radio }, { id: "firms", name: "Fire Hotspots (24h)", source: "NASA FIRMS VIIRS", count: data?.firms_fires?.length || 0, icon: Flame }, { id: "internet_outages", name: "Internet Outages", source: "IODA / Georgia Tech", count: data?.internet_outages?.length || 0, icon: Wifi }, { id: "datacenters", name: "Data Centers", source: "DC Map (GitHub)", count: data?.datacenters?.length || 0, icon: Server }, { id: "day_night", name: "Day / Night Cycle", source: "Solar Calc", count: null, icon: Sun }, ]; const shipIcon = ; return ( {/* Header */}
TOP SECRET // SI-TK // NOFORN
KH11-4094 OPS-4168

FLIR

{onSettingsClick && ( )} {onLegendClick && ( )} v{packageJson.version}
{/* Data Layers Box */}
{/* Header / Toggle */}
setIsMinimized(!isMinimized)}>DATA LAYERS
{!isMinimized && (
{/* POTUS Fleet — pinned to TOP when aircraft are active */} {potusEnabled && potusFlights.length > 0 && (
POTUS FLEET {potusFlights.length} ACTIVE
{potusFlights.map((pf) => { const color = pf.meta.type === 'AF1' ? '#ff1493' : pf.meta.type === 'M1' ? '#ff1493' : '#3b82f6'; const alt = pf.flight.alt_baro || pf.flight.alt || 0; const speed = pf.flight.gs || pf.flight.speed || 0; return (
{ if (onFlyTo && pf.flight.lat != null && pf.flight.lng != null) { onFlyTo(pf.flight.lat, pf.flight.lng); } if (onEntityClick) { onEntityClick({ type: 'tracked_flight', id: pf.index }); } }} >
{pf.meta.label} {alt > 0 ? `${Math.round(alt).toLocaleString()} ft` : 'GND'} · {speed > 0 ? `${Math.round(speed)} kts` : 'STATIC'}
TRACK
); })}
)} {layers.map((layer, idx) => { const Icon = layer.icon; const active = activeLayers[layer.id as keyof typeof activeLayers] || false; return (
setActiveLayers((prev: any) => ({ ...prev, [layer.id]: !active }))} >
{(['ships_important', 'ships_civilian', 'ships_passenger'].includes(layer.id)) ? shipIcon : }
{layer.name} {layer.source} · {active ? (() => { const fKey = FRESHNESS_MAP[layer.id]; const freshness = fKey && data?.freshness?.[fKey]; const rt = freshness ? relativeTime(freshness) : ''; return rt ? {rt} : 'LIVE'; })() : 'OFF'}
{active && layer.count > 0 && ( {layer.count.toLocaleString()} )}
{active ? 'ON' : 'OFF'}
{/* GIBS Imagery inline controls: time slider + play/pause + opacity */} {active && layer.id === 'gibs_imagery' && gibsDate && setGibsDate && setGibsOpacity && (
e.stopPropagation()}>
{ const yesterday = new Date(); yesterday.setDate(yesterday.getDate() - 1); const selected = new Date(gibsDate + 'T00:00:00'); const diff = Math.round((yesterday.getTime() - selected.getTime()) / 86400000); return 29 - Math.max(0, Math.min(29, diff)); })()} onChange={e => { const daysAgo = 29 - parseInt(e.target.value); const d = new Date(); d.setDate(d.getDate() - 1 - daysAgo); setGibsDate(d.toISOString().slice(0, 10)); }} className="flex-1 h-1 accent-cyan-500 cursor-pointer" />
{gibsDate}
OPC setGibsOpacity(parseInt(e.target.value) / 100)} className="w-16 h-1 accent-cyan-500 cursor-pointer" />
)}
) })} {/* POTUS Fleet — bottom section when inactive or hidden */} {(potusFlights.length === 0 || !potusEnabled) && (
POTUS FLEET
{!potusEnabled ? ( ) : ( NO ACTIVE AIRCRAFT )}
)}
)}
); }); export default WorldviewLeftPanel;