Skip to content

Commit 037dad6

Browse files
authored
fix(undo-redo): preserve subblock values during undo/redo cycles (#2884)
* fix(undo-redo): preserve subblock values during undo/redo cycles * added tests
1 parent 408597e commit 037dad6

File tree

3 files changed

+451
-26
lines changed

3 files changed

+451
-26
lines changed

apps/sim/hooks/use-undo-redo.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ import {
2121
type BatchToggleEnabledOperation,
2222
type BatchToggleHandlesOperation,
2323
type BatchUpdateParentOperation,
24+
captureLatestEdges,
25+
captureLatestSubBlockValues,
2426
createOperationEntry,
2527
runWithUndoRedoRecordingSuspended,
2628
type UpdateParentOperation,
2729
useUndoRedoStore,
2830
} from '@/stores/undo-redo'
2931
import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
3032
import { useSubBlockStore } from '@/stores/workflows/subblock/store'
31-
import { mergeSubblockState } from '@/stores/workflows/utils'
3233
import { useWorkflowStore } from '@/stores/workflows/workflow/store'
3334
import type { BlockState } from '@/stores/workflows/workflow/types'
3435

@@ -445,34 +446,19 @@ export function useUndoRedo() {
445446
break
446447
}
447448

448-
const latestEdges = useWorkflowStore
449-
.getState()
450-
.edges.filter(
451-
(e) => existingBlockIds.includes(e.source) || existingBlockIds.includes(e.target)
452-
)
449+
const latestEdges = captureLatestEdges(
450+
useWorkflowStore.getState().edges,
451+
existingBlockIds
452+
)
453453
batchRemoveOp.data.edgeSnapshots = latestEdges
454454

455-
const latestSubBlockValues: Record<string, Record<string, unknown>> = {}
456-
existingBlockIds.forEach((blockId) => {
457-
const merged = mergeSubblockState(
458-
useWorkflowStore.getState().blocks,
459-
activeWorkflowId,
460-
blockId
461-
)
462-
const block = merged[blockId]
463-
if (block?.subBlocks) {
464-
const values: Record<string, unknown> = {}
465-
Object.entries(block.subBlocks).forEach(([subBlockId, subBlock]) => {
466-
if (subBlock.value !== null && subBlock.value !== undefined) {
467-
values[subBlockId] = subBlock.value
468-
}
469-
})
470-
if (Object.keys(values).length > 0) {
471-
latestSubBlockValues[blockId] = values
472-
}
473-
}
474-
})
455+
const latestSubBlockValues = captureLatestSubBlockValues(
456+
useWorkflowStore.getState().blocks,
457+
activeWorkflowId,
458+
existingBlockIds
459+
)
475460
batchRemoveOp.data.subBlockValues = latestSubBlockValues
461+
;(entry.operation as BatchAddBlocksOperation).data.subBlockValues = latestSubBlockValues
476462

477463
addToQueue({
478464
id: opId,
@@ -1153,6 +1139,20 @@ export function useUndoRedo() {
11531139
break
11541140
}
11551141

1142+
const latestEdges = captureLatestEdges(
1143+
useWorkflowStore.getState().edges,
1144+
existingBlockIds
1145+
)
1146+
batchOp.data.edgeSnapshots = latestEdges
1147+
1148+
const latestSubBlockValues = captureLatestSubBlockValues(
1149+
useWorkflowStore.getState().blocks,
1150+
activeWorkflowId,
1151+
existingBlockIds
1152+
)
1153+
batchOp.data.subBlockValues = latestSubBlockValues
1154+
;(entry.inverse as BatchAddBlocksOperation).data.subBlockValues = latestSubBlockValues
1155+
11561156
addToQueue({
11571157
id: opId,
11581158
operation: {

0 commit comments

Comments
 (0)