"use client" import { useState, useMemo, useEffect } from "react" import { Popup } from "react-map-gl/mapbox" import { Badge } from "@/app/_components/ui/badge" import { Card } from "@/app/_components/ui/card" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/app/_components/ui/tabs" import { Button } from "@/app/_components/ui/button" import { getMonthName } from "@/app/_utils/common" import { BarChart, Users, Home, AlertTriangle, ChevronRight, Building, Calendar, X } from 'lucide-react' import { IDistrictFeature } from "@/app/_utils/types/map" // Helper function to format numbers function formatNumber(num?: number): string { if (num === undefined || num === null) return "N/A" if (num >= 1_000_000) { return (num / 1_000_000).toFixed(1) + "M" } if (num >= 1_000) { return (num / 1_000).toFixed(1) + "K" } return num.toLocaleString() } interface DistrictPopupProps { longitude: number latitude: number onClose: () => void district: IDistrictFeature year?: string month?: string filterCategory?: string | "all" } export default function DistrictPopup({ longitude, latitude, onClose, district, year, month, filterCategory = "all", }: DistrictPopupProps) { const [activeTab, setActiveTab] = useState("overview") // Add debug log when the component is rendered useEffect(() => { console.log("DistrictPopup mounted:", { district: district.name, coords: [longitude, latitude], year, month }); }, [district, longitude, latitude, year, month]); // Extract all crime incidents from the district data and apply filtering if needed const allCrimeIncidents = useMemo(() => { // Check if there are crime incidents in the district object if (!Array.isArray(district.crime_incidents)) { console.warn("No crime incidents array found in district data") return [] } // Return all incidents if filterCategory is 'all' if (filterCategory === "all") { return district.crime_incidents } // Otherwise, filter by category return district.crime_incidents.filter((incident) => incident.category === filterCategory) }, [district, filterCategory]) const getCrimeRateBadge = (level?: string) => { switch (level) { case "low": return Low case "medium": return Medium case "high": return High case "critical": return Critical default: return Unknown } } // Format a time period string from year and month const getTimePeriod = () => { if (year && month && month !== "all") { return `${getMonthName(Number(month))} ${year}` } return year || "All time" } return (
{/* Custom close button */}

{district.name}

{getCrimeRateBadge(district.level)}
{formatNumber(district.number_of_crime || 0)} Incidents
{formatNumber(district.demographics?.population || 0)} Population
{formatNumber(district.geographics?.land_area || 0)} km²
Overview Demographics Incidents

Crime Level

This area has a {district.level || "unknown"} level of crime based on incident reports.

{district.geographics && district.geographics.land_area && (

Geography

Land area: {formatNumber(district.geographics.land_area)} km²

{district.geographics.address && (

Address: {district.geographics.address}

)}
)}

Time Period

Data shown for {getTimePeriod()} {filterCategory !== "all" ? ` (${filterCategory} category)` : ""}

{district.demographics ? (

Population

Total: {formatNumber(district.demographics.population || 0)}

Density: {formatNumber(district.demographics.population_density || 0)} people/km²

Unemployment

{formatNumber(district.demographics.number_of_unemployed || 0)} unemployed people

{district.demographics.population && district.demographics.number_of_unemployed && (

Rate:{" "} {( (district.demographics.number_of_unemployed / district.demographics.population) * 100 ).toFixed(1)} %

)}

Crime Rate

{district.number_of_crime && district.demographics.population ? (

{((district.number_of_crime / district.demographics.population) * 10000).toFixed(2)} crime incidents per 10,000 people

) : (

No data available

)}
) : (

No demographic data available for this district.

)}
{allCrimeIncidents && allCrimeIncidents.length > 0 ? (
{allCrimeIncidents.map((incident, index) => (
{incident.category || incident.type || "Unknown"} {incident.status || "unknown"}

{incident.description || "No description"}

{incident.timestamp ? new Date(incident.timestamp).toLocaleString() : "Unknown date"}

))} {district.number_of_crime > allCrimeIncidents.length && (

Showing {allCrimeIncidents.length} of {district.number_of_crime} total incidents {filterCategory !== "all" ? ` for ${filterCategory} category` : ""}

)}
) : (

No crime incidents available to display{filterCategory !== "all" ? ` for ${filterCategory}` : ""}.

Total reported incidents: {district.number_of_crime || 0}

)}
{/* Connection line */}
{/* Connection dot */}
) }