| 1 | 'use client'; |
| 2 | |
| 3 | import { useState, useEffect, useRef } from 'react'; |
| 4 | import { motion, AnimatePresence } from 'framer-motion'; |
| 5 | import { X, ExternalLink, RefreshCw, MapPin, Camera, Maximize2 } from 'lucide-react'; |
| 6 | import Hls from 'hls.js'; |
| 7 | |
| 8 | interface CameraViewerProps { |
| 9 | camera: any | null; |
| 10 | onClose: () => void; |
| 11 | onLocate?: (lat: number, lng: number) => void; |
| 12 | } |
| 13 | |
| 14 | export default function CameraViewer({ camera, onClose, onLocate }: CameraViewerProps) { |
| 15 | const [imageUrl, setImageUrl] = useState<string | null>(null); |
| 16 | const [loading, setLoading] = useState(true); |
| 17 | const [error, setError] = useState(false); |
| 18 | const [refreshKey, setRefreshKey] = useState(0); |
| 19 | const [fullscreen, setFullscreen] = useState(false); |
| 20 | |
| 21 | const videoRef = useRef<HTMLVideoElement>(null); |
| 22 | const hlsRef = useRef<Hls | null>(null); |
| 23 | |
| 24 | const streamType = camera?.stream_type || 'jpg'; |
| 25 | const externalFeedUrl = camera?.external_url || camera?.feed_url; |
| 26 | const externalOnly = Boolean(camera?.external_url && !camera?.feed_url && !camera?.stream_url); |
| 27 | |
| 28 | useEffect(() => { |
| 29 | if (!camera) return; |
| 30 | setLoading(true); |
| 31 | setError(false); |
| 32 | setImageUrl(null); |
| 33 | |
| 34 | // Cleanup previous HLS instance |
| 35 | if (hlsRef.current) { |
| 36 | hlsRef.current.destroy(); |
| 37 | hlsRef.current = null; |
| 38 | } |
| 39 | |
| 40 | if (externalOnly) { |
| 41 | setLoading(false); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | if (streamType === 'hls' && camera.stream_url) { |
| 46 | if (Hls.isSupported() && videoRef.current) { |
| 47 | const hls = new Hls({ enableWorker: false }); |
| 48 | hlsRef.current = hls; |
| 49 | hls.loadSource(camera.stream_url); |
| 50 | hls.attachMedia(videoRef.current); |
| 51 | hls.on(Hls.Events.MANIFEST_PARSED, () => { |
| 52 | setLoading(false); |
| 53 | videoRef.current?.play().catch(() => {}); |
| 54 | }); |
| 55 | hls.on(Hls.Events.ERROR, (event, data) => { |
| 56 | if (data.fatal) setError(true); |
| 57 | }); |
| 58 | } else if (videoRef.current?.canPlayType('application/vnd.apple.mpegurl')) { |
| 59 | videoRef.current.src = camera.stream_url; |
| 60 | videoRef.current.addEventListener('loadedmetadata', () => { |
| 61 | setLoading(false); |
| 62 | videoRef.current?.play().catch(() => {}); |
| 63 | }); |
| 64 | } |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | if (streamType === 'iframe' && camera.stream_url) { |
| 69 | setLoading(false); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | // JPG fallback |
| 74 | if (camera.feed_url) { |
| 75 | const url = camera.feed_url.includes('?') ? `${camera.feed_url}&_t=${Date.now()}` : `${camera.feed_url}?_t=${Date.now()}`; |
| 76 | setImageUrl(url); |
| 77 | } else { |
| 78 | setError(true); |
| 79 | setLoading(false); |
| 80 | } |
| 81 | }, [camera, refreshKey, streamType, externalOnly]); |
| 82 | |
| 83 | // Auto-refresh for JPGs |
| 84 | useEffect(() => { |
| 85 | if (streamType !== 'jpg' || !camera?.feed_url) return; |
| 86 | const iv = setInterval(() => setRefreshKey(k => k + 1), 5000); // 5s refresh for JPG |
| 87 | return () => clearInterval(iv); |
| 88 | }, [camera?.feed_url, streamType]); |
| 89 | |
| 90 | if (!camera) return null; |
| 91 | |
| 92 | return ( |
| 93 | <AnimatePresence> |
| 94 | <motion.div |
| 95 | initial={{ opacity: 0, scale: 0.95, y: 10 }} |
| 96 | animate={{ opacity: 1, scale: 1, y: 0 }} |
| 97 | exit={{ opacity: 0, scale: 0.95 }} |
| 98 | transition={{ duration: 0.3 }} |
| 99 | className={`fixed z-[500] ${ |
| 100 | fullscreen |
| 101 | ? 'inset-2 md:inset-4' |
| 102 | : 'bottom-[70px] left-2 right-2 md:bottom-6 md:right-6 md:left-auto md:w-[420px]' |
| 103 | }`} |
| 104 | > |
| 105 | <div className="glass-panel osiris-glow overflow-hidden h-full flex flex-col" style={{ borderColor: 'rgba(57, 255, 20, 0.3)' }}> |
| 106 | {/* Header */} |
| 107 | <div className="flex items-center justify-between px-3 md:px-4 py-2 md:py-3 border-b border-[var(--border-secondary)] bg-black/40"> |
| 108 | <div className="flex items-center gap-2 flex-1 min-w-0"> |
| 109 | <div className="w-2 h-2 rounded-full bg-[#39FF14] animate-osiris-pulse flex-shrink-0" /> |
| 110 | <Camera className="w-3.5 h-3.5 text-[#39FF14] flex-shrink-0" /> |
| 111 | <div className="min-w-0"> |
| 112 | <h3 className="text-[10px] md:text-[11px] font-mono font-bold text-[#39FF14] tracking-wider truncate">{camera.name}</h3> |
| 113 | <p className="text-[6px] md:text-[7px] font-mono text-[var(--text-muted)]">{camera.city}, {camera.country} · {camera.source}</p> |
| 114 | </div> |
| 115 | </div> |
| 116 | <div className="flex items-center gap-1 flex-shrink-0"> |
| 117 | {streamType === 'jpg' && ( |
| 118 | <button onClick={() => setRefreshKey(k => k + 1)} className="p-1.5 rounded hover:bg-[var(--hover-accent)] transition-colors" title="Refresh feed"> |
| 119 | <RefreshCw className="w-3 h-3 text-[var(--text-muted)] hover:text-[#39FF14]" /> |
| 120 | </button> |
| 121 | )} |
| 122 | {camera.lat && camera.lng && ( |
| 123 | <button onClick={() => onLocate?.(camera.lat, camera.lng)} className="p-1.5 rounded hover:bg-[var(--hover-accent)] transition-colors" title="Fly to location"> |
| 124 | <MapPin className="w-3 h-3 text-[var(--text-muted)] hover:text-[var(--gold-primary)]" /> |
| 125 | </button> |
| 126 | )} |
| 127 | <button onClick={() => setFullscreen(!fullscreen)} className="hidden md:block p-1.5 rounded hover:bg-[var(--hover-accent)] transition-colors" title="Toggle fullscreen"> |
| 128 | <Maximize2 className="w-3 h-3 text-[var(--text-muted)] hover:text-white" /> |
| 129 | </button> |
| 130 | <button onClick={onClose} className="p-1.5 rounded hover:bg-red-900/30 transition-colors"> |
| 131 | <X className="w-4 h-4 md:w-3 md:h-3 text-[var(--text-muted)] hover:text-red-400" /> |
| 132 | </button> |
| 133 | </div> |
| 134 | </div> |
| 135 | |
| 136 | {/* Camera Feed */} |
| 137 | <div className={`relative bg-black ${fullscreen ? 'flex-1' : 'aspect-video max-h-[35vh] md:max-h-none'}`}> |
| 138 | {loading && !error && !externalOnly && ( |
| 139 | <div className="absolute inset-0 flex items-center justify-center bg-black/80 z-10"> |
| 140 | <div className="text-center"> |
| 141 | <div className="w-6 h-6 border-2 border-[#39FF14] border-t-transparent rounded-full animate-spin mx-auto mb-2" /> |
| 142 | <span className="text-[8px] font-mono text-[#39FF14] tracking-widest">CONNECTING TO FEED...</span> |
| 143 | </div> |
| 144 | </div> |
| 145 | )} |
| 146 | |
| 147 | {externalOnly ? ( |
| 148 | <div className="absolute inset-0 flex items-center justify-center bg-black/90"> |
| 149 | <div className="text-center px-6"> |
| 150 | <div className="w-8 h-8 rounded-full bg-[#39FF14]/15 flex items-center justify-center mx-auto mb-2"><ExternalLink className="w-4 h-4 text-[#39FF14]" /></div> |
| 151 | <span className="text-[9px] font-mono text-[#39FF14] tracking-widest block mb-1">EXTERNAL FEED</span> |
| 152 | <span className="text-[7px] font-mono text-[var(--text-muted)]">Live stream opens in source viewer</span> |
| 153 | {externalFeedUrl && ( |
| 154 | <a href={externalFeedUrl} target="_blank" rel="noopener noreferrer" className="block mx-auto mt-3 px-3 py-1 text-[8px] font-mono text-[#39FF14] border border-[#39FF14]/30 rounded hover:bg-[#39FF14]/10 transition-colors tracking-wider"> |
| 155 | OPEN FEED |
| 156 | </a> |
| 157 | )} |
| 158 | </div> |
| 159 | </div> |
| 160 | ) : error ? ( |
| 161 | <div className="absolute inset-0 flex items-center justify-center bg-black/90"> |
| 162 | <div className="text-center"> |
| 163 | <div className="w-8 h-8 rounded-full bg-red-500/20 flex items-center justify-center mb-2 mx-auto"><Camera className="w-4 h-4 text-red-400" /></div> |
| 164 | <span className="text-[9px] font-mono text-red-400 tracking-widest block mb-1">FEED UNAVAILABLE</span> |
| 165 | <span className="text-[7px] font-mono text-[var(--text-muted)]">Camera may be offline or restricted</span> |
| 166 | <button onClick={() => { setError(false); setRefreshKey(k => k + 1); }} className="block mx-auto mt-3 px-3 py-1 text-[8px] font-mono text-[#39FF14] border border-[#39FF14]/30 rounded hover:bg-[#39FF14]/10 transition-colors tracking-wider"> |
| 167 | RETRY |
| 168 | </button> |
| 169 | </div> |
| 170 | </div> |
| 171 | ) : streamType === 'hls' ? ( |
| 172 | <video |
| 173 | ref={videoRef} |
| 174 | className={`w-full ${fullscreen ? 'h-full object-contain' : 'h-full object-cover'}`} |
| 175 | autoPlay |
| 176 | muted |
| 177 | playsInline |
| 178 | /> |
| 179 | ) : streamType === 'iframe' && camera.stream_url ? ( |
| 180 | <iframe |
| 181 | src={camera.stream_url} |
| 182 | className="w-full h-full border-0" |
| 183 | allow="autoplay; fullscreen" |
| 184 | allowFullScreen |
| 185 | /> |
| 186 | ) : imageUrl ? ( |
| 187 | <img |
| 188 | key={refreshKey} |
| 189 | src={imageUrl} |
| 190 | alt={camera.name} |
| 191 | className={`w-full ${fullscreen ? 'h-full object-contain' : 'h-full object-cover'}`} |
| 192 | onLoad={() => setLoading(false)} |
| 193 | onError={() => { setLoading(false); setError(true); }} |
| 194 | /> |
| 195 | ) : null} |
| 196 | |
| 197 | {/* Live indicator */} |
| 198 | {!error && !loading && !externalOnly && ( |
| 199 | <div className="absolute top-2 left-2 flex items-center gap-1.5 bg-black/70 backdrop-blur-sm px-2 py-1 rounded"> |
| 200 | <div className="w-1.5 h-1.5 rounded-full bg-red-500 animate-osiris-pulse" /> |
| 201 | <span className="text-[7px] font-mono text-white tracking-widest"> |
| 202 | {streamType === 'jpg' ? 'LIVE SNAPSHOT' : 'LIVE VIDEO'} |
| 203 | </span> |
| 204 | </div> |
| 205 | )} |
| 206 | </div> |
| 207 | |
| 208 | {/* Footer with coords + links */} |
| 209 | <div className="px-3 md:px-4 py-2 border-t border-[var(--border-secondary)] bg-black/40 flex items-center justify-between"> |
| 210 | <div className="text-[7px] md:text-[8px] font-mono text-[var(--text-muted)]"> |
| 211 | {camera.lat?.toFixed(4)}, {camera.lng?.toFixed(4)} |
| 212 | </div> |
| 213 | <div className="flex gap-2"> |
| 214 | {(camera.feed_url || camera.external_url) && ( |
| 215 | <a href={camera.external_url || camera.feed_url} target="_blank" rel="noopener noreferrer" |
| 216 | className="flex items-center gap-1 text-[7px] font-mono text-[#39FF14] hover:underline tracking-wider"> |
| 217 | <ExternalLink className="w-2.5 h-2.5" /> FEED |
| 218 | </a> |
| 219 | )} |
| 220 | <a href={`https://www.google.com/maps/@${camera.lat},${camera.lng},17z`} target="_blank" rel="noopener noreferrer" |
| 221 | className="flex items-center gap-1 text-[7px] font-mono text-[var(--cyan-primary)] hover:underline tracking-wider"> |
| 222 | <MapPin className="w-2.5 h-2.5" /> MAP |
| 223 | </a> |
| 224 | </div> |
| 225 | </div> |
| 226 | </div> |
| 227 | </motion.div> |
| 228 | </AnimatePresence> |
| 229 | ); |
| 230 | } |