- Funktion `generateLineTooltipContent` aus `useLineData.js` extrahiert - Neue Datei: `components/gisPolylines/tooltip/generateLineTooltipContent.js` - Trennung von Logik (Hook) und Darstellung (Tooltip-HTML) - Verbesserte Lesbarkeit und Wiederverwendbarkeit der Tooltip-Generierung
85 lines
2.7 KiB
JavaScript
85 lines
2.7 KiB
JavaScript
// /components/gisPolylines/tooltip/useLineData.js
|
|
import { useEffect, useState } from "react";
|
|
import { useSelector, useDispatch } from "react-redux";
|
|
import { selectGisLinesStatusFromWebservice } from "@/redux/slices/webservice/gisLinesStatusSlice";
|
|
import { fetchGisLinesThunk } from "@/redux/thunks/database/polylines/fetchGisLinesThunk";
|
|
import { fetchGisLinesStatusThunk } from "@/../redux/thunks/webservice/fetchGisLinesStatusThunk";
|
|
import { generateLineTooltipContent } from "./generateLineTooltipContent";
|
|
|
|
const useLineData = () => {
|
|
const dispatch = useDispatch();
|
|
|
|
const { data: statisData } = useSelector(selectGisLinesStatusFromWebservice);
|
|
const linesData = useSelector(state => state.gisLinesFromDatabase.data);
|
|
const [lineColors, setLineColors] = useState({});
|
|
const [tooltipContents, setTooltipContents] = useState({});
|
|
|
|
useEffect(() => {
|
|
dispatch(fetchGisLinesThunk());
|
|
dispatch(fetchGisLinesStatusThunk());
|
|
}, [dispatch]);
|
|
|
|
useEffect(() => {
|
|
if (!statisData || !Array.isArray(statisData)) return;
|
|
|
|
const colorsByModule = {};
|
|
const newTooltipContents = {};
|
|
const valueMap = {};
|
|
|
|
const sortedStatis = [...statisData].sort((a, b) => a.Level - b.Level);
|
|
|
|
sortedStatis.forEach(statis => {
|
|
const key = `${statis.IdLD}-${statis.Modul}`;
|
|
if (!valueMap[key]) {
|
|
valueMap[key] = {
|
|
messages: [],
|
|
messwert: undefined,
|
|
schleifenwert: undefined,
|
|
};
|
|
}
|
|
|
|
if (
|
|
statis.DpName.endsWith("_Messwert") &&
|
|
statis.Value !== "True" &&
|
|
!valueMap[key].messwert
|
|
) {
|
|
valueMap[key].messwert = statis.Value;
|
|
}
|
|
|
|
if (statis.DpName.endsWith("_Schleifenwert") && !valueMap[key].schleifenwert) {
|
|
valueMap[key].schleifenwert = statis.Value;
|
|
}
|
|
|
|
if (statis.Message && statis.Message !== "?") {
|
|
valueMap[key].messages.push({
|
|
message: statis.Message,
|
|
prioColor:
|
|
statis.PrioColor && statis.PrioColor !== "#ffffff" ? statis.PrioColor : "green",
|
|
});
|
|
}
|
|
});
|
|
|
|
sortedStatis.forEach(statis => {
|
|
const key = `${statis.IdLD}-${statis.Modul}`;
|
|
const matchingLine = linesData.find(
|
|
item => item.idLD === statis.IdLD && item.idModul === statis.Modul
|
|
);
|
|
|
|
if (matchingLine) {
|
|
const values = valueMap[key];
|
|
|
|
colorsByModule[key] = values.messages.length > 0 ? values.messages[0].prioColor : "green";
|
|
|
|
newTooltipContents[key] = generateLineTooltipContent(statis, matchingLine, values);
|
|
}
|
|
});
|
|
|
|
setLineColors(colorsByModule);
|
|
setTooltipContents(newTooltipContents);
|
|
}, [statisData, linesData]);
|
|
|
|
return { lineColors, tooltipContents };
|
|
};
|
|
|
|
export default useLineData;
|