"use client"; import React, { useEffect, useState } from "react"; import { useAppSelector } from "@/redux/store"; export default function SlotActivityOverlay({ slotIndex, }: { slotIndex: number; }) { const ksx = useAppSelector((s) => s.deviceEvents.ksx); const ksy = useAppSelector((s) => s.deviceEvents.ksy); const ksz = useAppSelector((s) => s.deviceEvents.ksz); const loopStartedAt = useAppSelector((s) => s.deviceEvents.loopStartedAt); const tdrStartedAt = useAppSelector((s) => s.deviceEvents.tdrStartedAt); const comparisonStartedAt = useAppSelector( (s) => s.deviceEvents.comparisonStartedAt ); const loopStartedAtBySlot = useAppSelector( (s) => s.deviceEvents.loopStartedAtBySlot ); const tdrStartedAtBySlot = useAppSelector( (s) => s.deviceEvents.tdrStartedAtBySlot ); const comparisonStartedAtBySlot = useAppSelector( (s) => s.deviceEvents.comparisonStartedAtBySlot ); const loopActive = Array.isArray(ksx) && ksx[slotIndex] === 1; const tdrActive = Array.isArray(ksy) && ksy[slotIndex] === 1; const comparisonActive = Array.isArray(ksz) && ksz[slotIndex] === 1; // Persist whenever arrays change useEffect(() => { try { localStorage.setItem( "deviceEventsTimingsV1", JSON.stringify({ loop: loopStartedAtBySlot, tdr: tdrStartedAtBySlot, compare: comparisonStartedAtBySlot, }) ); } catch (e) { // eslint-disable-next-line no-console console.warn("Failed to persist timings", e); } }, [loopStartedAtBySlot, tdrStartedAtBySlot, comparisonStartedAtBySlot]); // Progress ticker const [now, setNow] = useState(Date.now()); useEffect(() => { const any = loopActive || tdrActive || comparisonActive; if (!any) return; const id = setInterval(() => setNow(Date.now()), 1000); return () => clearInterval(id); }, [loopActive, tdrActive, comparisonActive]); const clamp = (v: number, min = 0, max = 1) => Math.max(min, Math.min(max, v)); const compute = (startedAt: number | null, durationMs: number) => { if (!startedAt) return { pct: 0 }; const elapsed = now - startedAt; const pct = clamp(elapsed / durationMs) * 100; return { pct }; }; // Durations const LOOP_MS = 2 * 60 * 1000; // ~2 min const TDR_MS = 110 * 1000; // ~2 min laut die Eigaben const ALIGN_MS = 10 * 60 * 1000; // ~10 min if (!loopActive && !tdrActive && !comparisonActive) return null; return (
Bitte warten…
{loopActive && (
Schleife
{(() => { const started = loopStartedAtBySlot[slotIndex] ?? loopStartedAt; const { pct } = compute(started, LOOP_MS); return (
{Math.round(pct)}%
); })()}
)} {tdrActive && (
TDR
{(() => { const started = tdrStartedAtBySlot[slotIndex] ?? tdrStartedAt; const { pct } = compute(started, TDR_MS); return (
{Math.round(pct)}%
); })()}
)} {comparisonActive && (
Abgleich
{(() => { const started = comparisonStartedAtBySlot[slotIndex] ?? comparisonStartedAt; const { pct } = compute(started, ALIGN_MS); return (
{Math.round(pct)}%
); })()}
)}
); }