From 85cc048c196fd4a2ffda7c83ee141b16bf0e1e7f Mon Sep 17 00:00:00 2001 From: Noah Manneschmidt Date: Mon, 11 Nov 2024 18:55:56 -0800 Subject: [PATCH] config should respond to stage changes --- ui/stage/hooks.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ui/stage/hooks.ts b/ui/stage/hooks.ts index 400779e..97a055e 100644 --- a/ui/stage/hooks.ts +++ b/ui/stage/hooks.ts @@ -1,5 +1,5 @@ import { useAtomValue } from "jotai"; -import { useState, useEffect } from "react"; +import { useState, useEffect, useRef } from "react"; import { type SMXStage, type SMXSensorTestData, SensorTestMode } from "../../sdk"; import { displayTestData$ } from "../state"; @@ -48,8 +48,17 @@ export function useTestData(stage: SMXStage | undefined) { } export function useConfig(stage: SMXStage | undefined) { + const stageRef = useRef(stage); const [configData, setConfig] = useState(stage?.config); + useEffect(() => { + if (stageRef.current !== stage) { + // detected the stage has changed, so keep the config in sync + setConfig(stage?.config); + stageRef.current = stage; + } + }, [stage]); + useEffect(() => { return stage?.configResponse$.onValue((config) => setConfig(config.config)); }, [stage]);