fix: re-sync draft from store when canvas card is opened

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 <noreply@anthropic.com>
This commit is contained in:
chihlasm
2026-02-18 03:03:13 -05:00
parent d36f0406db
commit 239be5017f

View File

@@ -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<TreeStructure>) => {
setDraft((prev) => ({ ...prev, ...updates }))
}, [])