import { useEffect, useState } from "react"; import { MapPin } from "lucide-react"; interface Location { id: string; name: string; type: string; address: string; coordinates: [number, number]; status: string; capacity: number; currentLoad: number; lastPickup: string; coverage: string; population: number; schedule: string; } interface MapComponentProps { locations: Location[]; onLocationSelect: (location: Location) => void; } export function LeafletMap({ locations, onLocationSelect }: MapComponentProps) { const [mapComponents, setMapComponents] = useState(null); const [leaflet, setLeaflet] = useState(null); useEffect(() => { const loadMapComponents = async () => { try { const [{ MapContainer, TileLayer, Marker, Popup }, L] = await Promise.all([import("react-leaflet"), import("leaflet")]); delete (L as any).Icon.Default.prototype._getIconUrl; L.Icon.Default.mergeOptions({ iconRetinaUrl: "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/images/marker-icon-2x.png", iconUrl: "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/images/marker-icon.png", shadowUrl: "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/images/marker-shadow.png" }); setMapComponents({ MapContainer, TileLayer, Marker, Popup }); setLeaflet(L); } catch (error) { console.error("Error loading map components:", error); } }; loadMapComponents(); }, []); // Custom icon function const getMarkerIcon = (type: string, status: string) => { if (!leaflet) return undefined; const color = status === "active" ? "#10b981" : status === "maintenance" ? "#f59e0b" : "#ef4444"; return leaflet.divIcon({ html: `
`, className: "custom-div-icon", iconSize: [24, 24], iconAnchor: [12, 12], popupAnchor: [0, -12] }); }; if (!mapComponents || !leaflet) { return (

Loading map...

); } const { MapContainer, TileLayer, Marker, Popup } = mapComponents; return ( {locations.map((location) => ( onLocationSelect(location) }} >

{location.name}

{location.address}

Status: {location.status}
Load: {Math.round( (location.currentLoad / location.capacity) * 100 )} %
Population: {location.population.toLocaleString()}
Last Pickup: {location.lastPickup}
))}
); }