"use client" import React, { useState, useEffect } from "react" import { AlertTriangle, Calendar, Clock, MapPin, X, ChevronRight } from 'lucide-react' import { Card, CardHeader, CardTitle, CardDescription } from "@/app/_components/ui/card" import { cn } from "@/app/_lib/utils" import { Tabs, TabsList, TabsTrigger, TabsContent } from "@/app/_components/ui/tabs" import { Button } from "@/app/_components/ui/button" import { Skeleton } from "@/app/_components/ui/skeleton" import { useMap } from "react-map-gl/mapbox" import { ICrimes } from "@/app/_utils/types/crimes" // Import sidebar components import { SidebarIncidentsTab } from "./tabs/incidents-tab" import { useCrimeAnalytics } from "../../../(pages)/(admin)/dashboard/crime-management/crime-overview/_hooks/use-crime-analytics" import { usePagination } from "../../../_hooks/use-pagination" import { getMonthName } from "@/app/_utils/common" import { SidebarInfoTab } from "./tabs/info-tab" import { SidebarStatisticsTab } from "./tabs/statistics-tab" interface CrimeSidebarProps { className?: string defaultCollapsed?: boolean selectedCategory?: string | "all" selectedYear: number selectedMonth?: number | "all" crimes: ICrimes[] isLoading?: boolean } export default function CrimeSidebar({ className, defaultCollapsed = true, selectedCategory = "all", selectedYear, selectedMonth, crimes = [], isLoading = false, }: CrimeSidebarProps) { const [isCollapsed, setIsCollapsed] = useState(defaultCollapsed) const [activeTab, setActiveTab] = useState("incidents") const [activeIncidentTab, setActiveIncidentTab] = useState("recent") const [currentTime, setCurrentTime] = useState(new Date()) const [location, setLocation] = useState("Jember, East Java") // Get the map instance to use for flyTo const { current: map } = useMap() // Use custom hooks for analytics and pagination const crimeStats = useCrimeAnalytics(crimes) const { paginationState, handlePageChange } = usePagination(crimeStats.availableMonths) // Update current time every minute for the real-time display useEffect(() => { const timer = setInterval(() => { setCurrentTime(new Date()) }, 60000) return () => clearInterval(timer) }, []) // Format date with selected year and month if provided const getDisplayDate = () => { // If we have a specific month selected, use that for display if (selectedMonth && selectedMonth !== 'all') { const date = new Date() date.setFullYear(selectedYear) date.setMonth(Number(selectedMonth) - 1) // Month is 0-indexed in JS Date return new Intl.DateTimeFormat('en-US', { year: 'numeric', month: 'long' }).format(date) } // Otherwise show today's date return new Intl.DateTimeFormat('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }).format(currentTime) } const formattedDate = getDisplayDate() const formattedTime = new Intl.DateTimeFormat('en-US', { hour: '2-digit', minute: '2-digit', hour12: true }).format(currentTime) // Generate a time period display for the current view const getTimePeriodDisplay = () => { if (selectedMonth && selectedMonth !== 'all') { return `${getMonthName(Number(selectedMonth))} ${selectedYear}` } return `${selectedYear} - All months` } // Function to fly to incident location when clicked const handleIncidentClick = (incident: any) => { if (!map || !incident.longitude || !incident.latitude) return // Fly to the incident location map.flyTo({ center: [incident.longitude, incident.latitude], zoom: 15, pitch: 0, bearing: 0, duration: 1500, easing: (t) => t * (2 - t), // easeOutQuad }) // Create and dispatch a custom event for the incident click const customEvent = new CustomEvent("incident_click", { detail: incident, bubbles: true }) if (map.getMap().getCanvas()) { map.getMap().getCanvas().dispatchEvent(customEvent) } else { document.dispatchEvent(customEvent) } } return (
{/* Header with improved styling */}
Crime Analysis {!isLoading && ( {getTimePeriodDisplay()} )}
{/* Improved tabs with pill style */} Dashboard Statistics Information
{isLoading ? (
) : ( <> )}
) }