94 lines
3.7 KiB
JavaScript
94 lines
3.7 KiB
JavaScript
// /hooks/layers/useDynamicDeviceLayers.js
|
|
import { useEffect, useRef, useState } from "react";
|
|
import L from "leaflet";
|
|
import { createAndSetDevices } from "@/utils/devices/createAndSetDevices";
|
|
import { checkOverlappingMarkers } from "@/utils/mapUtils";
|
|
import plusRoundIcon from "@/components/icons/devices/overlapping/PlusRoundIcon";
|
|
import { useSelector } from "react-redux";
|
|
import { selectGisStationsStaticDistrict } from "@/redux/slices/webservice/gisStationsStaticDistrictSlice.js";
|
|
import { selectGisStationsMeasurements } from "@/redux/slices/webservice/gisStationsMeasurementsSlice.js";
|
|
import { selectGisStationsStatusDistrict } from "@/redux/slices/webservice/gisStationsStatusDistrictSlice.js";
|
|
|
|
/**
|
|
* Dynamisch GIS-System-Marker erstellen & Sichtbarkeit steuern.
|
|
* @param {object} map - Leaflet Map-Instanz
|
|
* @param {Array} GisSystemStatic - Liste der Systeme aus Webservice
|
|
* @param {object} mapLayersVisibility - Redux-Objekt mit Layer-Sichtbarkeiten
|
|
* @param {object} priorityConfig - Konfig für Prioritäten
|
|
* @returns {{ markerStates, layerRefs }} Alle Marker und Referenzen
|
|
*/
|
|
const useDynamicDeviceLayers = (map, GisSystemStatic, mapLayersVisibility, priorityConfig, oms) => {
|
|
const measurements = useSelector(selectGisStationsMeasurements);
|
|
const staticDistrictData = useSelector(selectGisStationsStaticDistrict);
|
|
const statusDistrict = useSelector(selectGisStationsStatusDistrict);
|
|
|
|
const [markerStates, setMarkerStates] = useState({});
|
|
const layerRefs = useRef({});
|
|
|
|
// Marker initialisieren (nicht sichtbar machen)
|
|
useEffect(() => {
|
|
if (!map || GisSystemStatic.length === 0) return;
|
|
|
|
GisSystemStatic.forEach(({ Name, IdSystem }) => {
|
|
const key = `system-${IdSystem}`;
|
|
// LayerGroup immer komplett neu erstellen, um doppelte Marker zu verhindern
|
|
if (layerRefs.current[key]) {
|
|
if (map.hasLayer(layerRefs.current[key])) {
|
|
map.removeLayer(layerRefs.current[key]);
|
|
}
|
|
layerRefs.current[key].clearLayers();
|
|
delete layerRefs.current[key];
|
|
}
|
|
layerRefs.current[key] = new L.LayerGroup();
|
|
layerRefs.current[key].addTo(map);
|
|
|
|
createAndSetDevices(
|
|
IdSystem,
|
|
newMarkers => {
|
|
// Füge neue Marker der LayerGroup hinzu (nur Geräte-Marker)
|
|
if (layerRefs.current[key]) {
|
|
layerRefs.current[key].clearLayers();
|
|
// Nur eindeutige Marker hinzufügen
|
|
const uniqueMarkers = Array.isArray(newMarkers) ? Array.from(new Set(newMarkers)) : [];
|
|
uniqueMarkers.forEach(marker => {
|
|
marker.addTo(layerRefs.current[key]);
|
|
});
|
|
}
|
|
setMarkerStates(prev => ({ ...prev, [key]: newMarkers }));
|
|
},
|
|
GisSystemStatic,
|
|
priorityConfig,
|
|
undefined,
|
|
oms
|
|
);
|
|
});
|
|
}, [map, GisSystemStatic, priorityConfig, staticDistrictData, measurements, statusDistrict]);
|
|
|
|
// Sichtbarkeit nach Redux-Status steuern
|
|
useEffect(() => {
|
|
if (!map) return;
|
|
const editMode = localStorage.getItem("editMode") === "true";
|
|
|
|
Object.entries(layerRefs.current).forEach(([key, layerGroup]) => {
|
|
const isVisible = mapLayersVisibility[key] ?? true;
|
|
if (editMode || isVisible === false) {
|
|
if (map.hasLayer(layerGroup)) {
|
|
map.removeLayer(layerGroup);
|
|
}
|
|
} else if (isVisible === true) {
|
|
if (!map.hasLayer(layerGroup)) {
|
|
layerGroup.addTo(map);
|
|
}
|
|
}
|
|
});
|
|
|
|
// Overlapping-Check bleibt wie gehabt
|
|
const allMarkers = Object.values(markerStates).filter(Array.isArray).flat();
|
|
checkOverlappingMarkers(map, allMarkers, plusRoundIcon);
|
|
}, [map, markerStates, mapLayersVisibility]);
|
|
|
|
return { markerStates, layerRefs };
|
|
};
|
|
|
|
export default useDynamicDeviceLayers;
|