Skip to content

Commit 757343d

Browse files
committed
ack PR comments
1 parent e054cef commit 757343d

File tree

4 files changed

+69
-38
lines changed

4 files changed

+69
-38
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { enqueueReplaceWorkflowState } from '@/lib/workflows/operations/socket-o
66
import { useOperationQueue } from '@/stores/operation-queue/store'
77
import {
88
type BatchAddBlocksOperation,
9+
type BatchAddEdgesOperation,
910
type BatchMoveBlocksOperation,
1011
type BatchRemoveBlocksOperation,
1112
type BatchRemoveEdgesOperation,
@@ -141,7 +142,6 @@ export function useUndoRedo() {
141142
data: { edgeId },
142143
}
143144

144-
// Inverse is batch-remove-edges with a single edge
145145
const inverse: BatchRemoveEdgesOperation = {
146146
id: crypto.randomUUID(),
147147
type: 'batch-remove-edges',
@@ -176,10 +176,9 @@ export function useUndoRedo() {
176176
},
177177
}
178178

179-
// Inverse is batch-add-edges (using the snapshots to restore)
180-
const inverse: BatchRemoveEdgesOperation = {
179+
const inverse: BatchAddEdgesOperation = {
181180
id: crypto.randomUUID(),
182-
type: 'batch-remove-edges',
181+
type: 'batch-add-edges',
183182
timestamp: Date.now(),
184183
workflowId: activeWorkflowId,
185184
userId,

apps/sim/socket/database/operations.ts

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -703,25 +703,22 @@ async function handleBlocksOperationTx(
703703
`Batch toggling enabled state for ${blockIds.length} blocks in workflow ${workflowId}`
704704
)
705705

706-
for (const blockId of blockIds) {
707-
const block = await tx
708-
.select({ enabled: workflowBlocks.enabled })
709-
.from(workflowBlocks)
710-
.where(and(eq(workflowBlocks.id, blockId), eq(workflowBlocks.workflowId, workflowId)))
711-
.limit(1)
706+
const blocks = await tx
707+
.select({ id: workflowBlocks.id, enabled: workflowBlocks.enabled })
708+
.from(workflowBlocks)
709+
.where(and(eq(workflowBlocks.workflowId, workflowId), inArray(workflowBlocks.id, blockIds)))
712710

713-
if (block.length > 0) {
714-
await tx
715-
.update(workflowBlocks)
716-
.set({
717-
enabled: !block[0].enabled,
718-
updatedAt: new Date(),
719-
})
720-
.where(and(eq(workflowBlocks.id, blockId), eq(workflowBlocks.workflowId, workflowId)))
721-
}
711+
for (const block of blocks) {
712+
await tx
713+
.update(workflowBlocks)
714+
.set({
715+
enabled: !block.enabled,
716+
updatedAt: new Date(),
717+
})
718+
.where(and(eq(workflowBlocks.id, block.id), eq(workflowBlocks.workflowId, workflowId)))
722719
}
723720

724-
logger.debug(`Batch toggled enabled state for ${blockIds.length} blocks`)
721+
logger.debug(`Batch toggled enabled state for ${blocks.length} blocks`)
725722
break
726723
}
727724

@@ -733,25 +730,22 @@ async function handleBlocksOperationTx(
733730

734731
logger.info(`Batch toggling handles for ${blockIds.length} blocks in workflow ${workflowId}`)
735732

736-
for (const blockId of blockIds) {
737-
const block = await tx
738-
.select({ horizontalHandles: workflowBlocks.horizontalHandles })
739-
.from(workflowBlocks)
740-
.where(and(eq(workflowBlocks.id, blockId), eq(workflowBlocks.workflowId, workflowId)))
741-
.limit(1)
733+
const blocks = await tx
734+
.select({ id: workflowBlocks.id, horizontalHandles: workflowBlocks.horizontalHandles })
735+
.from(workflowBlocks)
736+
.where(and(eq(workflowBlocks.workflowId, workflowId), inArray(workflowBlocks.id, blockIds)))
742737

743-
if (block.length > 0) {
744-
await tx
745-
.update(workflowBlocks)
746-
.set({
747-
horizontalHandles: !block[0].horizontalHandles,
748-
updatedAt: new Date(),
749-
})
750-
.where(and(eq(workflowBlocks.id, blockId), eq(workflowBlocks.workflowId, workflowId)))
751-
}
738+
for (const block of blocks) {
739+
await tx
740+
.update(workflowBlocks)
741+
.set({
742+
horizontalHandles: !block.horizontalHandles,
743+
updatedAt: new Date(),
744+
})
745+
.where(and(eq(workflowBlocks.id, block.id), eq(workflowBlocks.workflowId, workflowId)))
752746
}
753747

754-
logger.debug(`Batch toggled handles for ${blockIds.length} blocks`)
748+
logger.debug(`Batch toggled handles for ${blocks.length} blocks`)
755749
break
756750
}
757751

apps/sim/stores/undo-redo/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type OperationType =
55
| 'batch-add-blocks'
66
| 'batch-remove-blocks'
77
| 'add-edge'
8+
| 'batch-add-edges'
89
| 'batch-remove-edges'
910
| 'add-subflow'
1011
| 'remove-subflow'
@@ -50,6 +51,13 @@ export interface AddEdgeOperation extends BaseOperation {
5051
}
5152
}
5253

54+
export interface BatchAddEdgesOperation extends BaseOperation {
55+
type: 'batch-add-edges'
56+
data: {
57+
edgeSnapshots: Edge[]
58+
}
59+
}
60+
5361
export interface BatchRemoveEdgesOperation extends BaseOperation {
5462
type: 'batch-remove-edges'
5563
data: {
@@ -159,6 +167,7 @@ export type Operation =
159167
| BatchAddBlocksOperation
160168
| BatchRemoveBlocksOperation
161169
| AddEdgeOperation
170+
| BatchAddEdgesOperation
162171
| BatchRemoveEdgesOperation
163172
| AddSubflowOperation
164173
| RemoveSubflowOperation

apps/sim/stores/undo-redo/utils.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {
22
BatchAddBlocksOperation,
3+
BatchAddEdgesOperation,
34
BatchMoveBlocksOperation,
45
BatchRemoveBlocksOperation,
56
BatchRemoveEdgesOperation,
@@ -56,8 +57,8 @@ export function createInverseOperation(operation: Operation): Operation {
5657
},
5758
} as BatchRemoveEdgesOperation
5859

59-
case 'batch-remove-edges': {
60-
const op = operation as BatchRemoveEdgesOperation
60+
case 'batch-add-edges': {
61+
const op = operation as BatchAddEdgesOperation
6162
return {
6263
...operation,
6364
type: 'batch-remove-edges',
@@ -67,6 +68,17 @@ export function createInverseOperation(operation: Operation): Operation {
6768
} as BatchRemoveEdgesOperation
6869
}
6970

71+
case 'batch-remove-edges': {
72+
const op = operation as BatchRemoveEdgesOperation
73+
return {
74+
...operation,
75+
type: 'batch-add-edges',
76+
data: {
77+
edgeSnapshots: op.data.edgeSnapshots,
78+
},
79+
} as BatchAddEdgesOperation
80+
}
81+
7082
case 'add-subflow':
7183
return {
7284
...operation,
@@ -218,6 +230,23 @@ export function operationToCollaborativePayload(operation: Operation): {
218230
payload: { id: operation.data.edgeId },
219231
}
220232

233+
case 'batch-add-edges': {
234+
const op = operation as BatchAddEdgesOperation
235+
return {
236+
operation: 'batch-add-edges',
237+
target: 'edges',
238+
payload: {
239+
edges: op.data.edgeSnapshots.map((e) => ({
240+
id: e.id,
241+
source: e.source,
242+
target: e.target,
243+
sourceHandle: e.sourceHandle ?? null,
244+
targetHandle: e.targetHandle ?? null,
245+
})),
246+
},
247+
}
248+
}
249+
221250
case 'batch-remove-edges': {
222251
const op = operation as BatchRemoveEdgesOperation
223252
return {

0 commit comments

Comments
 (0)