Skip to content

Commit c937334

Browse files
committed
added tests
1 parent 84cc5bd commit c937334

File tree

3 files changed

+445
-51
lines changed

3 files changed

+445
-51
lines changed

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

Lines changed: 20 additions & 51 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,33 +446,17 @@ 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
476461
;(entry.operation as BatchAddBlocksOperation).data.subBlockValues = latestSubBlockValues
477462

@@ -1154,33 +1139,17 @@ export function useUndoRedo() {
11541139
break
11551140
}
11561141

1157-
const latestEdges = useWorkflowStore
1158-
.getState()
1159-
.edges.filter(
1160-
(e) => existingBlockIds.includes(e.source) || existingBlockIds.includes(e.target)
1161-
)
1142+
const latestEdges = captureLatestEdges(
1143+
useWorkflowStore.getState().edges,
1144+
existingBlockIds
1145+
)
11621146
batchOp.data.edgeSnapshots = latestEdges
11631147

1164-
const latestSubBlockValues: Record<string, Record<string, unknown>> = {}
1165-
existingBlockIds.forEach((blockId) => {
1166-
const merged = mergeSubblockState(
1167-
useWorkflowStore.getState().blocks,
1168-
activeWorkflowId,
1169-
blockId
1170-
)
1171-
const block = merged[blockId]
1172-
if (block?.subBlocks) {
1173-
const values: Record<string, unknown> = {}
1174-
Object.entries(block.subBlocks).forEach(([subBlockId, subBlock]) => {
1175-
if (subBlock.value !== null && subBlock.value !== undefined) {
1176-
values[subBlockId] = subBlock.value
1177-
}
1178-
})
1179-
if (Object.keys(values).length > 0) {
1180-
latestSubBlockValues[blockId] = values
1181-
}
1182-
}
1183-
})
1148+
const latestSubBlockValues = captureLatestSubBlockValues(
1149+
useWorkflowStore.getState().blocks,
1150+
activeWorkflowId,
1151+
existingBlockIds
1152+
)
11841153
batchOp.data.subBlockValues = latestSubBlockValues
11851154
;(entry.inverse as BatchAddBlocksOperation).data.subBlockValues = latestSubBlockValues
11861155

0 commit comments

Comments
 (0)