200 lines
5.5 KiB
TypeScript
200 lines
5.5 KiB
TypeScript
"use client"; // /components/modules/kue705FO/charts/ChartSwitcher.tsx
|
|
|
|
import React, { useEffect } from "react";
|
|
import ReactModal from "react-modal";
|
|
import LoopChartActionBar from "./LoopMeasurementChart/LoopChartActionBar";
|
|
import LoopMeasurementChart from "./LoopMeasurementChart/LoopMeasurementChart";
|
|
import TDRChart from "./TDRChart/TDRChart";
|
|
import { useSelector, useDispatch } from "react-redux";
|
|
import { AppDispatch } from "@/redux/store";
|
|
import { RootState } from "@/redux/store";
|
|
import {
|
|
setChartOpen,
|
|
setFullScreen,
|
|
} from "@/redux/slices/kabelueberwachungChartSlice";
|
|
|
|
import { resetBrushRange } from "@/redux/slices/brushSlice";
|
|
import { useLoopChartLoader } from "./LoopMeasurementChart/LoopChartActionBar";
|
|
|
|
import {
|
|
setVonDatum,
|
|
setBisDatum,
|
|
setSelectedMode,
|
|
setSelectedSlotType,
|
|
} from "@/redux/slices/kabelueberwachungChartSlice";
|
|
|
|
interface ChartSwitcherProps {
|
|
isOpen: boolean;
|
|
onClose: () => void;
|
|
slotIndex: number;
|
|
}
|
|
|
|
const ChartSwitcher: React.FC<ChartSwitcherProps> = ({ isOpen, onClose }) => {
|
|
const dispatch = useDispatch<AppDispatch>();
|
|
const chartTitle = useSelector(
|
|
(state: RootState) => state.loopChartType.chartTitle
|
|
);
|
|
|
|
// **Redux-States für aktive Messkurve (TDR oder Schleife)**
|
|
const activeMode = useSelector(
|
|
(state: RootState) => state.kueChartModeSlice.activeMode
|
|
);
|
|
const isFullScreen = useSelector(
|
|
(state: RootState) => state.kabelueberwachungChartSlice.isFullScreen
|
|
);
|
|
|
|
// **Modal schließen + Redux-Status zurücksetzen**
|
|
const handleClose = () => {
|
|
const today = new Date();
|
|
const thirtyDaysAgo = new Date();
|
|
thirtyDaysAgo.setDate(today.getDate() - 30);
|
|
|
|
const toISO = (date: Date) => date.toLocaleDateString("sv-SE");
|
|
|
|
// Reset Datum
|
|
dispatch(setVonDatum(toISO(thirtyDaysAgo)));
|
|
dispatch(setBisDatum(toISO(today)));
|
|
|
|
// Reset Dropdowns
|
|
dispatch(setSelectedMode("DIA1"));
|
|
dispatch(setSelectedSlotType("isolationswiderstand"));
|
|
|
|
// Sonstiges Reset
|
|
dispatch(setChartOpen(false));
|
|
dispatch(setFullScreen(false));
|
|
dispatch(resetBrushRange());
|
|
|
|
onClose();
|
|
};
|
|
|
|
// **Vollbildmodus umschalten**
|
|
const toggleFullScreen = () => {
|
|
dispatch(setFullScreen(!isFullScreen));
|
|
};
|
|
|
|
// **Slot und Messkurve setzen**
|
|
// const setChartType = (chartType: "TDR" | "Schleife") => {
|
|
// dispatch(setSelectedSlot(slotIndex));
|
|
// dispatch(setSelectedChartType(chartType));
|
|
// };
|
|
|
|
// useLoopChartLoader hook
|
|
const loadLoopChartData = useLoopChartLoader();
|
|
|
|
// Slot number from Redux
|
|
const slotNumber = useSelector(
|
|
(state: RootState) => state.kabelueberwachungChartSlice.slotNumber
|
|
);
|
|
|
|
// immer beim Öffnen das Modal die letzten 30 Tage anzeigen
|
|
useEffect(() => {
|
|
if (isOpen && activeMode === "Schleife" && slotNumber !== null) {
|
|
const today = new Date();
|
|
const thirtyDaysAgo = new Date();
|
|
thirtyDaysAgo.setDate(today.getDate() - 30);
|
|
|
|
const toISO = (date: Date) => date.toLocaleDateString("sv-SE"); // YYYY-MM-DD
|
|
|
|
dispatch(setVonDatum(toISO(thirtyDaysAgo)));
|
|
dispatch(setBisDatum(toISO(today)));
|
|
|
|
// Warten, bis Redux gesetzt ist → dann Daten laden
|
|
setTimeout(() => {
|
|
loadLoopChartData.loadLoopChartData();
|
|
}, 10); // kleiner Delay, damit Redux-State sicher aktualisiert ist
|
|
}
|
|
}, [isOpen, activeMode, slotNumber, dispatch]);
|
|
|
|
return (
|
|
<ReactModal
|
|
isOpen={isOpen}
|
|
onRequestClose={handleClose}
|
|
ariaHideApp={false}
|
|
style={{
|
|
overlay: { backgroundColor: "rgba(0, 0, 0, 0.5)" },
|
|
content: {
|
|
top: "50%",
|
|
left: "50%",
|
|
bottom: "auto",
|
|
marginRight: "-50%",
|
|
transform: "translate(-50%, -50%)",
|
|
width: isFullScreen ? "90vw" : "70rem",
|
|
height: isFullScreen ? "90vh" : "35rem",
|
|
padding: "1rem",
|
|
transition: "all 0.3s ease-in-out",
|
|
display: "flex",
|
|
flexDirection: "column",
|
|
},
|
|
}}
|
|
>
|
|
{/* Action-Buttons */}
|
|
<div
|
|
style={{
|
|
position: "absolute",
|
|
top: "0.625rem",
|
|
right: "0.625rem",
|
|
display: "flex",
|
|
gap: "0.75rem",
|
|
}}
|
|
>
|
|
{/* Fullscreen-Button */}
|
|
<button
|
|
onClick={toggleFullScreen}
|
|
style={{
|
|
background: "transparent",
|
|
border: "none",
|
|
fontSize: "1.5rem",
|
|
cursor: "pointer",
|
|
}}
|
|
>
|
|
<i
|
|
className={
|
|
isFullScreen ? "bi bi-fullscreen-exit" : "bi bi-arrows-fullscreen"
|
|
}
|
|
></i>
|
|
</button>
|
|
|
|
{/* Schließen-Button */}
|
|
<button
|
|
onClick={handleClose}
|
|
style={{
|
|
background: "transparent",
|
|
border: "none",
|
|
fontSize: "1.5rem",
|
|
cursor: "pointer",
|
|
}}
|
|
>
|
|
<i className="bi bi-x-circle-fill"></i>
|
|
</button>
|
|
</div>
|
|
|
|
{/* Chart-Container */}
|
|
<div
|
|
style={{
|
|
flex: 1,
|
|
display: "flex",
|
|
flexDirection: "column",
|
|
height: "100%",
|
|
}}
|
|
>
|
|
{activeMode === "Schleife" ? (
|
|
<>
|
|
<h3 className="text-lg font-semibold">{chartTitle}</h3>
|
|
<LoopChartActionBar />
|
|
<div style={{ flex: 1, height: "90%" }}>
|
|
<LoopMeasurementChart />
|
|
</div>
|
|
</>
|
|
) : (
|
|
<>
|
|
<h3 className="text-lg font-semibold">TDR-Messung</h3>
|
|
<TDRChart isFullScreen={isFullScreen} />
|
|
</>
|
|
)}
|
|
</div>
|
|
</ReactModal>
|
|
);
|
|
};
|
|
|
|
export default ChartSwitcher;
|