"use client"; import { useEffect, useState, useRef, useMemo, useCallback } from "react"; import Map, { NavigationControl, MapRef, Popup } from "react-map-gl/maplibre"; import { MapboxOverlay } from "@deck.gl/mapbox"; import { ScatterplotLayer, GeoJsonLayer } from "@deck.gl/layers"; import type { PickingInfo } from "@deck.gl/core"; import "maplibre-gl/dist/maplibre-gl.css"; import type { ChargeFeature, BoundaryFeature, Filters } from "@/types/charging"; import { statusColor, freightColor, BOUNDARY_LINE, LEGEND, type RGBA } from "@/lib/colors"; interface MapDeckProps { chargeFeatures: ChargeFeature[]; boundaryFeatures: BoundaryFeature[]; bounds: [number, number, number, number] | null; filters: Filters; selectedLocationId: string | null; onSelect: (feature: ChargeFeature | null) => void; } const NL_BOUNDS: [[number, number], [number, number]] = [ [3.31, 50.75], [7.21, 53.47], ]; const cursorStyle = (hovering: boolean) => ` .maplibregl-map, .maplibregl-canvas-container, .maplibregl-canvas { cursor: ${hovering ? "pointer" : "default"} !important; } `; export default function MapDeck({ chargeFeatures, boundaryFeatures, bounds, filters, selectedLocationId, onSelect, }: MapDeckProps) { const mapRef = useRef(null); const [deckOverlay, setDeckOverlay] = useState(null); const [hovered, setHovered] = useState(null); useEffect(() => { const overlay = new MapboxOverlay({ interleaved: true, layers: [] }); setDeckOverlay(overlay); return () => overlay.finalize(); }, []); // Split + power-filter the charge features once. const { passenger, freight } = useMemo(() => { const p: ChargeFeature[] = []; const f: ChargeFeature[] = []; for (const feat of chargeFeatures) { if (feat.properties.maxPowerKw < filters.minPowerKw) continue; if (feat.properties.layer === "freight") f.push(feat); else p.push(feat); } return { passenger: p, freight: f }; }, [chargeFeatures, filters.minPowerKw]); const handleHover = useCallback((info: PickingInfo) => { setHovered(info.object ?? null); }, []); const handleClick = useCallback( (info: PickingInfo) => { if (info.object) onSelect(info.object); }, [onSelect], ); const layers = useMemo(() => { const result: unknown[] = []; if (filters.showBoundary && boundaryFeatures.length) { result.push( new GeoJsonLayer({ id: "boundaries", data: { type: "FeatureCollection", features: boundaryFeatures } as never, stroked: true, filled: false, getLineColor: BOUNDARY_LINE as RGBA, getLineWidth: 2, lineWidthMinPixels: 1.5, lineWidthMaxPixels: 3, pickable: false, }), ); } const scatterCommon = { pickable: true, stroked: true, filled: true, radiusUnits: "pixels" as const, lineWidthMinPixels: 0.5, getLineColor: [255, 255, 255, 230] as RGBA, getLineWidth: 1, onHover: handleHover, onClick: handleClick, getPosition: (d: ChargeFeature) => d.geometry.coordinates, }; if (filters.showPassenger) { result.push( new ScatterplotLayer({ ...scatterCommon, id: "passenger", data: passenger, radiusMinPixels: 3, radiusMaxPixels: 10, getRadius: (d) => (d.properties.locationId === selectedLocationId ? 11 : 5), getFillColor: (d) => statusColor(d.properties.status), updateTriggers: { getRadius: [selectedLocationId], getFillColor: [] }, }), ); } if (filters.showFreight) { result.push( new ScatterplotLayer({ ...scatterCommon, id: "freight", data: freight, radiusMinPixels: 5, radiusMaxPixels: 16, getRadius: (d) => d.properties.locationId === selectedLocationId ? 16 : d.properties.isMegawatt ? 11 : 8, getFillColor: (d) => freightColor(d.properties.isMegawatt), getLineColor: (d) => d.properties.isMegawatt ? ([124, 45, 18, 255] as RGBA) : ([255, 255, 255, 230] as RGBA), getLineWidth: (d) => (d.properties.isMegawatt ? 2 : 1), updateTriggers: { getRadius: [selectedLocationId] }, }), ); } return result; }, [ passenger, freight, boundaryFeatures, filters.showPassenger, filters.showFreight, filters.showBoundary, selectedLocationId, handleHover, handleClick, ]); useEffect(() => { if (deckOverlay) deckOverlay.setProps({ layers: layers as never }); }, [deckOverlay, layers]); // Fit to bounds whenever they change (municipality selection). useEffect(() => { if (bounds && mapRef.current) { mapRef.current.fitBounds( [ [bounds[0], bounds[1]], [bounds[2], bounds[3]], ], { padding: 40, duration: 800 }, ); } }, [bounds]); const onMapLoad = useCallback(() => { if (mapRef.current && deckOverlay) { mapRef.current.getMap().addControl(deckOverlay as never); } }, [deckOverlay]); return (
{hovered && (
{hovered.properties.name}
{hovered.properties.city && (
{hovered.properties.city}
)} {hovered.properties.operatorName && (
{hovered.properties.operatorName}
)}
{hovered.properties.layer === "freight" ? "Vracht" : "Personenauto"} {hovered.properties.maxPowerKw > 0 && {hovered.properties.maxPowerKw} kW}
Klik voor details
)}
{/* Legend */}
Personenauto (status)
Beschikbaar
Bezet (laden)
Niet beschikbaar
Onbekend
Logistiek / vracht
Vracht-laadpunt
Megawatt charging
); }