"use client" import { Card, CardContent, CardHeader, CardTitle } from "@/app/_components/ui/card" import { Skeleton } from "@/app/_components/ui/skeleton" import DistrictLayer, { type DistrictFeature } from "./layers/district-layer" import MapView from "./map" import { Button } from "@/app/_components/ui/button" import { AlertCircle } from "lucide-react" import { getMonthName } from "@/app/_utils/common" import { useRef, useState, useCallback, useMemo, useEffect } from "react" import { useFullscreen } from "@/app/_hooks/use-fullscreen" import { Overlay } from "./overlay" import MapLegend from "./controls/map-legend" import { useGetAvailableYears, useGetCrimeCategories, useGetCrimes } from "@/app/(pages)/(admin)/dashboard/crime-management/crime-overview/_queries/queries" import { ITopTooltipsMapId } from "./controls/map-tooltips" import MapSelectors from "./controls/map-selector" import TopNavigation from "./controls/map-navigations" import CrimeSidebar from "./sidebar/map-sidebar" import SidebarToggle from "./sidebar/sidebar-toggle" import { cn } from "@/app/_lib/utils" import CrimePopup from "./pop-up/crime-popup" import { $Enums, crime_categories, crime_incidents, crimes, demographics, districts, geographics, locations } from "@prisma/client" // Updated CrimeIncident type to match the structure in crime_incidents interface CrimeIncident { id: string timestamp: Date description: string status: string category?: string type?: string address?: string latitude?: number longitude?: number } export default function CrimeMap() { // State for sidebar const [sidebarCollapsed, setSidebarCollapsed] = useState(true) const [selectedDistrict, setSelectedDistrict] = useState(null) const [selectedIncident, setSelectedIncident] = useState(null) const [showLegend, setShowLegend] = useState(true) const [selectedCategory, setSelectedCategory] = useState("all") const [selectedYear, setSelectedYear] = useState(2024) const [selectedMonth, setSelectedMonth] = useState("all") const [activeControl, setActiveControl] = useState("incidents") const mapContainerRef = useRef(null) // Use the custom fullscreen hook const { isFullscreen } = useFullscreen(mapContainerRef) // Get available years const { data: availableYears, isLoading: isYearsLoading, error: yearsError } = useGetAvailableYears() // Extract all unique categories const { data: categoriesData, isLoading: isCategoryLoading } = useGetCrimeCategories() // Transform categories data to string array const categories = useMemo(() => categoriesData ? categoriesData.map(category => category.name) : [] , [categoriesData]) // Get all crime data in a single request const { data: crimes, isLoading: isCrimesLoading, error: crimesError } = useGetCrimes() // Filter crimes based on selected year and month const filteredByYearAndMonth = useMemo(() => { if (!crimes) return [] return crimes.filter((crime) => { const yearMatch = crime.year === selectedYear if (selectedMonth === "all") { return yearMatch } else { return yearMatch && crime.month === selectedMonth } }) }, [crimes, selectedYear, selectedMonth]) // Filter incidents based on selected category const filteredCrimes = useMemo(() => { if (!filteredByYearAndMonth) return [] if (selectedCategory === "all") return filteredByYearAndMonth return filteredByYearAndMonth.map((crime) => { const filteredIncidents = crime.crime_incidents.filter( incident => incident.crime_categories.name === selectedCategory ) return { ...crime, crime_incidents: filteredIncidents, number_of_crime: filteredIncidents.length } }) }, [filteredByYearAndMonth, selectedCategory]) // Extract all incidents from all districts for marker display // const allIncidents = useMemo(() => { // if (!filteredCrimes) return [] // return filteredCrimes.flatMap((crime) => // crime.crime_incidents.map((incident) => ({ // id: incident.id, // timestamp: incident.timestamp, // description: incident.description, // status: incident.status, // category: incident.crime_categories.name, // type: incident.crime_categories.type, // address: incident.locations.address, // latitude: incident.locations.latitude, // longitude: incident.locations.longitude, // })) // ) // }, [filteredCrimes]) // Handle incident marker click const handleIncidentClick = (incident: CrimeIncident) => { console.log("Incident clicked directly:", incident); if (!incident.longitude || !incident.latitude) { console.error("Invalid incident coordinates:", incident); return; } // When an incident is clicked, clear any selected district setSelectedDistrict(null); // Set the selected incident setSelectedIncident(incident); } // Set up event listener for incident clicks from the district layer useEffect(() => { const handleIncidentClickEvent = (e: CustomEvent) => { console.log("Received incident_click event:", e.detail); if (e.detail) { if (!e.detail.longitude || !e.detail.latitude) { console.error("Invalid incident coordinates in event:", e.detail); return; } // When an incident is clicked, clear any selected district setSelectedDistrict(null); // Set the selected incident setSelectedIncident(e.detail); } } // Add event listener to the map container and document const mapContainer = mapContainerRef.current // Clean up previous listeners to prevent duplicates document.removeEventListener('incident_click', handleIncidentClickEvent as EventListener); if (mapContainer) { mapContainer.removeEventListener('incident_click', handleIncidentClickEvent as EventListener); } // Listen on both the container and document to ensure we catch the event document.addEventListener('incident_click', handleIncidentClickEvent as EventListener); if (mapContainer) { mapContainer.addEventListener('incident_click', handleIncidentClickEvent as EventListener); } return () => { document.removeEventListener('incident_click', handleIncidentClickEvent as EventListener); if (mapContainer) { mapContainer.removeEventListener('incident_click', handleIncidentClickEvent as EventListener); } } }, []); // Handle district click const handleDistrictClick = (feature: DistrictFeature) => { // When a district is clicked, clear any selected incident setSelectedIncident(null); // Set the selected district setSelectedDistrict(feature); } // Reset filters const resetFilters = useCallback(() => { setSelectedYear(2024) setSelectedMonth("all") setSelectedCategory("all") }, []) // Determine the title based on filters const getMapTitle = () => { let title = `${selectedYear}` if (selectedMonth !== "all") { title += ` - ${getMonthName(Number(selectedMonth))}` } if (selectedCategory !== "all") { title += ` - ${selectedCategory}` } return title } // Toggle sidebar function const toggleSidebar = useCallback(() => { setSidebarCollapsed(!sidebarCollapsed) }, [sidebarCollapsed]) return ( Crime Map {getMapTitle()} {isCrimesLoading ? (
) : crimesError ? (

Failed to load crime data. Please try again later.

) : (
{/* District Layer with crime data */} {/* Popup for selected incident */} {selectedIncident && selectedIncident.latitude && selectedIncident.longitude && ( <> {/* {console.log("About to render CrimePopup with:", selectedIncident)} */} setSelectedIncident(null)} crime={selectedIncident} /> )} {/* Components that are only visible in fullscreen mode */} {isFullscreen && ( <>
{/* Pass selectedCategory, selectedYear, and selectedMonth to the sidebar */} )}
)}
) }