"use client"
import { useEffect } from "react"
import { MapContainer, TileLayer, CircleMarker, Popup, useMap } from "react-leaflet"
import { LatLngBounds, LatLng } from "leaflet"
import "leaflet/dist/leaflet.css"
import { type Detection } from "@/lib/api"
interface DashboardMapContentProps {
detections: Detection[]
}
// Component to auto-fit map bounds to show all markers
function FitBounds({ bounds }: { bounds: LatLngBounds }) {
const map = useMap()
useEffect(() => {
if (bounds.isValid()) {
map.fitBounds(bounds, { padding: [50, 50] })
}
}, [bounds, map])
return null
}
export default function DashboardMapContent({ detections }: DashboardMapContentProps) {
if (detections.length === 0) {
return (
)
}
// Calculate bounds to fit all markers
const firstDetection = detections[0]
const bounds = new LatLngBounds(
new LatLng(firstDetection.latitude!, firstDetection.longitude!),
new LatLng(firstDetection.latitude!, firstDetection.longitude!)
)
detections.forEach(d => {
if (d.latitude && d.longitude) {
bounds.extend(new LatLng(d.latitude, d.longitude))
}
})
// Center point
const center: [number, number] = [
(bounds.getNorth() + bounds.getSouth()) / 2,
(bounds.getEast() + bounds.getWest()) / 2
]
// Get marker color based on detection type
const getMarkerColor = (type: string) => {
const t = type.toLowerCase()
if (t === "pothole") {
return { fill: "#ef4444", stroke: "#b91c1c" } // Red
} else if (t === "defected_sign_board") {
return { fill: "#3b82f6", stroke: "#1d4ed8" } // Blue
} else if (t === "road_crack") {
return { fill: "#f59e0b", stroke: "#b45309" } // Orange
} else if (t === "damaged_road_marking") {
return { fill: "#6366f1", stroke: "#4338ca" } // Indigo
} else if (t === "good_sign_board") {
return { fill: "#10b981", stroke: "#047857" } // Emerald
}
return { fill: "#64748b", stroke: "#475569" } // Default Slate
}
// Get display name for detection type
const getTypeName = (type: string) => {
return type.split('_').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' ')
}
return (
{/* Detection markers */}
{detections.map((detection, idx) => {
const colors = getMarkerColor(detection.type)
const typeName = getTypeName(detection.type)
return (
{typeName}
Class:
{detection.class.replace(/_/g, " ")}
Confidence:
{(detection.confidence * 100).toFixed(1)}%
Coordinates
Lat: {detection.latitude!.toFixed(6)}
Lng: {detection.longitude!.toFixed(6)}
)
})}
{/* Auto-fit bounds */}
)
}