From 4761d07a7631326607804fec4c0bd71c3fbd9d02 Mon Sep 17 00:00:00 2001 From: robbond Date: Thu, 20 Aug 2026 10:00:18 +0100 Subject: [PATCH] test(ui): stabilize fresh start hydration --- components/scenario-form.jsx | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/components/scenario-form.jsx b/components/scenario-form.jsx index 528f47a..bd27b5e 100644 --- a/components/scenario-form.jsx +++ b/components/scenario-form.jsx @@ -237,16 +237,9 @@ export default function ScenarioForm() { const branchScoped = useBranchScopedFixture(); // Toggle to control when the experiment view is visible vs production - const [showExperimentView, setShowExperimentView] = useState( - typeof window !== "undefined" ? (() => { - const saved = sessionStorage?.getItem("ce-show-experiment"); - // Only default to true if user explicitly toggled ON (saved === "true") - // or has an active investigation context (session exists with situationGraph) - if (saved === "true") return true; - const session = getSession(); - return Boolean(session?.situationGraph); - })() : false, - ); + // Always start false for SSR-safe deterministic first render. + // sessionStorage reads are deferred to useEffect (after mount). + const [showExperimentView, setShowExperimentView] = useState(false); // Use fixture branches as the authoritative source when available const BRANCHES = useMemo(() => branchScoped.getBranches(), [branchScoped]); @@ -309,6 +302,20 @@ export default function ScenarioForm() { setStatus("success"); }, []); + /* Restore experiment view preference from storage (after hydration) ─ */ + useEffect(() => { + if (typeof window === "undefined") return; + const savedExp = sessionStorage?.getItem("ce-show-experiment"); + if (savedExp === "true") { + setShowExperimentView(true); + return; + } + const session = getSession(); + if (session?.situationGraph) { + setShowExperimentView(true); + } + }, []); + /* Restore facilitator dismiss preference (Experiment 05) ─── */ useEffect(() => { if (typeof window === "undefined") return;