From 28c4a801a6cc9dd621aab13297c85b00205830d0 Mon Sep 17 00:00:00 2001 From: "93094049+kraemr@users.noreply.github.com" <93094049+kraemr@users.noreply.github.com> Date: Mon, 1 Sep 2025 13:52:40 +0200 Subject: [PATCH 1/3] added global timeframe store --- electron/src/components/graph/useGraphSync.ts | 16 +++++------ electron/src/stores/graphSettingsStore.ts | 27 +++++++++++++++++++ 2 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 electron/src/stores/graphSettingsStore.ts diff --git a/electron/src/components/graph/useGraphSync.ts b/electron/src/components/graph/useGraphSync.ts index c4e396ef..81be6d71 100644 --- a/electron/src/components/graph/useGraphSync.ts +++ b/electron/src/components/graph/useGraphSync.ts @@ -1,14 +1,13 @@ import { useState, useCallback, useRef } from "react"; import { PropGraphSync } from "./types"; import { GraphExportData, exportGraphsToExcel } from "./excelExport"; +import { useGraphSettingsStore } from "@/stores/graphSettingsStore"; + +export function useGraphSync(exportGroupId?: string) { + let store = useGraphSettingsStore(); + // not reactive but works :) + let timeWindow = store.getTimeframe(); -export function useGraphSync( - defaultTimeWindow: number | "all" = 30 * 60 * 1000, - exportGroupId?: string, -) { - const [timeWindow, setTimeWindow] = useState( - defaultTimeWindow, - ); const [viewMode, setViewMode] = useState<"default" | "all" | "manual">( "default", ); @@ -86,7 +85,8 @@ export function useGraphSync( } // Batch all state updates together - if (updates.timeWindow !== undefined) setTimeWindow(updates.timeWindow); + if (updates.timeWindow !== undefined) + store.setTimeframe(updates.timeWindow); if (updates.viewMode !== undefined) setViewMode(updates.viewMode); if (updates.isLiveMode !== undefined) setIsLiveMode(updates.isLiveMode); if (updates.xRange !== undefined) setXRange(updates.xRange); diff --git a/electron/src/stores/graphSettingsStore.ts b/electron/src/stores/graphSettingsStore.ts new file mode 100644 index 00000000..b894f45b --- /dev/null +++ b/electron/src/stores/graphSettingsStore.ts @@ -0,0 +1,27 @@ +import { create } from "zustand"; +import { produce } from "immer"; + +export type MachineName = string; // e.g., "mock1", "winder2", "extruder2" + +export type TimeFrame = number | "all"; + +interface GraphSettingsState { + timeframe: TimeFrame; + setTimeframe: (graphTimeFrame: TimeFrame) => void; + getTimeframe: () => TimeFrame; +} + +export const useGraphSettingsStore = create((set, get) => ({ + timeframe: 30 * 1000 * 60, + setTimeframe: (graphTimeFrame) => + set( + produce((state: GraphSettingsState) => { + state.timeframe = graphTimeFrame; + }), + ), + + getTimeframe: () => { + const state = get(); + return state.timeframe; + }, +})); From 91660fd1ecd4ffd129e52e4c9124b30be8a94040 Mon Sep 17 00:00:00 2001 From: "93094049+kraemr@users.noreply.github.com" <93094049+kraemr@users.noreply.github.com> Date: Mon, 1 Sep 2025 13:58:07 +0200 Subject: [PATCH 2/3] fixed compile errors --- electron/src/machines/extruder/extruder2/Extruder2Graph.tsx | 2 +- electron/src/machines/laser/laser1/Laser1Graph.tsx | 2 +- electron/src/machines/mock/mock1/Mock1Graph.tsx | 2 +- electron/src/machines/winder/winder2/Winder2Graphs.tsx | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/electron/src/machines/extruder/extruder2/Extruder2Graph.tsx b/electron/src/machines/extruder/extruder2/Extruder2Graph.tsx index 5cd15dd1..b8bc86e2 100644 --- a/electron/src/machines/extruder/extruder2/Extruder2Graph.tsx +++ b/electron/src/machines/extruder/extruder2/Extruder2Graph.tsx @@ -26,7 +26,7 @@ export function Extruder2GraphsPage() { combinedPower, } = useExtruder2(); - const syncHook = useGraphSync(30 * 60 * 1000, "extruder-graphs"); + const syncHook = useGraphSync("extruder-graphs"); // Base config const baseConfig: GraphConfig = { diff --git a/electron/src/machines/laser/laser1/Laser1Graph.tsx b/electron/src/machines/laser/laser1/Laser1Graph.tsx index ff84ee7c..8cf11799 100644 --- a/electron/src/machines/laser/laser1/Laser1Graph.tsx +++ b/electron/src/machines/laser/laser1/Laser1Graph.tsx @@ -12,7 +12,7 @@ import { useLaser1 } from "./useLaser1"; export function Laser1GraphsPage() { const { diameter, state } = useLaser1(); - const syncHook = useGraphSync(30 * 60 * 1000, "diameter-group"); + const syncHook = useGraphSync("diameter-group"); const targetDiameter = state?.laser_state?.target_diameter ?? 0; const lowerTolerance = state?.laser_state?.lower_tolerance ?? 0; const higherTolerance = state?.laser_state?.higher_tolerance ?? 0; diff --git a/electron/src/machines/mock/mock1/Mock1Graph.tsx b/electron/src/machines/mock/mock1/Mock1Graph.tsx index 9edb452e..5f0805d9 100644 --- a/electron/src/machines/mock/mock1/Mock1Graph.tsx +++ b/electron/src/machines/mock/mock1/Mock1Graph.tsx @@ -12,7 +12,7 @@ import { TimeSeriesValue, type Series, TimeSeries } from "@/lib/timeseries"; export function Mock1GraphPage() { const { sineWaveSum } = useMock1(); - const syncHook = useGraphSync(30 * 60 * 1000, "mock-graphs"); + const syncHook = useGraphSync("mock-graphs"); const config: GraphConfig = { title: "Sine Wave", diff --git a/electron/src/machines/winder/winder2/Winder2Graphs.tsx b/electron/src/machines/winder/winder2/Winder2Graphs.tsx index 9cdbc747..458a8bcb 100644 --- a/electron/src/machines/winder/winder2/Winder2Graphs.tsx +++ b/electron/src/machines/winder/winder2/Winder2Graphs.tsx @@ -23,7 +23,7 @@ export function Winder2GraphsPage() { spoolProgress, } = useWinder2(); - const syncHook = useGraphSync(30 * 60 * 1000, "winder2-group"); + const syncHook = useGraphSync("winder2-group"); return ( From 84f1fa123fca007ebca83c505faa3da43c52399b Mon Sep 17 00:00:00 2001 From: "93094049+kraemr@users.noreply.github.com" <93094049+kraemr@users.noreply.github.com> Date: Mon, 1 Sep 2025 14:00:14 +0200 Subject: [PATCH 3/3] fixed linter error --- electron/src/components/graph/useGraphSync.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/electron/src/components/graph/useGraphSync.ts b/electron/src/components/graph/useGraphSync.ts index 81be6d71..f63223e5 100644 --- a/electron/src/components/graph/useGraphSync.ts +++ b/electron/src/components/graph/useGraphSync.ts @@ -4,9 +4,9 @@ import { GraphExportData, exportGraphsToExcel } from "./excelExport"; import { useGraphSettingsStore } from "@/stores/graphSettingsStore"; export function useGraphSync(exportGroupId?: string) { - let store = useGraphSettingsStore(); + const store = useGraphSettingsStore(); // not reactive but works :) - let timeWindow = store.getTimeframe(); + const timeWindow = store.getTimeframe(); const [viewMode, setViewMode] = useState<"default" | "all" | "manual">( "default",