"use client"; import { useEffect, useState } from "react"; import { Circle, Popup } from "react-leaflet"; import { Geofence } from "@/lib/types"; interface GeofenceLayerProps { visible: boolean; } export default function GeofenceLayer({ visible }: GeofenceLayerProps) { const [geofences, setGeofences] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { if (!visible) return; fetchGeofences(); // Refresh every 30 seconds const interval = setInterval(fetchGeofences, 30000); return () => clearInterval(interval); }, [visible]); const fetchGeofences = async () => { try { const response = await fetch("/api/geofences"); if (!response.ok) throw new Error("Failed to fetch geofences"); const data = await response.json(); setGeofences(data.geofences || []); } catch (err) { console.error("Failed to fetch geofences", err); } finally { setLoading(false); } }; if (!visible || loading) return null; return ( <> {geofences .filter((g) => g.is_active === 1) .map((geofence) => (

{geofence.name}

{geofence.description && (

{geofence.description}

)}

📍 Device: {geofence.device_id}

📏 Radius:{" "} {geofence.radius_meters >= 1000 ? `${(geofence.radius_meters / 1000).toFixed(1)} km` : `${geofence.radius_meters} m`}

🎯 Center:{" "} {geofence.center_latitude.toFixed(4)},{" "} {geofence.center_longitude.toFixed(4)}

Active

))} ); }