"use client"; import L from "leaflet"; import { useEffect, useRef, useState } from "react"; import { MapContainer, TileLayer, Marker, Popup, useMap } from "react-leaflet"; type GeoPoint = { id: string; gps_lat: number | null; gps_lon: number | null; }; function MapContent({ points, error }: { points: GeoPoint[]; error: string | null }) { const map = useMap(); const markersRef = useRef([]); useEffect(() => { markersRef.current.forEach((marker) => marker.remove()); markersRef.current = []; if (points.length === 0) return; points.forEach((point) => { if (point.gps_lat === null || point.gps_lon === null) return; const marker = L.marker([point.gps_lat, point.gps_lon]); marker.addTo(map); markersRef.current.push(marker); }); if (points.length > 0) { const group = L.featureGroup(markersRef.current); map.fitBounds(group.getBounds().pad(0.1)); } }, [points, map]); return null; } export default function MapPage() { const [points, setPoints] = useState([]); const [error, setError] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { fetch("/api/geo") .then((res) => { if (!res.ok) throw new Error("Failed to fetch geo points"); return res.json(); }) .then((data) => { setPoints(data); }) .catch((err) => { setError(err instanceof Error ? err.message : "Unknown error"); }) .finally(() => { setLoading(false); }); }, []); return (

Map

{loading ? (
Loading map...
) : error ? (
Error: {error}
) : points.length === 0 ? (
No GPS points available
) : (
)}
); }