import React, { useState, useEffect } from 'react'; import api from '../services/api'; import { format } from 'date-fns'; import { MagnifyingGlassIcon, FunnelIcon, EyeIcon } from '@heroicons/react/24/outline'; const Detections = () => { const [detections, setDetections] = useState([]); const [loading, setLoading] = useState(true); const [pagination, setPagination] = useState({}); const [filters, setFilters] = useState({ device_id: '', drone_id: '', start_date: '', end_date: '', limit: 50, offset: 0 }); const [showFilters, setShowFilters] = useState(false); useEffect(() => { fetchDetections(); }, [filters]); const fetchDetections = async () => { try { setLoading(true); const params = new URLSearchParams(); Object.entries(filters).forEach(([key, value]) => { if (value) params.append(key, value); }); console.log('🔍 Fetching detections with params:', params.toString()); const response = await api.get(`/detections?${params}`); console.log('✅ Detections response:', response.data); setDetections(response.data.data?.detections || []); setPagination(response.data.data?.pagination || {}); } catch (error) { console.error('❌ Error fetching detections:', error); setDetections([]); // Ensure detections is always an array setPagination({}); } finally { setLoading(false); } }; const handleFilterChange = (key, value) => { setFilters(prev => ({ ...prev, [key]: value, offset: 0 // Reset to first page when filtering })); }; const handlePageChange = (newOffset) => { setFilters(prev => ({ ...prev, offset: newOffset })); }; const clearFilters = () => { setFilters({ device_id: '', drone_id: '', start_date: '', end_date: '', limit: 50, offset: 0 }); }; return (

Drone Detections

History of all drone detections from your devices

{/* Filters */} {showFilters && (
handleFilterChange('device_id', e.target.value)} />
handleFilterChange('drone_id', e.target.value)} />
handleFilterChange('start_date', e.target.value)} />
handleFilterChange('end_date', e.target.value)} />
)} {/* Detection List */}
{loading ? (
) : ( <>
{detections.map((detection) => ( ))}
Device Drone ID Type Frequency RSSI Location Detected At Actions
{detection.device?.name || `Device ${detection.device_id}`}
ID: {detection.device_id}
{detection.drone_id} {detection.drone_type_info?.name || `Type ${detection.drone_type}`} {detection.drone_type_info?.category && (
{detection.drone_type_info.category}
)}
{detection.freq} MHz -60 ? 'text-red-600' : detection.rssi > -80 ? 'text-yellow-600' : 'text-green-600' }`}> {detection.rssi} dBm
{detection.device?.location_description || (detection.geo_lat && detection.geo_lon ? `${detection.geo_lat}, ${detection.geo_lon}` : 'Unknown')}
{format(new Date(detection.server_timestamp), 'MMM dd, yyyy')}
{format(new Date(detection.server_timestamp), 'HH:mm:ss')}
{detections.length === 0 && !loading && (

No detections found

Try adjusting your search filters.

)} {/* Pagination */} {pagination.total > 0 && (

Showing{' '} {filters.offset + 1} {' '}to{' '} {Math.min(filters.offset + filters.limit, pagination.total)} {' '}of{' '} {pagination.total} {' '}results

)} )}
); }; export default Detections;