Skip to content

Commit 80e98dd

Browse files
committed
use consolidated consts for sockets operations
1 parent 687733d commit 80e98dd

File tree

9 files changed

+524
-326
lines changed

9 files changed

+524
-326
lines changed

apps/sim/hooks/use-collaborative-workflow.ts

Lines changed: 171 additions & 116 deletions
Large diffs are not rendered by default.

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

Lines changed: 92 additions & 85 deletions
Large diffs are not rendered by default.

apps/sim/socket/constants.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
export const BLOCK_OPERATIONS = {
2+
UPDATE_POSITION: 'update-position',
3+
UPDATE_NAME: 'update-name',
4+
TOGGLE_ENABLED: 'toggle-enabled',
5+
UPDATE_PARENT: 'update-parent',
6+
UPDATE_WIDE: 'update-wide',
7+
UPDATE_ADVANCED_MODE: 'update-advanced-mode',
8+
UPDATE_TRIGGER_MODE: 'update-trigger-mode',
9+
TOGGLE_HANDLES: 'toggle-handles',
10+
} as const
11+
12+
export type BlockOperation = (typeof BLOCK_OPERATIONS)[keyof typeof BLOCK_OPERATIONS]
13+
14+
export const BLOCKS_OPERATIONS = {
15+
BATCH_UPDATE_POSITIONS: 'batch-update-positions',
16+
BATCH_ADD_BLOCKS: 'batch-add-blocks',
17+
BATCH_REMOVE_BLOCKS: 'batch-remove-blocks',
18+
BATCH_TOGGLE_ENABLED: 'batch-toggle-enabled',
19+
BATCH_TOGGLE_HANDLES: 'batch-toggle-handles',
20+
BATCH_UPDATE_PARENT: 'batch-update-parent',
21+
} as const
22+
23+
export type BlocksOperation = (typeof BLOCKS_OPERATIONS)[keyof typeof BLOCKS_OPERATIONS]
24+
25+
export const EDGE_OPERATIONS = {
26+
ADD: 'add',
27+
REMOVE: 'remove',
28+
} as const
29+
30+
export type EdgeOperation = (typeof EDGE_OPERATIONS)[keyof typeof EDGE_OPERATIONS]
31+
32+
export const EDGES_OPERATIONS = {
33+
BATCH_ADD_EDGES: 'batch-add-edges',
34+
BATCH_REMOVE_EDGES: 'batch-remove-edges',
35+
} as const
36+
37+
export type EdgesOperation = (typeof EDGES_OPERATIONS)[keyof typeof EDGES_OPERATIONS]
38+
39+
export const SUBFLOW_OPERATIONS = {
40+
ADD: 'add',
41+
REMOVE: 'remove',
42+
UPDATE: 'update',
43+
} as const
44+
45+
export type SubflowOperation = (typeof SUBFLOW_OPERATIONS)[keyof typeof SUBFLOW_OPERATIONS]
46+
47+
export const VARIABLE_OPERATIONS = {
48+
ADD: 'add',
49+
REMOVE: 'remove',
50+
UPDATE: 'variable-update',
51+
} as const
52+
53+
export type VariableOperation = (typeof VARIABLE_OPERATIONS)[keyof typeof VARIABLE_OPERATIONS]
54+
55+
export const WORKFLOW_OPERATIONS = {
56+
REPLACE_STATE: 'replace-state',
57+
} as const
58+
59+
export type WorkflowOperation = (typeof WORKFLOW_OPERATIONS)[keyof typeof WORKFLOW_OPERATIONS]
60+
61+
export const SUBBLOCK_OPERATIONS = {
62+
UPDATE: 'subblock-update',
63+
} as const
64+
65+
export type SubblockOperation = (typeof SUBBLOCK_OPERATIONS)[keyof typeof SUBBLOCK_OPERATIONS]
66+
67+
export const OPERATION_TARGETS = {
68+
BLOCK: 'block',
69+
BLOCKS: 'blocks',
70+
EDGE: 'edge',
71+
EDGES: 'edges',
72+
SUBBLOCK: 'subblock',
73+
SUBFLOW: 'subflow',
74+
VARIABLE: 'variable',
75+
WORKFLOW: 'workflow',
76+
} as const
77+
78+
export type OperationTarget = (typeof OPERATION_TARGETS)[keyof typeof OPERATION_TARGETS]
79+
80+
/** Undo/Redo operation types (includes some socket operations + undo-specific ones) */
81+
export const UNDO_REDO_OPERATIONS = {
82+
BATCH_ADD_BLOCKS: 'batch-add-blocks',
83+
BATCH_REMOVE_BLOCKS: 'batch-remove-blocks',
84+
BATCH_ADD_EDGES: 'batch-add-edges',
85+
BATCH_REMOVE_EDGES: 'batch-remove-edges',
86+
BATCH_MOVE_BLOCKS: 'batch-move-blocks',
87+
UPDATE_PARENT: 'update-parent',
88+
BATCH_UPDATE_PARENT: 'batch-update-parent',
89+
BATCH_TOGGLE_ENABLED: 'batch-toggle-enabled',
90+
BATCH_TOGGLE_HANDLES: 'batch-toggle-handles',
91+
APPLY_DIFF: 'apply-diff',
92+
ACCEPT_DIFF: 'accept-diff',
93+
REJECT_DIFF: 'reject-diff',
94+
} as const
95+
96+
export type UndoRedoOperation = (typeof UNDO_REDO_OPERATIONS)[keyof typeof UNDO_REDO_OPERATIONS]

apps/sim/socket/database/operations.ts

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ import postgres from 'postgres'
77
import { env } from '@/lib/core/config/env'
88
import { cleanupExternalWebhook } from '@/lib/webhooks/provider-subscriptions'
99
import { loadWorkflowFromNormalizedTables } from '@/lib/workflows/persistence/utils'
10+
import {
11+
BLOCK_OPERATIONS,
12+
BLOCKS_OPERATIONS,
13+
EDGE_OPERATIONS,
14+
EDGES_OPERATIONS,
15+
OPERATION_TARGETS,
16+
SUBFLOW_OPERATIONS,
17+
VARIABLE_OPERATIONS,
18+
WORKFLOW_OPERATIONS,
19+
} from '@/socket/constants'
1020

1121
const logger = createLogger('SocketDatabase')
1222

@@ -155,7 +165,7 @@ export async function persistWorkflowOperation(workflowId: string, operation: an
155165
try {
156166
const { operation: op, target, payload, timestamp, userId } = operation
157167

158-
if (op === 'update-position' && Math.random() < 0.01) {
168+
if (op === BLOCK_OPERATIONS.UPDATE_POSITION && Math.random() < 0.01) {
159169
logger.debug('Socket DB operation sample:', {
160170
operation: op,
161171
target,
@@ -170,25 +180,25 @@ export async function persistWorkflowOperation(workflowId: string, operation: an
170180
.where(eq(workflow.id, workflowId))
171181

172182
switch (target) {
173-
case 'block':
183+
case OPERATION_TARGETS.BLOCK:
174184
await handleBlockOperationTx(tx, workflowId, op, payload)
175185
break
176-
case 'blocks':
186+
case OPERATION_TARGETS.BLOCKS:
177187
await handleBlocksOperationTx(tx, workflowId, op, payload)
178188
break
179-
case 'edge':
189+
case OPERATION_TARGETS.EDGE:
180190
await handleEdgeOperationTx(tx, workflowId, op, payload)
181191
break
182-
case 'edges':
192+
case OPERATION_TARGETS.EDGES:
183193
await handleEdgesOperationTx(tx, workflowId, op, payload)
184194
break
185-
case 'subflow':
195+
case OPERATION_TARGETS.SUBFLOW:
186196
await handleSubflowOperationTx(tx, workflowId, op, payload)
187197
break
188-
case 'variable':
198+
case OPERATION_TARGETS.VARIABLE:
189199
await handleVariableOperationTx(tx, workflowId, op, payload)
190200
break
191-
case 'workflow':
201+
case OPERATION_TARGETS.WORKFLOW:
192202
await handleWorkflowOperationTx(tx, workflowId, op, payload)
193203
break
194204
default:
@@ -222,7 +232,7 @@ async function handleBlockOperationTx(
222232
payload: any
223233
) {
224234
switch (operation) {
225-
case 'update-position': {
235+
case BLOCK_OPERATIONS.UPDATE_POSITION: {
226236
if (!payload.id || !payload.position) {
227237
throw new Error('Missing required fields for update position operation')
228238
}
@@ -247,7 +257,7 @@ async function handleBlockOperationTx(
247257
break
248258
}
249259

250-
case 'update-name': {
260+
case BLOCK_OPERATIONS.UPDATE_NAME: {
251261
if (!payload.id || !payload.name) {
252262
throw new Error('Missing required fields for update name operation')
253263
}
@@ -269,7 +279,7 @@ async function handleBlockOperationTx(
269279
break
270280
}
271281

272-
case 'toggle-enabled': {
282+
case BLOCK_OPERATIONS.TOGGLE_ENABLED: {
273283
if (!payload.id) {
274284
throw new Error('Missing block ID for toggle enabled operation')
275285
}
@@ -299,7 +309,7 @@ async function handleBlockOperationTx(
299309
break
300310
}
301311

302-
case 'update-parent': {
312+
case BLOCK_OPERATIONS.UPDATE_PARENT: {
303313
if (!payload.id) {
304314
throw new Error('Missing block ID for update parent operation')
305315
}
@@ -364,7 +374,7 @@ async function handleBlockOperationTx(
364374
break
365375
}
366376

367-
case 'update-advanced-mode': {
377+
case BLOCK_OPERATIONS.UPDATE_ADVANCED_MODE: {
368378
if (!payload.id || payload.advancedMode === undefined) {
369379
throw new Error('Missing required fields for update advanced mode operation')
370380
}
@@ -386,7 +396,7 @@ async function handleBlockOperationTx(
386396
break
387397
}
388398

389-
case 'update-trigger-mode': {
399+
case BLOCK_OPERATIONS.UPDATE_TRIGGER_MODE: {
390400
if (!payload.id || payload.triggerMode === undefined) {
391401
throw new Error('Missing required fields for update trigger mode operation')
392402
}
@@ -408,7 +418,7 @@ async function handleBlockOperationTx(
408418
break
409419
}
410420

411-
case 'toggle-handles': {
421+
case BLOCK_OPERATIONS.TOGGLE_HANDLES: {
412422
if (!payload.id || payload.horizontalHandles === undefined) {
413423
throw new Error('Missing required fields for toggle handles operation')
414424
}
@@ -445,7 +455,7 @@ async function handleBlocksOperationTx(
445455
payload: any
446456
) {
447457
switch (operation) {
448-
case 'batch-update-positions': {
458+
case BLOCKS_OPERATIONS.BATCH_UPDATE_POSITIONS: {
449459
const { updates } = payload
450460
if (!Array.isArray(updates) || updates.length === 0) {
451461
return
@@ -466,7 +476,7 @@ async function handleBlocksOperationTx(
466476
break
467477
}
468478

469-
case 'batch-add-blocks': {
479+
case BLOCKS_OPERATIONS.BATCH_ADD_BLOCKS: {
470480
const { blocks, edges, loops, parallels } = payload
471481

472482
logger.info(`Batch adding blocks to workflow ${workflowId}`, {
@@ -578,7 +588,7 @@ async function handleBlocksOperationTx(
578588
break
579589
}
580590

581-
case 'batch-remove-blocks': {
591+
case BLOCKS_OPERATIONS.BATCH_REMOVE_BLOCKS: {
582592
const { ids } = payload
583593
if (!Array.isArray(ids) || ids.length === 0) {
584594
return
@@ -693,7 +703,7 @@ async function handleBlocksOperationTx(
693703
break
694704
}
695705

696-
case 'batch-toggle-enabled': {
706+
case BLOCKS_OPERATIONS.BATCH_TOGGLE_ENABLED: {
697707
const { blockIds } = payload
698708
if (!Array.isArray(blockIds) || blockIds.length === 0) {
699709
return
@@ -722,7 +732,7 @@ async function handleBlocksOperationTx(
722732
break
723733
}
724734

725-
case 'batch-toggle-handles': {
735+
case BLOCKS_OPERATIONS.BATCH_TOGGLE_HANDLES: {
726736
const { blockIds } = payload
727737
if (!Array.isArray(blockIds) || blockIds.length === 0) {
728738
return
@@ -749,7 +759,7 @@ async function handleBlocksOperationTx(
749759
break
750760
}
751761

752-
case 'batch-update-parent': {
762+
case BLOCKS_OPERATIONS.BATCH_UPDATE_PARENT: {
753763
const { updates } = payload
754764
if (!Array.isArray(updates) || updates.length === 0) {
755765
return
@@ -829,7 +839,7 @@ async function handleBlocksOperationTx(
829839

830840
async function handleEdgeOperationTx(tx: any, workflowId: string, operation: string, payload: any) {
831841
switch (operation) {
832-
case 'add': {
842+
case EDGE_OPERATIONS.ADD: {
833843
// Validate required fields
834844
if (!payload.id || !payload.source || !payload.target) {
835845
throw new Error('Missing required fields for add edge operation')
@@ -848,7 +858,7 @@ async function handleEdgeOperationTx(tx: any, workflowId: string, operation: str
848858
break
849859
}
850860

851-
case 'remove': {
861+
case EDGE_OPERATIONS.REMOVE: {
852862
if (!payload.id) {
853863
throw new Error('Missing edge ID for remove operation')
854864
}
@@ -879,7 +889,7 @@ async function handleEdgesOperationTx(
879889
payload: any
880890
) {
881891
switch (operation) {
882-
case 'batch-remove-edges': {
892+
case EDGES_OPERATIONS.BATCH_REMOVE_EDGES: {
883893
const { ids } = payload
884894
if (!Array.isArray(ids) || ids.length === 0) {
885895
logger.debug('No edge IDs provided for batch remove')
@@ -896,7 +906,7 @@ async function handleEdgesOperationTx(
896906
break
897907
}
898908

899-
case 'batch-add-edges': {
909+
case EDGES_OPERATIONS.BATCH_ADD_EDGES: {
900910
const { edges } = payload
901911
if (!Array.isArray(edges) || edges.length === 0) {
902912
logger.debug('No edges provided for batch add')
@@ -933,7 +943,7 @@ async function handleSubflowOperationTx(
933943
payload: any
934944
) {
935945
switch (operation) {
936-
case 'update': {
946+
case SUBFLOW_OPERATIONS.UPDATE: {
937947
if (!payload.id || !payload.config) {
938948
throw new Error('Missing required fields for update subflow operation')
939949
}
@@ -1060,7 +1070,7 @@ async function handleVariableOperationTx(
10601070
const currentVariables = (workflowData[0].variables as Record<string, any>) || {}
10611071

10621072
switch (operation) {
1063-
case 'add': {
1073+
case VARIABLE_OPERATIONS.ADD: {
10641074
if (!payload.id || !payload.name || payload.type === undefined) {
10651075
throw new Error('Missing required fields for add variable operation')
10661076
}
@@ -1089,7 +1099,7 @@ async function handleVariableOperationTx(
10891099
break
10901100
}
10911101

1092-
case 'remove': {
1102+
case VARIABLE_OPERATIONS.REMOVE: {
10931103
if (!payload.variableId) {
10941104
throw new Error('Missing variable ID for remove operation')
10951105
}
@@ -1123,7 +1133,7 @@ async function handleWorkflowOperationTx(
11231133
payload: any
11241134
) {
11251135
switch (operation) {
1126-
case 'replace-state': {
1136+
case WORKFLOW_OPERATIONS.REPLACE_STATE: {
11271137
if (!payload.state) {
11281138
throw new Error('Missing state for replace-state operation')
11291139
}

0 commit comments

Comments
 (0)