Skip to content

Commit d330694

Browse files
refactor: dedupe collaborative undo prune
1 parent 6547518 commit d330694

File tree

1 file changed

+31
-89
lines changed

1 file changed

+31
-89
lines changed

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

Lines changed: 31 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ function parseUndoRedoStackKey(key: string): { workflowId: string; userId: strin
4545
}
4646
}
4747

48+
function pruneUndoRedoStacksForWorkflow(
49+
workflowId: string,
50+
graph?: { blocksById: Record<string, BlockState>; edgesById: Record<string, Edge> }
51+
) {
52+
const resolvedGraph = graph ?? {
53+
blocksById: useWorkflowStore.getState().blocks,
54+
edgesById: Object.fromEntries(useWorkflowStore.getState().edges.map((edge) => [edge.id, edge])),
55+
}
56+
57+
const undoRedoStore = useUndoRedoStore.getState()
58+
const stackKeys = Object.keys(undoRedoStore.stacks)
59+
stackKeys.forEach((key) => {
60+
const parsedKey = parseUndoRedoStackKey(key)
61+
if (parsedKey?.workflowId === workflowId) {
62+
undoRedoStore.pruneInvalidEntries(parsedKey.workflowId, parsedKey.userId, resolvedGraph)
63+
}
64+
})
65+
}
66+
4867
export function useCollaborativeWorkflow() {
4968
const undoRedo = useUndoRedo()
5069
const isUndoRedoInProgress = useRef(false)
@@ -248,23 +267,9 @@ export function useCollaborativeWorkflow() {
248267
},
249268
])
250269

251-
// Prune undo/redo stacks since remote parent change may invalidate local history
252-
const freshStore = useWorkflowStore.getState()
253-
const updatedBlocks = freshStore.blocks
254-
const updatedEdges = freshStore.edges
255-
const graph = {
256-
blocksById: updatedBlocks,
257-
edgesById: Object.fromEntries(updatedEdges.map((e) => [e.id, e])),
270+
if (activeWorkflowId) {
271+
pruneUndoRedoStacksForWorkflow(activeWorkflowId)
258272
}
259-
260-
const undoRedoStore = useUndoRedoStore.getState()
261-
const stackKeys = Object.keys(undoRedoStore.stacks)
262-
stackKeys.forEach((key) => {
263-
const parsedKey = parseUndoRedoStackKey(key)
264-
if (parsedKey?.workflowId === activeWorkflowId) {
265-
undoRedoStore.pruneInvalidEntries(parsedKey.workflowId, parsedKey.userId, graph)
266-
}
267-
})
268273
}
269274
break
270275
}
@@ -317,23 +322,9 @@ export function useCollaborativeWorkflow() {
317322
)
318323
)
319324

320-
// Prune undo/redo stacks
321-
const freshStore = useWorkflowStore.getState()
322-
const updatedBlocks = freshStore.blocks
323-
const updatedEdges = freshStore.edges
324-
const graph = {
325-
blocksById: updatedBlocks,
326-
edgesById: Object.fromEntries(updatedEdges.map((e) => [e.id, e])),
325+
if (activeWorkflowId) {
326+
pruneUndoRedoStacksForWorkflow(activeWorkflowId)
327327
}
328-
329-
const undoRedoStore = useUndoRedoStore.getState()
330-
const stackKeys = Object.keys(undoRedoStore.stacks)
331-
stackKeys.forEach((key) => {
332-
const parsedKey = parseUndoRedoStackKey(key)
333-
if (parsedKey?.workflowId === activeWorkflowId) {
334-
undoRedoStore.pruneInvalidEntries(parsedKey.workflowId, parsedKey.userId, graph)
335-
}
336-
})
337328
}
338329

339330
logger.info('Successfully applied batch-update-parent from remote user')
@@ -347,21 +338,9 @@ export function useCollaborativeWorkflow() {
347338
if (Array.isArray(ids) && ids.length > 0) {
348339
useWorkflowStore.getState().batchRemoveEdges(ids)
349340

350-
const updatedBlocks = useWorkflowStore.getState().blocks
351-
const updatedEdges = useWorkflowStore.getState().edges
352-
const graph = {
353-
blocksById: updatedBlocks,
354-
edgesById: Object.fromEntries(updatedEdges.map((e) => [e.id, e])),
341+
if (activeWorkflowId) {
342+
pruneUndoRedoStacksForWorkflow(activeWorkflowId)
355343
}
356-
357-
const undoRedoStore = useUndoRedoStore.getState()
358-
const stackKeys = Object.keys(undoRedoStore.stacks)
359-
stackKeys.forEach((key) => {
360-
const parsedKey = parseUndoRedoStackKey(key)
361-
if (parsedKey?.workflowId === activeWorkflowId) {
362-
undoRedoStore.pruneInvalidEntries(parsedKey.workflowId, parsedKey.userId, graph)
363-
}
364-
})
365344
}
366345
break
367346
}
@@ -389,22 +368,10 @@ export function useCollaborativeWorkflow() {
389368
})
390369
useWorkflowStore.getState().batchRemoveEdges([payload.id])
391370

392-
const updatedBlocks = useWorkflowStore.getState().blocks
393-
const updatedEdges = useWorkflowStore.getState().edges
394-
const graph = {
395-
blocksById: updatedBlocks,
396-
edgesById: Object.fromEntries(updatedEdges.map((e) => [e.id, e])),
371+
if (activeWorkflowId) {
372+
pruneUndoRedoStacksForWorkflow(activeWorkflowId)
397373
}
398374

399-
const undoRedoStore = useUndoRedoStore.getState()
400-
const stackKeys = Object.keys(undoRedoStore.stacks)
401-
stackKeys.forEach((key) => {
402-
const parsedKey = parseUndoRedoStackKey(key)
403-
if (parsedKey?.workflowId === activeWorkflowId) {
404-
undoRedoStore.pruneInvalidEntries(parsedKey.workflowId, parsedKey.userId, graph)
405-
}
406-
})
407-
408375
logger.info('Successfully applied remove-edge from remote user')
409376
}
410377
break
@@ -548,23 +515,9 @@ export function useCollaborativeWorkflow() {
548515
const store = useWorkflowStore.getState()
549516
store.batchRemoveBlocks(ids)
550517

551-
// Prune undo/redo stacks
552-
const freshStore = useWorkflowStore.getState()
553-
const updatedBlocks = freshStore.blocks
554-
const updatedEdges = freshStore.edges
555-
const graph = {
556-
blocksById: updatedBlocks,
557-
edgesById: Object.fromEntries(updatedEdges.map((e) => [e.id, e])),
518+
if (activeWorkflowId) {
519+
pruneUndoRedoStacksForWorkflow(activeWorkflowId)
558520
}
559-
560-
const undoRedoStore = useUndoRedoStore.getState()
561-
const stackKeys = Object.keys(undoRedoStore.stacks)
562-
stackKeys.forEach((key) => {
563-
const parsedKey = parseUndoRedoStackKey(key)
564-
if (parsedKey?.workflowId === activeWorkflowId) {
565-
undoRedoStore.pruneInvalidEntries(parsedKey.workflowId, parsedKey.userId, graph)
566-
}
567-
})
568521
}
569522

570523
logger.info('Successfully applied batch-remove-blocks from remote user')
@@ -751,22 +704,11 @@ export function useCollaborativeWorkflow() {
751704

752705
logger.info(`Successfully loaded reverted workflow state for ${workflowId}`)
753706

754-
const graph = {
707+
pruneUndoRedoStacksForWorkflow(workflowId, {
755708
blocksById: workflowData.state.blocks || {},
756709
edgesById: Object.fromEntries(
757-
(workflowData.state.edges || []).map((e: any) => [e.id, e])
710+
(workflowData.state.edges || []).map((edge: any) => [edge.id, edge])
758711
),
759-
}
760-
761-
const undoRedoStore = useUndoRedoStore.getState()
762-
const stackKeys = Object.keys(undoRedoStore.stacks)
763-
stackKeys.forEach((key) => {
764-
const parsedKey = parseUndoRedoStackKey(key)
765-
if (!parsedKey || parsedKey.workflowId !== workflowId) {
766-
return
767-
}
768-
769-
undoRedoStore.pruneInvalidEntries(parsedKey.workflowId, parsedKey.userId, graph)
770712
})
771713
} finally {
772714
isApplyingRemoteChange.current = false

0 commit comments

Comments
 (0)