| 1 | 'use client'; |
| 2 | |
| 3 | import { memo, useState } from 'react'; |
| 4 | import { motion, AnimatePresence } from 'framer-motion'; |
| 5 | import { |
| 6 | Plane, Satellite, Activity, Globe, Radio, Eye, |
| 7 | Shield, Sun, AlertTriangle, Camera, Flame, Target, |
| 8 | CloudLightning, Radiation, Tv, Anchor, Ship, Newspaper, |
| 9 | ChevronDown, ChevronUp, ToggleLeft, ToggleRight, Network, |
| 10 | } from 'lucide-react'; |
| 11 | |
| 12 | interface LayerPanelProps { |
| 13 | data: any; |
| 14 | activeLayers: any; |
| 15 | setActiveLayers: React.Dispatch<React.SetStateAction<any>>; |
| 16 | } |
| 17 | |
| 18 | const LAYER_GROUPS = [ |
| 19 | { |
| 20 | label: 'OSIRIS SDK', |
| 21 | icon: Network, |
| 22 | color: '#1565C0', |
| 23 | layers: [ |
| 24 | { key: 'sdk_stream', label: 'Intelligence Stream', icon: Network, color: '#1565C0', dataKey: 'sdk_entities' }, |
| 25 | ], |
| 26 | }, |
| 27 | { |
| 28 | label: 'AVIATION', |
| 29 | icon: Plane, |
| 30 | color: '#00E5FF', |
| 31 | layers: [ |
| 32 | { key: 'flights', label: 'Commercial', icon: Plane, color: '#00E5FF', dataKey: 'commercial_flights' }, |
| 33 | { key: 'private', label: 'Private', icon: Plane, color: '#00E676', dataKey: 'private_flights' }, |
| 34 | { key: 'jets', label: 'Private Jets', icon: Plane, color: '#FF69B4', dataKey: 'private_jets' }, |
| 35 | { key: 'military', label: 'Military', icon: Shield, color: '#FF3D3D', dataKey: 'military_flights' }, |
| 36 | ], |
| 37 | }, |
| 38 | { |
| 39 | label: 'MARITIME & SPACE', |
| 40 | icon: Ship, |
| 41 | color: '#00BCD4', |
| 42 | layers: [ |
| 43 | { key: 'maritime', label: 'Maritime / Naval', icon: Ship, color: '#00BCD4', dataKey: 'maritime_ships,maritime_ports,maritime_chokepoints' }, |
| 44 | { key: 'satellites', label: 'Satellites', icon: Satellite, color: '#D4AF37', dataKey: 'satellites' }, |
| 45 | ], |
| 46 | }, |
| 47 | { |
| 48 | label: 'SURVEILLANCE', |
| 49 | icon: Camera, |
| 50 | color: '#39FF14', |
| 51 | layers: [ |
| 52 | { key: 'cctv', label: 'CCTV Cameras', icon: Camera, color: '#39FF14', dataKey: 'cameras' }, |
| 53 | { key: 'live_news', label: 'Live News Feeds', icon: Tv, color: '#FF4081', dataKey: 'live_feeds' }, |
| 54 | ], |
| 55 | }, |
| 56 | { |
| 57 | label: 'NATURAL HAZARDS', |
| 58 | icon: Activity, |
| 59 | color: '#FF9500', |
| 60 | layers: [ |
| 61 | { key: 'earthquakes', label: 'Earthquakes (24h)', icon: Activity, color: '#FF9500', dataKey: 'earthquakes' }, |
| 62 | { key: 'fires', label: 'Active Fires', icon: Flame, color: '#FF6B00', dataKey: 'fires' }, |
| 63 | { key: 'weather', label: 'Severe Weather', icon: CloudLightning, color: '#E040FB', dataKey: 'weather_events' }, |
| 64 | ], |
| 65 | }, |
| 66 | { |
| 67 | label: 'THREATS & INFRA', |
| 68 | icon: AlertTriangle, |
| 69 | color: '#FF3D3D', |
| 70 | layers: [ |
| 71 | { key: 'infrastructure', label: 'Nuclear Facilities', icon: Radiation, color: '#76FF03', dataKey: 'infrastructure' }, |
| 72 | { key: 'global_incidents', label: 'Global Incidents', icon: AlertTriangle, color: '#FF3D3D', dataKey: 'gdelt' }, |
| 73 | { key: 'gps_jamming', label: 'GPS Jamming', icon: Radio, color: '#FF4444', dataKey: 'gps_jamming' }, |
| 74 | ], |
| 75 | }, |
| 76 | { |
| 77 | label: 'DISPLAY', |
| 78 | icon: Sun, |
| 79 | color: '#448AFF', |
| 80 | layers: [ |
| 81 | { key: 'day_night', label: 'Day / Night Cycle', icon: Sun, color: '#448AFF', dataKey: '' }, |
| 82 | ], |
| 83 | }, |
| 84 | ]; |
| 85 | |
| 86 | // Flat list for backward compat |
| 87 | const ALL_LAYERS = LAYER_GROUPS.flatMap(g => g.layers); |
| 88 | |
| 89 | function LayerPanel({ data, activeLayers, setActiveLayers }: LayerPanelProps) { |
| 90 | const [expandedGroups, setExpandedGroups] = useState<Record<string, boolean>>(() => { |
| 91 | const initial: Record<string, boolean> = {}; |
| 92 | LAYER_GROUPS.forEach(g => { initial[g.label] = true; }); |
| 93 | return initial; |
| 94 | }); |
| 95 | |
| 96 | const toggle = (key: string) => setActiveLayers((prev: any) => ({ ...prev, [key]: !prev[key] })); |
| 97 | const getCount = (dk: string): number | null => { |
| 98 | if (!dk) return null; |
| 99 | let total = 0; |
| 100 | let found = false; |
| 101 | for (const k of dk.split(',')) { |
| 102 | if (data[k] && Array.isArray(data[k])) { |
| 103 | total += data[k].length; |
| 104 | found = true; |
| 105 | } |
| 106 | } |
| 107 | return found ? total : null; |
| 108 | }; |
| 109 | const totalEntities = ALL_LAYERS.reduce((s: number, l: any) => s + (getCount(l.dataKey) || 0), 0); |
| 110 | const activeCount = Object.values(activeLayers).filter(Boolean).length; |
| 111 | |
| 112 | const toggleGroup = (groupLabel: string) => { |
| 113 | setExpandedGroups(prev => ({ ...prev, [groupLabel]: !prev[groupLabel] })); |
| 114 | }; |
| 115 | |
| 116 | const toggleAllInGroup = (group: typeof LAYER_GROUPS[0]) => { |
| 117 | const allActive = group.layers.every(l => activeLayers[l.key]); |
| 118 | setActiveLayers((prev: any) => { |
| 119 | const next = { ...prev }; |
| 120 | group.layers.forEach(l => { next[l.key] = !allActive; }); |
| 121 | return next; |
| 122 | }); |
| 123 | }; |
| 124 | |
| 125 | return ( |
| 126 | <motion.div initial={{ opacity: 0, x: -20 }} animate={{ opacity: 1, x: 0 }} transition={{ delay: 0.3, duration: 0.6 }} className="glass-panel p-3 pointer-events-auto"> |
| 127 | {/* Header */} |
| 128 | <div className="flex items-center justify-between mb-3"> |
| 129 | <div className="flex items-center gap-2"> |
| 130 | <div className="relative"> |
| 131 | <Eye className="w-3.5 h-3.5 stroke-[1.5] text-[var(--gold-primary)]" /> |
| 132 | <div className="absolute -top-0.5 -right-0.5 w-2 h-2 rounded-full bg-[var(--alert-green)] animate-osiris-pulse" /> |
| 133 | </div> |
| 134 | <span className="hud-text text-[12px] text-[var(--text-primary)] tracking-widest">DATA LAYERS</span> |
| 135 | </div> |
| 136 | <div className="flex items-center gap-1.5"> |
| 137 | <span className={`gotham-tag ${activeCount > 10 ? 'gotham-tag--critical' : activeCount > 5 ? 'gotham-tag--high' : 'gotham-tag--low'}`} style={{ fontSize: '8px', padding: '1px 6px' }}> |
| 138 | {activeCount}/{ALL_LAYERS.length} |
| 139 | </span> |
| 140 | <span className="gotham-tag gotham-tag--info" style={{ fontSize: '7px', padding: '1px 5px' }}>{totalEntities.toLocaleString()} ENT</span> |
| 141 | </div> |
| 142 | </div> |
| 143 | |
| 144 | {/* Groups */} |
| 145 | <div className="space-y-1"> |
| 146 | {LAYER_GROUPS.map((group) => { |
| 147 | const isExpanded = expandedGroups[group.label]; |
| 148 | const groupActiveCount = group.layers.filter(l => activeLayers[l.key]).length; |
| 149 | const allActive = groupActiveCount === group.layers.length; |
| 150 | const GroupIcon = group.icon; |
| 151 | |
| 152 | return ( |
| 153 | <div key={group.label}> |
| 154 | {/* Group Header */} |
| 155 | <div className="flex items-center gap-1.5"> |
| 156 | <button |
| 157 | onClick={() => toggleGroup(group.label)} |
| 158 | className="flex-1 flex items-center gap-2 px-2 py-1.5 rounded-md hover:bg-white/[0.03] transition-colors" |
| 159 | > |
| 160 | <GroupIcon className="w-3 h-3 stroke-[1.5] flex-shrink-0" style={{ color: group.color }} /> |
| 161 | <span className="text-[9px] font-mono tracking-[0.15em] text-[var(--text-secondary)] font-bold flex-1 text-left">{group.label}</span> |
| 162 | <span className="text-[8px] font-mono tabular-nums" style={{ color: groupActiveCount > 0 ? group.color : 'var(--text-muted)' }}> |
| 163 | {groupActiveCount}/{group.layers.length} |
| 164 | </span> |
| 165 | {isExpanded ? ( |
| 166 | <ChevronUp className="w-3 h-3 stroke-[1.5] text-[var(--text-muted)]" /> |
| 167 | ) : ( |
| 168 | <ChevronDown className="w-3 h-3 stroke-[1.5] text-[var(--text-muted)]" /> |
| 169 | )} |
| 170 | </button> |
| 171 | {/* Toggle all in group */} |
| 172 | <button |
| 173 | onClick={() => toggleAllInGroup(group)} |
| 174 | className="p-1 rounded hover:bg-white/[0.05] transition-colors" |
| 175 | title={allActive ? 'Disable all' : 'Enable all'} |
| 176 | > |
| 177 | {allActive ? ( |
| 178 | <ToggleRight className="w-3.5 h-3.5 stroke-[1.5]" style={{ color: group.color }} /> |
| 179 | ) : ( |
| 180 | <ToggleLeft className="w-3.5 h-3.5 stroke-[1.5] text-[var(--text-muted)]" /> |
| 181 | )} |
| 182 | </button> |
| 183 | </div> |
| 184 | |
| 185 | {/* Layer items */} |
| 186 | <AnimatePresence> |
| 187 | {isExpanded && ( |
| 188 | <motion.div |
| 189 | initial={{ height: 0, opacity: 0 }} |
| 190 | animate={{ height: 'auto', opacity: 1 }} |
| 191 | exit={{ height: 0, opacity: 0 }} |
| 192 | transition={{ duration: 0.2 }} |
| 193 | className="overflow-hidden" |
| 194 | > |
| 195 | <div className="ml-2 pl-2 border-l border-[var(--border-secondary)]/40 space-y-px"> |
| 196 | {group.layers.map((layer) => { |
| 197 | const Icon = layer.icon; |
| 198 | const isActive = activeLayers[layer.key]; |
| 199 | const count = getCount(layer.dataKey); |
| 200 | return ( |
| 201 | <button |
| 202 | key={layer.key} |
| 203 | onClick={() => toggle(layer.key)} |
| 204 | className={`w-full flex items-center gap-2.5 px-2 py-[5px] rounded-md transition-all duration-200 group ${ |
| 205 | isActive |
| 206 | ? 'bg-white/[0.04] border border-white/[0.06]' |
| 207 | : 'border border-transparent hover:bg-white/[0.02]' |
| 208 | }`} |
| 209 | > |
| 210 | {/* Color dot indicator */} |
| 211 | <div |
| 212 | className={`w-1.5 h-1.5 rounded-full flex-shrink-0 transition-all duration-300 ${isActive ? 'scale-100' : 'scale-50 opacity-30'}`} |
| 213 | style={{ |
| 214 | backgroundColor: layer.color, |
| 215 | boxShadow: isActive ? `0 0 6px ${layer.color}60` : 'none', |
| 216 | }} |
| 217 | /> |
| 218 | <Icon |
| 219 | className="w-3.5 h-3.5 stroke-[1.5] flex-shrink-0 transition-colors duration-200" |
| 220 | style={{ color: isActive ? layer.color : 'var(--text-muted)' }} |
| 221 | /> |
| 222 | <span className={`text-[11px] font-mono tracking-wide flex-1 text-left transition-colors duration-200 ${ |
| 223 | isActive ? 'text-[var(--text-primary)]' : 'text-[var(--text-muted)] group-hover:text-[var(--text-secondary)]' |
| 224 | }`}> |
| 225 | {layer.label} |
| 226 | </span> |
| 227 | {count !== null && ( |
| 228 | <span |
| 229 | className="text-[9px] font-mono tabular-nums font-bold transition-colors duration-200" |
| 230 | style={{ color: isActive ? layer.color : 'var(--text-muted)' }} |
| 231 | > |
| 232 | {count.toLocaleString()} |
| 233 | </span> |
| 234 | )} |
| 235 | {/* Toggle switch */} |
| 236 | <div className={`layer-toggle ${isActive ? 'active' : ''}`} /> |
| 237 | </button> |
| 238 | ); |
| 239 | })} |
| 240 | </div> |
| 241 | </motion.div> |
| 242 | )} |
| 243 | </AnimatePresence> |
| 244 | </div> |
| 245 | ); |
| 246 | })} |
| 247 | </div> |
| 248 | </motion.div> |
| 249 | ); |
| 250 | } |
| 251 | |
| 252 | export default memo(LayerPanel); |