From 239be5017f0d542c2a290aca98f14f1b865bcd9d Mon Sep 17 00:00:00 2001 From: chihlasm Date: Wed, 18 Feb 2026 03:03:13 -0500 Subject: [PATCH] fix: re-sync draft from store when canvas card is opened MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a decision node is saved with new options, stub next_node_id values are written back to the store. But the local draft was initialized once at mount and never refreshed, so reopening the card gave a stale draft with empty next_node_ids — causing duplicate stubs on every subsequent save. Fix: reset draft from the live node whenever isExpanded transitions to true. Co-Authored-By: Claude Sonnet 4.6 --- .../src/components/tree-editor/TreeCanvasNode.tsx | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/tree-editor/TreeCanvasNode.tsx b/frontend/src/components/tree-editor/TreeCanvasNode.tsx index 66f83233..3a3cb005 100644 --- a/frontend/src/components/tree-editor/TreeCanvasNode.tsx +++ b/frontend/src/components/tree-editor/TreeCanvasNode.tsx @@ -1,4 +1,4 @@ -import { useState, useCallback } from 'react' +import { useState, useCallback, useEffect } from 'react' import { HelpCircle, Zap, @@ -97,13 +97,22 @@ export function TreeCanvasNode({ cloneNodeWithoutChildren(node) ) - // Reset draft if node changes while editing (e.g. store update from undo) + // Reset draft if node ID changes (e.g. navigating between nodes) const [lastNodeId, setLastNodeId] = useState(node.id) if (node.id !== lastNodeId) { setDraft(cloneNodeWithoutChildren(node)) setLastNodeId(node.id) } + // Re-sync draft from store whenever the card is opened, so stale next_node_id + // values (written back after stub creation) don't cause duplicate stubs on re-save + useEffect(() => { + if (isExpanded) { + setDraft(cloneNodeWithoutChildren(node)) + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [isExpanded]) + const handleDraftUpdate = useCallback((updates: Partial) => { setDraft((prev) => ({ ...prev, ...updates })) }, [])