feat(map): update MapLegend component usage and enhance click handler management in DistrictLayer

This commit is contained in:
vergiLgood1 2025-05-04 03:17:54 +07:00
parent 7d01547a02
commit 7916680122
4 changed files with 62 additions and 5 deletions

View File

@ -8,7 +8,7 @@ interface MapLegendProps {
export default function MapLegend({ position = "bottom-right" }: MapLegendProps) {
return (
<Overlay position={position}>
// <Overlay position={position}>
<div className="flex flex-row text-xs font-semibold font-sans text-white">
<div className={`flex items-center gap-1.5 py-0 px-8 rounded-l-md border-y border-1 `} style={{ backgroundColor: `${CRIME_RATE_COLORS.low}` }}>
<span>Low</span>
@ -20,6 +20,6 @@ export default function MapLegend({ position = "bottom-right" }: MapLegendProps)
<span>High</span>
</div>
</div>
</Overlay>
// </Overlay>
)
}

View File

@ -303,8 +303,9 @@ export default function CrimeMap() {
selectedYear={selectedYear}
selectedMonth={selectedMonth}
/>
<div className="absolute bottom-20 right-0 z-10 p-2">
<MapLegend position="bottom-right" />
</div>
</>
)}

View File

@ -716,6 +716,10 @@ export default function DistrictLayer({
CRIME_RATE_COLORS.default,
] as any)
}
// Re-add click handler to ensure it works after timelapse
map.off("click", "district-fill", handleDistrictClick)
map.on("click", "district-fill", handleDistrictClick)
}
} catch (error) {
console.error("Error adding district layers:", error)
@ -735,6 +739,58 @@ export default function DistrictLayer({
}
}, [map, visible, tilesetId, crimes, filterCategory, year, month, handleIncidentClick, handleClusterClick])
// Add an additional effect to ensure click handlers are properly maintained
useEffect(() => {
if (!map || !layersAdded.current) return
// Re-attach the click handler after any data changes
if (map.getMap().getLayer("district-fill")) {
map.off("click", "district-fill", handleDistrictClick)
map.on("click", "district-fill", handleDistrictClick)
}
// Ensure district-extrusion settings are maintained after timelapse
if (map.getMap().getLayer("district-extrusion")) {
map.getMap().setFilter("district-extrusion", ["==", ["get", "kode_kec"], focusedDistrictId || ""]);
// Re-apply the extrusion color based on the focused district
map.getMap().setPaintProperty("district-extrusion", "fill-extrusion-color", [
"case",
["has", "kode_kec"],
[
"match",
["get", "kode_kec"],
focusedDistrictId || "",
crimeDataByDistrict[focusedDistrictId || ""]?.level === "low"
? CRIME_RATE_COLORS.low
: crimeDataByDistrict[focusedDistrictId || ""]?.level === "medium"
? CRIME_RATE_COLORS.medium
: crimeDataByDistrict[focusedDistrictId || ""]?.level === "high"
? CRIME_RATE_COLORS.high
: CRIME_RATE_COLORS.default,
"transparent",
],
"transparent",
]);
// Ensure extrusion height is restored if needed
if (focusedDistrictId) {
map.getMap().setPaintProperty("district-extrusion", "fill-extrusion-height", [
"case",
["has", "kode_kec"],
["match", ["get", "kode_kec"], focusedDistrictId, 800, 0],
0,
]);
}
}
return () => {
if (map) {
map.off("click", "district-fill", handleDistrictClick)
}
}
}, [map, year, month, crimeDataByDistrict, focusedDistrictId])
useEffect(() => {
if (!map || !layersAdded.current) return

View File

@ -160,10 +160,10 @@
/* Mapbox copyright */
.mapbox-logo {
/* display: none; */
display: none !important;
}
.mapboxgl-ctrl-logo {
/* display: none !important; */
display: none !important;
}
/* .mapbox-improve-map {