"use client" import type React from "react" import { useState, useCallback, useEffect, useRef } from "react" import ReactMapGL, { type ViewState, NavigationControl, ScaleControl, type MapRef, FullscreenControl, GeolocateControl, } from "react-map-gl/mapbox" import { BASE_LATITUDE, BASE_LONGITUDE, BASE_ZOOM, MAP_STYLE } from "@/app/_utils/const/map" import { Search } from "lucide-react" import "mapbox-gl/dist/mapbox-gl.css" import MapSidebar from "./controls/map-sidebar" import SidebarToggle from "./controls/map-toggle" import MapControls from "./controls/map-control" import TimeControls from "./controls/time-control" import SeverityIndicator from "./controls/severity-indicator" import { useFullscreen } from "@/app/_hooks/use-fullscreen" import MapFilterControl from "./controls/map-filter-control" interface MapViewProps { children?: React.ReactNode initialViewState?: Partial mapStyle?: string className?: string width?: string | number height?: string | number mapboxApiAccessToken?: string onMoveEnd?: (viewState: ViewState) => void customControls?: React.ReactNode crimes?: Array<{ id: string district_name: string district_id?: string number_of_crime?: number level?: "low" | "medium" | "high" | "critical" incidents: any[] }> selectedYear?: number | string selectedMonth?: number | string } export default function MapView({ children, initialViewState, mapStyle = MAP_STYLE, className = "w-full h-96", width = "100%", height = "100%", mapboxApiAccessToken = process.env.NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN, onMoveEnd, customControls, crimes = [], selectedYear, selectedMonth, }: MapViewProps) { const [mapRef, setMapRef] = useState(null) const [activeControl, setActiveControl] = useState("crime-rate") const [activeTime, setActiveTime] = useState("today") const [sidebarOpen, setSidebarOpen] = useState(false) const mapContainerRef = useRef(null) const { isFullscreen } = useFullscreen(mapContainerRef) const defaultViewState: Partial = { longitude: BASE_LONGITUDE, latitude: BASE_LATITUDE, zoom: BASE_ZOOM, bearing: 0, pitch: 0, ...initialViewState, } const handleMapLoad = useCallback((event: any) => { setMapRef(event.target) }, []) const handleMoveEnd = useCallback( (event: any) => { if (onMoveEnd) { onMoveEnd(event.viewState) } }, [onMoveEnd], ) const handleControlChange = (control: string) => { setActiveControl(control) // Here you would implement logic to change the map display based on the selected control } const handleTimeChange = (time: string) => { setActiveTime(time) // Here you would implement logic to change the time period of data shown } const toggleSidebar = () => { setSidebarOpen(!sidebarOpen) } return (
{/* Custom controls - only show when in fullscreen mode */} {isFullscreen && ( <> {/* Sidebar */} {/* Additional controls that should only appear in fullscreen */}
{/* Make sure customControls is displayed in fullscreen mode */} {customControls} )} {/* Main content with left padding when sidebar is open */}
setMapRef(ref)} mapStyle={mapStyle} mapboxAccessToken={mapboxApiAccessToken} initialViewState={defaultViewState} onLoad={handleMapLoad} onMoveEnd={handleMoveEnd} interactiveLayerIds={["district-fill", "clusters", "unclustered-point"]} attributionControl={false} style={{ width, height }} > {children} {/* GeolocateControl only shown in fullscreen mode */} {isFullscreen && ( )}
{/* Debug indicator - remove in production */}
Fullscreen: {isFullscreen ? "Yes" : "No"}
) }