feat: implement centralized layer visibility management across map layers
This commit is contained in:
parent
ba191c5e88
commit
952bdbed8f
|
@ -1,11 +1,11 @@
|
||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import { useEffect, useCallback } from "react"
|
import { useEffect, useCallback } from "react"
|
||||||
|
|
||||||
import mapboxgl from "mapbox-gl"
|
import mapboxgl from "mapbox-gl"
|
||||||
import type { GeoJSON } from "geojson"
|
import type { GeoJSON } from "geojson"
|
||||||
import type { IClusterLayerProps } from "@/app/_utils/types/map"
|
import type { IClusterLayerProps } from "@/app/_utils/types/map"
|
||||||
import { extractCrimeIncidents } from "@/app/_utils/map"
|
import { extractCrimeIncidents } from "@/app/_utils/map"
|
||||||
|
import { manageLayerVisibility } from "@/app/_utils/map/layer-visibility"
|
||||||
|
|
||||||
interface ExtendedClusterLayerProps extends IClusterLayerProps {
|
interface ExtendedClusterLayerProps extends IClusterLayerProps {
|
||||||
clusteringEnabled?: boolean
|
clusteringEnabled?: boolean
|
||||||
|
@ -23,6 +23,9 @@ export default function ClusterLayer({
|
||||||
showClusters = false,
|
showClusters = false,
|
||||||
sourceType = "cbt",
|
sourceType = "cbt",
|
||||||
}: ExtendedClusterLayerProps) {
|
}: ExtendedClusterLayerProps) {
|
||||||
|
// Define layer IDs for consistent management
|
||||||
|
const LAYER_IDS = ['clusters', 'cluster-count', 'crime-points', 'crime-count-labels'];
|
||||||
|
|
||||||
const handleClusterClick = useCallback(
|
const handleClusterClick = useCallback(
|
||||||
(e: any) => {
|
(e: any) => {
|
||||||
if (!map) return
|
if (!map) return
|
||||||
|
@ -64,6 +67,13 @@ export default function ClusterLayer({
|
||||||
[map],
|
[map],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Use centralized layer visibility management
|
||||||
|
useEffect(() => {
|
||||||
|
if (!map) return;
|
||||||
|
|
||||||
|
return manageLayerVisibility(map, LAYER_IDS, visible && showClusters && !focusedDistrictId);
|
||||||
|
}, [map, visible, showClusters, focusedDistrictId]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!map || !visible) return
|
if (!map || !visible) return
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
import { getCrimeRateColor, getFillOpacity } from "@/app/_utils/map"
|
import { getCrimeRateColor, getFillOpacity } from "@/app/_utils/map"
|
||||||
import type { IExtrusionLayerProps } from "@/app/_utils/types/map"
|
import type { IExtrusionLayerProps } from "@/app/_utils/types/map"
|
||||||
import { useEffect, useRef } from "react"
|
import { useEffect, useRef } from "react"
|
||||||
|
import { manageLayerVisibility } from "@/app/_utils/map/layer-visibility"
|
||||||
|
|
||||||
export default function DistrictExtrusionLayer({
|
export default function DistrictExtrusionLayer({
|
||||||
visible = true,
|
visible = true,
|
||||||
|
@ -17,6 +18,9 @@ export default function DistrictExtrusionLayer({
|
||||||
const extrusionCreatedRef = useRef(false)
|
const extrusionCreatedRef = useRef(false)
|
||||||
const lastFocusedDistrictRef = useRef<string | null>(null)
|
const lastFocusedDistrictRef = useRef<string | null>(null)
|
||||||
|
|
||||||
|
// Define layer IDs for consistent management
|
||||||
|
const LAYER_IDS = ['district-extrusion'];
|
||||||
|
|
||||||
// Helper to (re)create the extrusion layer
|
// Helper to (re)create the extrusion layer
|
||||||
const createExtrusionLayer = () => {
|
const createExtrusionLayer = () => {
|
||||||
if (!map) return
|
if (!map) return
|
||||||
|
@ -51,7 +55,6 @@ export default function DistrictExtrusionLayer({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Create the extrusion layer
|
// Create the extrusion layer
|
||||||
map.addLayer(
|
map.addLayer(
|
||||||
{
|
{
|
||||||
|
@ -339,5 +342,23 @@ export default function DistrictExtrusionLayer({
|
||||||
rotationAnimationRef.current = requestAnimationFrame(animate)
|
rotationAnimationRef.current = requestAnimationFrame(animate)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use centralized layer visibility management
|
||||||
|
useEffect(() => {
|
||||||
|
if (!map) return;
|
||||||
|
|
||||||
|
// Special case: also cancel animations when hiding the layer
|
||||||
|
return manageLayerVisibility(map, LAYER_IDS, visible && !!focusedDistrictId, () => {
|
||||||
|
if (!visible && animationRef.current) {
|
||||||
|
cancelAnimationFrame(animationRef.current);
|
||||||
|
animationRef.current = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!visible && rotationAnimationRef.current) {
|
||||||
|
cancelAnimationFrame(rotationAnimationRef.current);
|
||||||
|
rotationAnimationRef.current = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, [map, visible, focusedDistrictId]);
|
||||||
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import React, { useEffect } from 'react';
|
import React, { useEffect } from 'react';
|
||||||
import mapboxgl from 'mapbox-gl';
|
import mapboxgl from 'mapbox-gl';
|
||||||
|
import { manageLayerVisibility } from "@/app/_utils/map/layer-visibility"
|
||||||
|
|
||||||
interface FaultLinesLayerProps {
|
interface FaultLinesLayerProps {
|
||||||
map: mapboxgl.Map | null;
|
map: mapboxgl.Map | null;
|
||||||
|
@ -9,16 +10,16 @@ interface FaultLinesLayerProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function FaultLinesLayer({ map, visible = true }: FaultLinesLayerProps) {
|
export default function FaultLinesLayer({ map, visible = true }: FaultLinesLayerProps) {
|
||||||
|
// Define layer IDs for consistent management
|
||||||
|
const LAYER_IDS = ['indo-faults-line-layer'];
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!map) return;
|
if (!map) return;
|
||||||
|
|
||||||
// Function to add fault lines layer
|
// Function to add fault lines layer
|
||||||
function addFaultLines() {
|
const setupFaultLines = () => {
|
||||||
const sourceId = 'indo_faults_lines';
|
const sourceId = 'indo-faults-lines';
|
||||||
const layerId = 'indo_faults_line_layer';
|
const layerId = 'indo-faults-line-layer';
|
||||||
|
|
||||||
// Make sure map is defined
|
|
||||||
if (!map) return;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Check if the source already exists
|
// Check if the source already exists
|
||||||
|
@ -45,55 +46,22 @@ export default function FaultLinesLayer({ map, visible = true }: FaultLinesLayer
|
||||||
'line-opacity': 0.5
|
'line-opacity': 0.5
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else if (map.getLayer(layerId)) {
|
|
||||||
// If the layer exists, just update its visibility
|
|
||||||
map.setLayoutProperty(
|
|
||||||
layerId,
|
|
||||||
'visibility',
|
|
||||||
visible ? 'visible' : 'none'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use the manageLayerVisibility utility to handle layer visibility
|
||||||
|
manageLayerVisibility(map, LAYER_IDS, visible);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn("Error adding fault lines:", error);
|
console.warn("Error adding fault lines:", error);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
// Function to clean up layers
|
|
||||||
function cleanupFaultLines() {
|
|
||||||
try {
|
|
||||||
if (!map || !map.getStyle()) return;
|
|
||||||
|
|
||||||
const layerId = 'indo_faults_line_layer';
|
|
||||||
const sourceId = 'indo_faults_lines';
|
|
||||||
|
|
||||||
if (map.getLayer(layerId)) {
|
|
||||||
map.removeLayer(layerId);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (map.getSource(sourceId)) {
|
|
||||||
map.removeSource(sourceId);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.warn("Error cleaning up fault lines:", error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let timeoutId: NodeJS.Timeout | undefined;
|
|
||||||
|
|
||||||
// Try to add the layers now or set up listeners for when map is ready
|
// Try to add the layers now or set up listeners for when map is ready
|
||||||
try {
|
try {
|
||||||
if (map.loaded() && map.isStyleLoaded()) {
|
if (map.loaded() && map.isStyleLoaded()) {
|
||||||
addFaultLines();
|
setupFaultLines();
|
||||||
} else {
|
} else {
|
||||||
// Use multiple events to catch map ready state
|
// Use multiple events to catch map ready state
|
||||||
map.on('load', addFaultLines);
|
map.once('load', setupFaultLines);
|
||||||
map.on('style.load', addFaultLines);
|
|
||||||
map.on('styledata', addFaultLines);
|
|
||||||
|
|
||||||
// Fallback timeout
|
|
||||||
timeoutId = setTimeout(() => {
|
|
||||||
addFaultLines();
|
|
||||||
}, 2000);
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn("Error setting up fault lines:", error);
|
console.warn("Error setting up fault lines:", error);
|
||||||
|
@ -101,15 +69,9 @@ export default function FaultLinesLayer({ map, visible = true }: FaultLinesLayer
|
||||||
|
|
||||||
// Single cleanup function
|
// Single cleanup function
|
||||||
return () => {
|
return () => {
|
||||||
if (timeoutId) clearTimeout(timeoutId);
|
|
||||||
|
|
||||||
if (map) {
|
if (map) {
|
||||||
map.off('load', addFaultLines);
|
map.off('load', setupFaultLines);
|
||||||
map.off('style.load', addFaultLines);
|
|
||||||
map.off('styledata', addFaultLines);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanupFaultLines();
|
|
||||||
};
|
};
|
||||||
}, [map, visible]);
|
}, [map, visible]);
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import { Layer, Source } from "react-map-gl/mapbox";
|
import { Layer, Source } from "react-map-gl/mapbox";
|
||||||
import { useMemo } from "react";
|
import { useMemo, useEffect } from "react";
|
||||||
import { ICrimes } from "@/app/_utils/types/crimes";
|
import { ICrimes } from "@/app/_utils/types/crimes";
|
||||||
|
import { manageLayerVisibility } from "@/app/_utils/map/layer-visibility";
|
||||||
|
import type mapboxgl from "mapbox-gl";
|
||||||
|
|
||||||
interface HeatmapLayerProps {
|
interface HeatmapLayerProps {
|
||||||
crimes: ICrimes[];
|
crimes: ICrimes[];
|
||||||
|
@ -9,8 +11,9 @@ interface HeatmapLayerProps {
|
||||||
filterCategory: string | "all";
|
filterCategory: string | "all";
|
||||||
visible?: boolean;
|
visible?: boolean;
|
||||||
useAllData?: boolean;
|
useAllData?: boolean;
|
||||||
enableInteractions?: boolean; // Add new prop
|
enableInteractions?: boolean;
|
||||||
setFocusedDistrictId?: (id: string | null, isMarkerClick?: boolean) => void; // Add new prop
|
setFocusedDistrictId?: (id: string | null, isMarkerClick?: boolean) => void;
|
||||||
|
map?: mapboxgl.Map | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function HeatmapLayer({
|
export default function HeatmapLayer({
|
||||||
|
@ -21,8 +24,12 @@ export default function HeatmapLayer({
|
||||||
year,
|
year,
|
||||||
month,
|
month,
|
||||||
enableInteractions = true,
|
enableInteractions = true,
|
||||||
setFocusedDistrictId
|
setFocusedDistrictId,
|
||||||
|
map
|
||||||
}: HeatmapLayerProps) {
|
}: HeatmapLayerProps) {
|
||||||
|
// Define layer IDs for consistent management
|
||||||
|
const LAYER_IDS = ['crime-heatmap'];
|
||||||
|
|
||||||
// Convert crime data to GeoJSON format for the heatmap
|
// Convert crime data to GeoJSON format for the heatmap
|
||||||
const heatmapData = useMemo(() => {
|
const heatmapData = useMemo(() => {
|
||||||
const features = crimes.flatMap(crime =>
|
const features = crimes.flatMap(crime =>
|
||||||
|
@ -74,11 +81,14 @@ export default function HeatmapLayer({
|
||||||
};
|
};
|
||||||
}, [crimes, filterCategory, useAllData, year, month]);
|
}, [crimes, filterCategory, useAllData, year, month]);
|
||||||
|
|
||||||
if (!visible) return null;
|
// Manage layer visibility
|
||||||
|
useEffect(() => {
|
||||||
|
if (!map) return;
|
||||||
|
|
||||||
// The heatmap layer doesn't generally support direct interactions like clicks,
|
return manageLayerVisibility(map, LAYER_IDS, visible);
|
||||||
// but we're including the props to maintain consistency with other layers
|
}, [map, visible]);
|
||||||
// and to support future interaction patterns if needed
|
|
||||||
|
if (!visible) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Source id="crime-heatmap-data" type="geojson" data={heatmapData}>
|
<Source id="crime-heatmap-data" type="geojson" data={heatmapData}>
|
||||||
|
|
|
@ -22,13 +22,9 @@ import DistrictFillLineLayer from "./district-layer"
|
||||||
|
|
||||||
import TimezoneLayer from "./timezone"
|
import TimezoneLayer from "./timezone"
|
||||||
import FaultLinesLayer from "./fault-lines"
|
import FaultLinesLayer from "./fault-lines"
|
||||||
import EWSAlertLayer from "./ews-alert-layer"
|
|
||||||
import PanicButtonDemo from "../controls/panic-button-demo"
|
|
||||||
|
|
||||||
import type { IIncidentLog } from "@/app/_utils/types/ews"
|
|
||||||
import { addMockIncident, getAllIncidents, resolveIncident } from "@/app/_utils/mock/ews-data"
|
|
||||||
import RecentIncidentsLayer from "./recent-incidents-layer"
|
import RecentIncidentsLayer from "./recent-incidents-layer"
|
||||||
import IncidentPopup from "../pop-up/incident-popup"
|
import IncidentPopup from "../pop-up/incident-popup"
|
||||||
|
import { manageLayerVisibility } from "@/app/_utils/map/layer-visibility"
|
||||||
|
|
||||||
// Interface for crime incident
|
// Interface for crime incident
|
||||||
interface ICrimeIncident {
|
interface ICrimeIncident {
|
||||||
|
@ -112,33 +108,6 @@ export default function Layers({
|
||||||
|
|
||||||
const crimeDataByDistrict = processCrimeDataByDistrict(crimes)
|
const crimeDataByDistrict = processCrimeDataByDistrict(crimes)
|
||||||
|
|
||||||
const [ewsIncidents, setEwsIncidents] = useState<IIncidentLog[]>([])
|
|
||||||
const [showPanicDemo, setShowPanicDemo] = useState(true)
|
|
||||||
const [displayPanicDemo, setDisplayPanicDemo] = useState(showEWS && showPanicDemo)
|
|
||||||
|
|
||||||
// useEffect(() => {
|
|
||||||
// setEwsIncidents(getAllIncidents())
|
|
||||||
// }, [])
|
|
||||||
|
|
||||||
// const handleTriggerAlert = useCallback((priority: "high" | "medium" | "low") => {
|
|
||||||
// const newIncident = addMockIncident({ priority })
|
|
||||||
// setEwsIncidents(getAllIncidents())
|
|
||||||
// }, [])
|
|
||||||
|
|
||||||
// const handleResolveIncident = useCallback((id: string) => {
|
|
||||||
// resolveIncident(id)
|
|
||||||
// setEwsIncidents(getAllIncidents())
|
|
||||||
// }, [])
|
|
||||||
|
|
||||||
// const handleResolveAllAlerts = useCallback(() => {
|
|
||||||
// ewsIncidents.forEach((incident) => {
|
|
||||||
// if (incident.status === "active") {
|
|
||||||
// resolveIncident(incident.id)
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
// setEwsIncidents(getAllIncidents())
|
|
||||||
// }, [ewsIncidents])
|
|
||||||
|
|
||||||
const handlePopupClose = useCallback(() => {
|
const handlePopupClose = useCallback(() => {
|
||||||
selectedDistrictRef.current = null
|
selectedDistrictRef.current = null
|
||||||
setSelectedDistrict(null)
|
setSelectedDistrict(null)
|
||||||
|
@ -167,8 +136,6 @@ export default function Layers({
|
||||||
const fillColorExpression = createFillColorExpression(null, crimeDataByDistrict)
|
const fillColorExpression = createFillColorExpression(null, crimeDataByDistrict)
|
||||||
map.getMap().setPaintProperty("district-fill", "fill-color", fillColorExpression as any)
|
map.getMap().setPaintProperty("district-fill", "fill-color", fillColorExpression as any)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}, [map, crimeDataByDistrict])
|
}, [map, crimeDataByDistrict])
|
||||||
|
|
||||||
|
@ -248,26 +215,17 @@ export default function Layers({
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleCloseDistrictPopup = useCallback(() => {
|
const handleCloseDistrictPopup = useCallback(() => {
|
||||||
// console.log("Closing district popup")
|
|
||||||
|
|
||||||
animateExtrusionDown()
|
animateExtrusionDown()
|
||||||
handlePopupClose()
|
handlePopupClose()
|
||||||
}, [handlePopupClose, animateExtrusionDown])
|
}, [handlePopupClose, animateExtrusionDown])
|
||||||
|
|
||||||
const handleDistrictClick = useCallback(
|
const handleDistrictClick = useCallback(
|
||||||
(feature: IDistrictFeature) => {
|
(feature: IDistrictFeature) => {
|
||||||
// console.log("District clicked:", feature)
|
|
||||||
|
|
||||||
// If we're currently interacting with a marker, don't process district click
|
|
||||||
if (isInteractingWithMarker.current) {
|
if (isInteractingWithMarker.current) {
|
||||||
// console.log("Ignoring district click because we're interacting with a marker")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear any existing incident selection
|
|
||||||
setSelectedIncident(null)
|
setSelectedIncident(null)
|
||||||
|
|
||||||
// Set the district as selected
|
|
||||||
setSelectedDistrict(feature)
|
setSelectedDistrict(feature)
|
||||||
selectedDistrictRef.current = feature
|
selectedDistrictRef.current = feature
|
||||||
setFocusedDistrictId(feature.id)
|
setFocusedDistrictId(feature.id)
|
||||||
|
@ -282,7 +240,6 @@ export default function Layers({
|
||||||
easing: (t) => t * (2 - t),
|
easing: (t) => t * (2 - t),
|
||||||
})
|
})
|
||||||
|
|
||||||
// Hide clusters when focusing on a district
|
|
||||||
if (map.getLayer("clusters")) {
|
if (map.getLayer("clusters")) {
|
||||||
map.getMap().setLayoutProperty("clusters", "visibility", "none")
|
map.getMap().setLayoutProperty("clusters", "visibility", "none")
|
||||||
}
|
}
|
||||||
|
@ -403,13 +360,9 @@ export default function Layers({
|
||||||
}, [crimes, filterCategory, year, month, crimeDataByDistrict])
|
}, [crimes, filterCategory, year, month, crimeDataByDistrict])
|
||||||
|
|
||||||
const handleSetFocusedDistrictId = useCallback((id: string | null, isMarkerClick = false) => {
|
const handleSetFocusedDistrictId = useCallback((id: string | null, isMarkerClick = false) => {
|
||||||
// console.log("Setting focused district ID:", id, "from marker click:", isMarkerClick)
|
|
||||||
|
|
||||||
// If this is from a marker click, set the marker interaction flag
|
|
||||||
if (isMarkerClick) {
|
if (isMarkerClick) {
|
||||||
isInteractingWithMarker.current = true
|
isInteractingWithMarker.current = true
|
||||||
|
|
||||||
// Reset the flag after a delay
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
isInteractingWithMarker.current = false
|
isInteractingWithMarker.current = false
|
||||||
}, 1000)
|
}, 1000)
|
||||||
|
@ -431,33 +384,41 @@ export default function Layers({
|
||||||
activeControl === "recents"
|
activeControl === "recents"
|
||||||
const showIncidentMarkers = activeControl !== "heatmap" && activeControl !== "timeline" && sourceType !== "cbu"
|
const showIncidentMarkers = activeControl !== "heatmap" && activeControl !== "timeline" && sourceType !== "cbu"
|
||||||
|
|
||||||
// Ensure showPanicDemo is always defined
|
|
||||||
// const [displayPanicDemo, setDisplayPanicDemo] = useState(showEWS && showPanicDemo)
|
|
||||||
|
|
||||||
// Always render the DistrictExtrusionLayer when a district is focused
|
|
||||||
// This ensures it's available when needed
|
|
||||||
const shouldShowExtrusion = focusedDistrictId !== null && !isInteractingWithMarker.current
|
const shouldShowExtrusion = focusedDistrictId !== null && !isInteractingWithMarker.current
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!mapboxMap) return;
|
if (!mapboxMap) return;
|
||||||
|
|
||||||
const ensureLayerVisibility = () => {
|
const recentLayerIds = ["very-recent-incidents-pulse", "recent-incidents-glow", "recent-incidents"];
|
||||||
|
const timelineLayerIds = ["timeline-markers-bg", "timeline-markers"];
|
||||||
|
const heatmapLayerIds = ["heatmap-layer"];
|
||||||
|
const unitsLayerIds = ["units-points", "incidents-points", "units-labels", "units-connection-lines"];
|
||||||
|
const clusterLayerIds = ["clusters", "cluster-count", "crime-points", "crime-count-labels"];
|
||||||
|
const unclusteredLayerIds = ["unclustered-point"];
|
||||||
|
|
||||||
if (activeControl !== "recents") {
|
if (activeControl !== "recents") {
|
||||||
const recentLayerIds = [
|
manageLayerVisibility(mapboxMap, recentLayerIds, false);
|
||||||
"very-recent-incidents-pulse",
|
|
||||||
"recent-incidents-glow",
|
|
||||||
"recent-incidents"
|
|
||||||
];
|
|
||||||
|
|
||||||
recentLayerIds.forEach(layerId => {
|
|
||||||
if (mapboxMap.getLayer(layerId)) {
|
|
||||||
mapboxMap.setLayoutProperty(layerId, "visibility", "none");
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
ensureLayerVisibility();
|
if (activeControl !== "timeline") {
|
||||||
|
manageLayerVisibility(mapboxMap, timelineLayerIds, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (activeControl !== "heatmap") {
|
||||||
|
manageLayerVisibility(mapboxMap, heatmapLayerIds, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (activeControl !== "units") {
|
||||||
|
manageLayerVisibility(mapboxMap, unitsLayerIds, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (activeControl !== "clusters") {
|
||||||
|
manageLayerVisibility(mapboxMap, clusterLayerIds, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (activeControl !== "incidents" && activeControl !== "recents" && activeControl !== "historical") {
|
||||||
|
manageLayerVisibility(mapboxMap, unclusteredLayerIds, false);
|
||||||
|
}
|
||||||
|
|
||||||
}, [activeControl, mapboxMap]);
|
}, [activeControl, mapboxMap]);
|
||||||
|
|
||||||
|
@ -479,7 +440,6 @@ export default function Layers({
|
||||||
onDistrictClick={handleDistrictClick}
|
onDistrictClick={handleDistrictClick}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* Always render the extrusion layer when a district is focused */}
|
|
||||||
{shouldShowExtrusion && (
|
{shouldShowExtrusion && (
|
||||||
<DistrictExtrusionLayer
|
<DistrictExtrusionLayer
|
||||||
visible={true}
|
visible={true}
|
||||||
|
@ -559,20 +519,6 @@ export default function Layers({
|
||||||
<TimezoneLayer map={mapboxMap} />
|
<TimezoneLayer map={mapboxMap} />
|
||||||
|
|
||||||
<FaultLinesLayer map={mapboxMap} />
|
<FaultLinesLayer map={mapboxMap} />
|
||||||
|
|
||||||
{/* {showEWS && <EWSAlertLayer map={mapboxMap} incidents={ewsIncidents} onIncidentResolved={handleResolveIncident} />} */}
|
|
||||||
|
|
||||||
{/* {showEWS && displayPanicDemo && (
|
|
||||||
<div className="absolute top-0 right-20 z-50 p-2">
|
|
||||||
<PanicButtonDemo
|
|
||||||
onTriggerAlert={handleTriggerAlert}
|
|
||||||
onResolveAllAlerts={handleResolveAllAlerts}
|
|
||||||
activeIncidents={ewsIncidents.filter((inc) => inc.status === "active")}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
)} */}
|
|
||||||
|
|
||||||
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,8 @@ import type { IIncidentLogs } from "@/app/_utils/types/crimes"
|
||||||
import { BASE_BEARING, BASE_DURATION, BASE_PITCH, BASE_ZOOM, PITCH_3D, ZOOM_3D } from "@/app/_utils/const/map"
|
import { BASE_BEARING, BASE_DURATION, BASE_PITCH, BASE_ZOOM, PITCH_3D, ZOOM_3D } from "@/app/_utils/const/map"
|
||||||
import IncidentLogsPopup from "../pop-up/incident-logs-popup"
|
import IncidentLogsPopup from "../pop-up/incident-logs-popup"
|
||||||
import type mapboxgl from "mapbox-gl"
|
import type mapboxgl from "mapbox-gl"
|
||||||
import type { MapMouseEvent, MapGeoJSONFeature } from "react-map-gl/mapbox"
|
import type { MapGeoJSONFeature, MapMouseEvent } from "react-map-gl/mapbox"
|
||||||
|
import { manageLayerVisibility } from "@/app/_utils/map/layer-visibility"
|
||||||
|
|
||||||
interface IRecentIncidentsLayerProps {
|
interface IRecentIncidentsLayerProps {
|
||||||
visible?: boolean
|
visible?: boolean
|
||||||
|
@ -46,8 +47,12 @@ export default function RecentIncidentsLayer({ visible = false, map, incidents =
|
||||||
const animationFrameRef = useRef<number | null>(null)
|
const animationFrameRef = useRef<number | null>(null)
|
||||||
const [selectedIncident, setSelectedIncident] = useState<IIncidentDetails | null>(null)
|
const [selectedIncident, setSelectedIncident] = useState<IIncidentDetails | null>(null)
|
||||||
|
|
||||||
// Add a ref to track if layers are initialized
|
// Define layer IDs once to be consistent
|
||||||
const layersInitialized = useRef(false);
|
const LAYER_IDS = [
|
||||||
|
"very-recent-incidents-pulse",
|
||||||
|
"recent-incidents-glow",
|
||||||
|
"recent-incidents"
|
||||||
|
];
|
||||||
|
|
||||||
// Filter incidents from the last 24 hours
|
// Filter incidents from the last 24 hours
|
||||||
const recentIncidents = incidents.filter((incident) => {
|
const recentIncidents = incidents.filter((incident) => {
|
||||||
|
@ -140,40 +145,20 @@ export default function RecentIncidentsLayer({ visible = false, map, incidents =
|
||||||
setSelectedIncident(null)
|
setSelectedIncident(null)
|
||||||
}, [map])
|
}, [map])
|
||||||
|
|
||||||
// This effect handles visibility changes and ensures markers are shown/hidden properly
|
// Effect to manage layer visibility consistently
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!map) return;
|
const cleanup = manageLayerVisibility(map, LAYER_IDS, visible, () => {
|
||||||
|
// When layers become invisible, close any open popup
|
||||||
|
if (!visible) setSelectedIncident(null);
|
||||||
|
|
||||||
// Function to update layer visibility
|
// Cancel animation frame when hiding the layer
|
||||||
const updateLayerVisibility = () => {
|
if (!visible && animationFrameRef.current) {
|
||||||
if (!map.isStyleLoaded()) return;
|
cancelAnimationFrame(animationFrameRef.current);
|
||||||
|
animationFrameRef.current = null;
|
||||||
const layers = [
|
|
||||||
"very-recent-incidents-pulse",
|
|
||||||
"recent-incidents-glow",
|
|
||||||
"recent-incidents"
|
|
||||||
];
|
|
||||||
|
|
||||||
layers.forEach(layerId => {
|
|
||||||
if (map.getLayer(layerId)) {
|
|
||||||
map.setLayoutProperty(
|
|
||||||
layerId,
|
|
||||||
"visibility",
|
|
||||||
visible ? "visible" : "none"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// If closing, also close any open popups
|
return cleanup;
|
||||||
if (!visible) {
|
|
||||||
setSelectedIncident(null);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// If layers are initialized, update their visibility
|
|
||||||
if (layersInitialized.current) {
|
|
||||||
updateLayerVisibility();
|
|
||||||
}
|
|
||||||
}, [visible, map]);
|
}, [visible, map]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -403,9 +388,6 @@ export default function RecentIncidentsLayer({ visible = false, map, incidents =
|
||||||
// Ensure click handler is properly registered
|
// Ensure click handler is properly registered
|
||||||
map.off("click", "recent-incidents", handleIncidentClick)
|
map.off("click", "recent-incidents", handleIncidentClick)
|
||||||
map.on("click", "recent-incidents", handleIncidentClick)
|
map.on("click", "recent-incidents", handleIncidentClick)
|
||||||
|
|
||||||
// Mark layers as initialized
|
|
||||||
layersInitialized.current = true;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error setting up recent incidents layer:", error)
|
console.error("Error setting up recent incidents layer:", error)
|
||||||
}
|
}
|
||||||
|
@ -437,16 +419,8 @@ export default function RecentIncidentsLayer({ visible = false, map, incidents =
|
||||||
}
|
}
|
||||||
}, [map, visible, recentIncidents, handleIncidentClick, twoHoursInMs])
|
}, [map, visible, recentIncidents, handleIncidentClick, twoHoursInMs])
|
||||||
|
|
||||||
// Close popup when layer becomes invisible
|
|
||||||
useEffect(() => {
|
|
||||||
if (!visible) {
|
|
||||||
setSelectedIncident(null)
|
|
||||||
}
|
|
||||||
}, [visible])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* Popup component */}
|
|
||||||
{selectedIncident && (
|
{selectedIncident && (
|
||||||
<IncidentLogsPopup
|
<IncidentLogsPopup
|
||||||
longitude={selectedIncident.longitude}
|
longitude={selectedIncident.longitude}
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
"use client"
|
"use client";
|
||||||
|
|
||||||
import React, { useEffect, useRef } from 'react';
|
import React, { useEffect, useRef } from "react";
|
||||||
import mapboxgl from 'mapbox-gl';
|
import mapboxgl from "mapbox-gl";
|
||||||
import { createRoot } from 'react-dom/client';
|
import { manageLayerVisibility } from "@/app/_utils/map/layer-visibility";
|
||||||
import { Badge } from "@/app/_components/ui/badge";
|
import { createRoot } from "react-dom/client";
|
||||||
|
import { Badge } from "../../ui/badge";
|
||||||
|
|
||||||
interface TimezoneLayerProps {
|
interface TimezoneLayerProps {
|
||||||
map: mapboxgl.Map | null;
|
map: mapboxgl.Map | null;
|
||||||
|
visible?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Component to display time in a specific timezone
|
// Component to display time in a specific timezone
|
||||||
|
@ -17,13 +19,13 @@ function Jam({ timeZone }: { timeZone: string }) {
|
||||||
function updateTime() {
|
function updateTime() {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const options: Intl.DateTimeFormatOptions = {
|
const options: Intl.DateTimeFormatOptions = {
|
||||||
hour: '2-digit',
|
hour: "2-digit",
|
||||||
minute: '2-digit',
|
minute: "2-digit",
|
||||||
second: '2-digit', // Added seconds display
|
second: "2-digit", // Added seconds display
|
||||||
hour12: false,
|
hour12: false,
|
||||||
timeZone
|
timeZone,
|
||||||
};
|
};
|
||||||
setTime(now.toLocaleTimeString('id-ID', options));
|
setTime(now.toLocaleTimeString("id-ID", options));
|
||||||
}
|
}
|
||||||
|
|
||||||
updateTime();
|
updateTime();
|
||||||
|
@ -34,50 +36,74 @@ function Jam({ timeZone }: { timeZone: string }) {
|
||||||
return <>{time}</>;
|
return <>{time}</>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function TimezoneLayer({ map }: TimezoneLayerProps) {
|
export default function TimezoneLayer({
|
||||||
|
map,
|
||||||
|
visible = true,
|
||||||
|
}: TimezoneLayerProps) {
|
||||||
const hoverTimezone = useRef<any>(null);
|
const hoverTimezone = useRef<any>(null);
|
||||||
const markersRef = useRef<mapboxgl.Marker[]>([]);
|
const markersRef = useRef<mapboxgl.Marker[]>([]);
|
||||||
|
|
||||||
|
// Define layer IDs for consistent management
|
||||||
|
const LAYER_IDS = ["timezone-layer"];
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!map) return;
|
if (!map) return;
|
||||||
|
|
||||||
// Function to add timezone data and markers
|
// Function to add timezone layer
|
||||||
function addTimezoneLayers() {
|
const setupTimezoneLayer = () => {
|
||||||
|
const sourceId = "timezone-source";
|
||||||
|
const layerId = "timezone-layer";
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Check if the source already exists
|
||||||
|
if (!map.getSource(sourceId)) {
|
||||||
// Add timezone data source
|
// Add timezone data source
|
||||||
const url = "/geojson/timezones_wVVG8.geojson"; // Changed path to public folder
|
const url = "/geojson/timezones_wVVG8.geojson";
|
||||||
if (map && !map.getSource('timezone')) {
|
map.addSource(sourceId, {
|
||||||
map.addSource('timezone', {
|
type: "geojson",
|
||||||
'type': 'geojson',
|
generateId: true,
|
||||||
'generateId': true,
|
data: url,
|
||||||
'data': url
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add timezone boundaries
|
// Add a line layer to visualize the timezones
|
||||||
map.addLayer({
|
map.addLayer({
|
||||||
'id': 'timezone-line',
|
id: layerId,
|
||||||
'type': 'line',
|
type: "line",
|
||||||
'source': 'timezone',
|
source: sourceId,
|
||||||
'layout': {},
|
layout: {
|
||||||
'paint': {
|
visibility: visible ? "visible" : "none",
|
||||||
'line-color': 'orange',
|
},
|
||||||
'line-width': 1,
|
paint: {
|
||||||
'line-opacity': 0.5
|
"line-color": "green",
|
||||||
}
|
"line-width": 1,
|
||||||
|
"line-opacity": 0.5,
|
||||||
|
"line-dasharray": [2, 2],
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Create markers for Indonesian time zones
|
// Create markers for Indonesian time zones
|
||||||
const createTimeMarker = (lngLat: [number, number], timeZone: string, label: string) => {
|
const createTimeMarker = (
|
||||||
const markerElement = document.createElement('div');
|
lngLat: [number, number],
|
||||||
|
timeZone: string,
|
||||||
|
label: string,
|
||||||
|
) => {
|
||||||
|
const markerElement = document.createElement("div");
|
||||||
const root = createRoot(markerElement);
|
const root = createRoot(markerElement);
|
||||||
root.render(
|
root.render(
|
||||||
<div className='bordered p-1 text-time show-pop-up text-center'>
|
<div className="bordered p-1 text-time show-pop-up text-center">
|
||||||
<p className="uppercase text-xl" style={{
|
<p
|
||||||
lineHeight: "1rem"
|
className="uppercase text-xl"
|
||||||
}}>
|
style={{
|
||||||
|
lineHeight: "1rem",
|
||||||
|
}}
|
||||||
|
>
|
||||||
<Jam timeZone={timeZone} />
|
<Jam timeZone={timeZone} />
|
||||||
</p>
|
</p>
|
||||||
<Badge variant="outline" className="text-xs mt-1">{label}</Badge>
|
<Badge variant="outline" className="text-xs mt-1">
|
||||||
</div>
|
{label}
|
||||||
|
</Badge>
|
||||||
|
</div>,
|
||||||
);
|
);
|
||||||
|
|
||||||
const marker = new mapboxgl.Marker(markerElement)
|
const marker = new mapboxgl.Marker(markerElement)
|
||||||
|
@ -89,59 +115,52 @@ export default function TimezoneLayer({ map }: TimezoneLayerProps) {
|
||||||
};
|
};
|
||||||
|
|
||||||
// WIB (GMT+7)
|
// WIB (GMT+7)
|
||||||
createTimeMarker([107.4999769225339, 3.4359354227361933], "Asia/Jakarta", "WIB / GMT+7");
|
createTimeMarker(
|
||||||
|
[107.4999769225339, 3.4359354227361933],
|
||||||
|
"Asia/Jakarta",
|
||||||
|
"WIB / GMT+7",
|
||||||
|
);
|
||||||
|
|
||||||
// WITA (GMT+8)
|
// WITA (GMT+8)
|
||||||
createTimeMarker([119.1174733337183, 3.4359354227361933], "Asia/Makassar", "WITA / GMT+8");
|
createTimeMarker(
|
||||||
|
[119.1174733337183, 3.4359354227361933],
|
||||||
|
"Asia/Makassar",
|
||||||
|
"WITA / GMT+8",
|
||||||
|
);
|
||||||
|
|
||||||
// WIT (GMT+9)
|
// WIT (GMT+9)
|
||||||
createTimeMarker([131.58387377752751, 3.4359354227361933], "Asia/Jayapura", "WIT / GMT+9");
|
createTimeMarker(
|
||||||
|
[131.58387377752751, 3.4359354227361933],
|
||||||
|
"Asia/Jayapura",
|
||||||
|
"WIT / GMT+9",
|
||||||
|
);
|
||||||
|
|
||||||
|
// Use the manageLayerVisibility utility to handle layer visibility
|
||||||
|
manageLayerVisibility(map, LAYER_IDS, visible);
|
||||||
|
} catch (error) {
|
||||||
|
console.warn("Error adding timezone layer:", error);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Check if style is loaded, otherwise wait for it
|
|
||||||
if (map.isStyleLoaded()) {
|
|
||||||
addTimezoneLayers();
|
|
||||||
} else {
|
|
||||||
// Use both 'load' and 'style.load' events to ensure we catch the style loading
|
|
||||||
map.on('load', addTimezoneLayers);
|
|
||||||
map.on('style.load', addTimezoneLayers);
|
|
||||||
|
|
||||||
// Fallback: If style hasn't loaded within 2 seconds, try again
|
|
||||||
const timeoutId = setTimeout(() => {
|
|
||||||
if (map.isStyleLoaded()) {
|
|
||||||
addTimezoneLayers();
|
|
||||||
}
|
|
||||||
}, 2000);
|
|
||||||
|
|
||||||
// Clean up the timeout if component unmounts
|
|
||||||
return () => {
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
map.off('load', addTimezoneLayers);
|
|
||||||
map.off('style.load', addTimezoneLayers);
|
|
||||||
|
|
||||||
cleanupLayers();
|
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
function cleanupLayers() {
|
// Try to add the layers now or set up listeners for when map is ready
|
||||||
if (map && map.getLayer('timezone-line')) {
|
try {
|
||||||
map.removeLayer('timezone-line');
|
if (map.loaded() && map.isStyleLoaded()) {
|
||||||
|
setupTimezoneLayer();
|
||||||
|
} else {
|
||||||
|
// Use event to catch map ready state
|
||||||
|
map.once("load", setupTimezoneLayer);
|
||||||
}
|
}
|
||||||
if (map && map.getSource('timezone')) {
|
} catch (error) {
|
||||||
map.removeSource('timezone');
|
console.warn("Error setting up timezone layer:", error);
|
||||||
}
|
|
||||||
|
|
||||||
// Remove all markers
|
|
||||||
markersRef.current.forEach(marker => marker.remove());
|
|
||||||
markersRef.current = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cleanup function
|
// Cleanup function
|
||||||
return () => {
|
return () => {
|
||||||
cleanupLayers();
|
if (map) {
|
||||||
|
map.off("load", setupTimezoneLayer);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}, [map]);
|
}, [map, visible]);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import type { IUnclusteredPointLayerProps } from "@/app/_utils/types/map"
|
import type { IUnclusteredPointLayerProps } from "@/app/_utils/types/map"
|
||||||
import { useEffect, useCallback, useRef } from "react"
|
import { useEffect, useCallback, useRef } from "react"
|
||||||
|
import { manageLayerVisibility } from "@/app/_utils/map/layer-visibility"
|
||||||
|
|
||||||
export default function UnclusteredPointLayer({
|
export default function UnclusteredPointLayer({
|
||||||
visible = true,
|
visible = true,
|
||||||
|
@ -13,6 +14,9 @@ export default function UnclusteredPointLayer({
|
||||||
// Add a ref to track if we're currently interacting with a marker
|
// Add a ref to track if we're currently interacting with a marker
|
||||||
const isInteractingWithMarker = useRef(false);
|
const isInteractingWithMarker = useRef(false);
|
||||||
|
|
||||||
|
// Define layer IDs for consistent management
|
||||||
|
const LAYER_IDS = ['unclustered-point'];
|
||||||
|
|
||||||
const handleIncidentClick = useCallback(
|
const handleIncidentClick = useCallback(
|
||||||
(e: any) => {
|
(e: any) => {
|
||||||
if (!map) return
|
if (!map) return
|
||||||
|
@ -73,7 +77,17 @@ export default function UnclusteredPointLayer({
|
||||||
}, 500);
|
}, 500);
|
||||||
},
|
},
|
||||||
[map],
|
[map],
|
||||||
)
|
);
|
||||||
|
|
||||||
|
// Use centralized layer visibility management
|
||||||
|
useEffect(() => {
|
||||||
|
if (!map) return;
|
||||||
|
|
||||||
|
// Special case for this layer: also consider focusedDistrictId
|
||||||
|
const isActuallyVisible = visible && !(focusedDistrictId && !isInteractingWithMarker.current);
|
||||||
|
|
||||||
|
return manageLayerVisibility(map, LAYER_IDS, isActuallyVisible);
|
||||||
|
}, [map, visible, focusedDistrictId]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!map || !visible) return
|
if (!map || !visible) return
|
||||||
|
|
|
@ -13,6 +13,7 @@ import { BASE_BEARING, BASE_DURATION, BASE_PITCH, BASE_ZOOM } from "@/app/_utils
|
||||||
import { INearestUnits } from "@/app/(pages)/(admin)/dashboard/crime-management/units/action"
|
import { INearestUnits } from "@/app/(pages)/(admin)/dashboard/crime-management/units/action"
|
||||||
import { useGetNearestUnitsQuery } from "@/app/(pages)/(admin)/dashboard/crime-management/units/_queries/queries"
|
import { useGetNearestUnitsQuery } from "@/app/(pages)/(admin)/dashboard/crime-management/units/_queries/queries"
|
||||||
import IncidentPopup from "../pop-up/incident-popup"
|
import IncidentPopup from "../pop-up/incident-popup"
|
||||||
|
import { manageLayerVisibility } from "@/app/_utils/map/layer-visibility"
|
||||||
|
|
||||||
interface UnitsLayerProps {
|
interface UnitsLayerProps {
|
||||||
crimes: ICrimes[]
|
crimes: ICrimes[]
|
||||||
|
@ -67,6 +68,14 @@ export default function UnitsLayer({ crimes, units = [], filterCategory, visible
|
||||||
// Add a ref to store pre-processed incidents by district for optimization
|
// Add a ref to store pre-processed incidents by district for optimization
|
||||||
const districtIncidentsCache = useRef<Map<string, IDistrictIncidents[]>>(new Map());
|
const districtIncidentsCache = useRef<Map<string, IDistrictIncidents[]>>(new Map());
|
||||||
|
|
||||||
|
// Define layer IDs for consistent management
|
||||||
|
const LAYER_IDS = [
|
||||||
|
'units-points',
|
||||||
|
'units-symbols',
|
||||||
|
'incidents-points',
|
||||||
|
'units-connection-lines'
|
||||||
|
];
|
||||||
|
|
||||||
// Use either provided units or loaded units
|
// Use either provided units or loaded units
|
||||||
const unitsData = useMemo(() => {
|
const unitsData = useMemo(() => {
|
||||||
return units.length > 0 ? units : loadedUnits || []
|
return units.length > 0 ? units : loadedUnits || []
|
||||||
|
@ -594,6 +603,19 @@ export default function UnitsLayer({ crimes, units = [], filterCategory, visible
|
||||||
}
|
}
|
||||||
}, [map])
|
}, [map])
|
||||||
|
|
||||||
|
// Use centralized layer visibility management
|
||||||
|
useEffect(() => {
|
||||||
|
if (!map) return;
|
||||||
|
|
||||||
|
const cleanup = manageLayerVisibility(map, LAYER_IDS, visible, () => {
|
||||||
|
if (!visible) {
|
||||||
|
handleClosePopup();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return cleanup;
|
||||||
|
}, [map, visible, handleClosePopup]);
|
||||||
|
|
||||||
// Clean up on unmount or when visibility changes
|
// Clean up on unmount or when visibility changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!visible) {
|
if (!visible) {
|
||||||
|
@ -601,15 +623,6 @@ export default function UnitsLayer({ crimes, units = [], filterCategory, visible
|
||||||
}
|
}
|
||||||
}, [visible, handleClosePopup])
|
}, [visible, handleClosePopup])
|
||||||
|
|
||||||
// Debug untuk komponen render
|
|
||||||
// useEffect(() => {
|
|
||||||
// console.log("Render state:", {
|
|
||||||
// selectedUnit: selectedUnit?.code_unit,
|
|
||||||
// selectedIncident: selectedIncident?.id,
|
|
||||||
// visible
|
|
||||||
// })
|
|
||||||
// }, [selectedUnit, selectedIncident, visible])
|
|
||||||
|
|
||||||
if (!visible) return null
|
if (!visible) return null
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -0,0 +1,58 @@
|
||||||
|
import type mapboxgl from "mapbox-gl"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manages visibility for map layers in a consistent way
|
||||||
|
*
|
||||||
|
* @param map The mapbox map instance
|
||||||
|
* @param layerIds Array of layer IDs to manage
|
||||||
|
* @param isVisible Boolean indicating if layers should be visible
|
||||||
|
* @param onCleanup Optional callback function to execute during cleanup
|
||||||
|
* @returns A cleanup function to remove listeners
|
||||||
|
*/
|
||||||
|
export function manageLayerVisibility(
|
||||||
|
map: mapboxgl.Map | null | undefined,
|
||||||
|
layerIds: string[],
|
||||||
|
isVisible: boolean,
|
||||||
|
onCleanup?: () => void
|
||||||
|
): () => void {
|
||||||
|
if (!map) return () => { }
|
||||||
|
|
||||||
|
// Check if map is loaded, if not wait for it
|
||||||
|
if (!map.isStyleLoaded()) {
|
||||||
|
const setupOnLoad = () => {
|
||||||
|
updateLayersVisibility(map, layerIds, isVisible)
|
||||||
|
}
|
||||||
|
|
||||||
|
map.once('load', setupOnLoad)
|
||||||
|
return () => {
|
||||||
|
map.off('load', setupOnLoad)
|
||||||
|
if (onCleanup) onCleanup()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map is loaded, update visibility directly
|
||||||
|
updateLayersVisibility(map, layerIds, isVisible)
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (onCleanup) onCleanup()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates visibility for specified layers
|
||||||
|
*/
|
||||||
|
function updateLayersVisibility(
|
||||||
|
map: mapboxgl.Map,
|
||||||
|
layerIds: string[],
|
||||||
|
isVisible: boolean
|
||||||
|
): void {
|
||||||
|
layerIds.forEach(layerId => {
|
||||||
|
if (map.getLayer(layerId)) {
|
||||||
|
map.setLayoutProperty(
|
||||||
|
layerId,
|
||||||
|
"visibility",
|
||||||
|
isVisible ? "visible" : "none"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue