"use client"
import { useEffect, useState } from "react"
import dynamic from "next/dynamic"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Loader2, MapPin, AlertTriangle, RectangleHorizontal } from "lucide-react"
import { fetchAllDetections, type Detection } from "@/lib/api"
// Dynamically import the map to avoid SSR issues with Leaflet
const DashboardMapContent = dynamic(
() => import("./dashboard-map-content"),
{
ssr: false,
loading: () => (
)
}
)
interface DashboardMapProps {
className?: string
selectedProjectId?: string | null
selectedPackageId?: string | null
selectedLocationId?: string | null
projectSummary?: any
}
export function DashboardMap({
className,
selectedProjectId,
selectedPackageId,
selectedLocationId,
projectSummary
}: DashboardMapProps) {
const [detections, setDetections] = useState([])
const [isLoading, setIsLoading] = useState(true)
const [error, setError] = useState(null)
useEffect(() => {
if (!projectSummary) {
setDetections([])
setIsLoading(false)
return
}
try {
setIsLoading(true)
setError(null)
const filteredDetections: Detection[] = []
const packagesToProcess = selectedPackageId && selectedPackageId !== "all"
? { [selectedPackageId]: projectSummary.packages[selectedPackageId] }
: projectSummary.packages || {}
for (const [pkgName, pkg] of Object.entries(packagesToProcess)) {
const locationsToProcess = selectedLocationId && selectedLocationId !== "all"
? { [selectedLocationId]: (pkg as any).locations[selectedLocationId] }
: (pkg as any).locations || {}
for (const [locName, loc] of Object.entries(locationsToProcess)) {
const locationDetections = (loc as any).detections || []
filteredDetections.push(...locationDetections)
}
}
setDetections(filteredDetections)
} catch (err) {
console.error("Failed to extract detections:", err)
setError("Failed to load detection data")
} finally {
setIsLoading(false)
}
}, [projectSummary, selectedPackageId, selectedLocationId])
// Filter detections with valid GPS coordinates
const validDetections = detections.filter(d => d.latitude && d.longitude)
// Count potholes and signboards
const potholeCount = validDetections.filter(d =>
d.type?.toLowerCase().includes("pothole") ||
d.class?.toLowerCase().includes("pothole")
).length
const signboardCount = validDetections.length - potholeCount
return (
Detection Map
{isLoading ? "Loading..." : `${validDetections.length} detections with GPS coordinates`}
{/* Compact Color Legend */}
Potholes ({potholeCount})
Signboards ({signboardCount})
{isLoading ? (
) : error ? (
) : validDetections.length === 0 ? (
No detections with GPS coordinates found
Process some videos to see detections on the map
) : (
)}
)
}