Skip to content

Commit c720f23

Browse files
fix(sockets): useCollabWorkflow cleanup, variables store logic simplification (#1154)
* fix(sockets): useCollabWorkflow cleanup, variables store logic simplification * remove unecessary check
1 parent 89f7d2b commit c720f23

File tree

8 files changed

+93
-304
lines changed

8 files changed

+93
-304
lines changed

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/variables/variables.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,8 @@ import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
3838
const logger = createLogger('Variables')
3939

4040
export function Variables() {
41-
const { activeWorkflowId, workflows } = useWorkflowRegistry()
42-
const {
43-
variables: storeVariables,
44-
addVariable,
45-
updateVariable,
46-
deleteVariable,
47-
duplicateVariable,
48-
getVariablesByWorkflowId,
49-
} = useVariablesStore()
41+
const { activeWorkflowId } = useWorkflowRegistry()
42+
const { getVariablesByWorkflowId } = useVariablesStore()
5043
const {
5144
collaborativeUpdateVariable,
5245
collaborativeAddVariable,

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/workflow.tsx

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import { useStreamCleanup } from '@/hooks/use-stream-cleanup'
3636
import { useWorkspacePermissions } from '@/hooks/use-workspace-permissions'
3737
import { useCopilotStore } from '@/stores/copilot/store'
3838
import { useExecutionStore } from '@/stores/execution/store'
39-
import { useVariablesStore } from '@/stores/panel/variables/store'
4039
import { useGeneralStore } from '@/stores/settings/general/store'
4140
import { useWorkflowDiffStore } from '@/stores/workflow-diff/store'
4241
import { hasWorkflowsInitiallyLoaded, useWorkflowRegistry } from '@/stores/workflows/registry/store'
@@ -178,8 +177,6 @@ const WorkflowContent = React.memo(() => {
178177
collaborativeSetSubblockValue,
179178
} = useCollaborativeWorkflow()
180179

181-
const { resetLoaded: resetVariablesLoaded } = useVariablesStore()
182-
183180
// Execution and debug mode state
184181
const { activeBlockIds, pendingBlocks } = useExecutionStore()
185182
const { isDebugModeEnabled } = useGeneralStore()
@@ -960,9 +957,6 @@ const WorkflowContent = React.memo(() => {
960957
const { activeWorkflowId } = useWorkflowRegistry.getState()
961958

962959
if (activeWorkflowId !== currentId) {
963-
// Only reset variables when actually switching workflows
964-
resetVariablesLoaded()
965-
966960
// Clear workflow diff store when switching workflows
967961
const { clearDiff } = useWorkflowDiffStore.getState()
968962
clearDiff()
@@ -975,15 +969,7 @@ const WorkflowContent = React.memo(() => {
975969
}
976970

977971
validateAndNavigate()
978-
}, [
979-
params.workflowId,
980-
workflows,
981-
isLoading,
982-
setActiveWorkflow,
983-
createWorkflow,
984-
router,
985-
resetVariablesLoaded,
986-
])
972+
}, [params.workflowId, workflows, isLoading, setActiveWorkflow, createWorkflow, router])
987973

988974
// Transform blocks and loops into ReactFlow nodes
989975
const nodes = useMemo(() => {

apps/sim/components/ui/tag-dropdown.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,9 @@ export const TagDropdown: React.FC<TagDropdownProps> = ({
298298
const workflowId = useWorkflowRegistry((state) => state.activeWorkflowId)
299299

300300
const getVariablesByWorkflowId = useVariablesStore((state) => state.getVariablesByWorkflowId)
301-
const loadVariables = useVariablesStore((state) => state.loadVariables)
302301
const variables = useVariablesStore((state) => state.variables)
303302
const workflowVariables = workflowId ? getVariablesByWorkflowId(workflowId) : []
304303

305-
useEffect(() => {
306-
if (workflowId) {
307-
loadVariables(workflowId)
308-
}
309-
}, [workflowId, loadVariables])
310-
311304
const searchTerm = useMemo(() => {
312305
const textBeforeCursor = inputValue.slice(0, cursorPosition)
313306
const match = textBeforeCursor.match(/<([^>]*)$/)

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

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ export function useCollaborativeWorkflow() {
6060
cancelOperationsForVariable,
6161
} = useOperationQueue()
6262

63+
const isInActiveRoom = useCallback(() => {
64+
return !!currentWorkflowId && activeWorkflowId === currentWorkflowId
65+
}, [currentWorkflowId, activeWorkflowId])
66+
6367
// Clear position timestamps when switching workflows
6468
// Note: Workflow joining is now handled automatically by socket connect event based on URL
6569
useEffect(() => {
@@ -470,6 +474,16 @@ export function useCollaborativeWorkflow() {
470474
return
471475
}
472476

477+
if (!isInActiveRoom()) {
478+
logger.debug('Skipping operation - not in active workflow', {
479+
currentWorkflowId,
480+
activeWorkflowId,
481+
operation,
482+
target,
483+
})
484+
return
485+
}
486+
473487
const operationId = crypto.randomUUID()
474488

475489
addToQueue({
@@ -485,7 +499,14 @@ export function useCollaborativeWorkflow() {
485499

486500
localAction()
487501
},
488-
[addToQueue, session?.user?.id, isShowingDiff]
502+
[
503+
addToQueue,
504+
session?.user?.id,
505+
isShowingDiff,
506+
activeWorkflowId,
507+
isInActiveRoom,
508+
currentWorkflowId,
509+
]
489510
)
490511

491512
const executeQueuedDebouncedOperation = useCallback(
@@ -497,11 +518,21 @@ export function useCollaborativeWorkflow() {
497518
return
498519
}
499520

521+
if (!isInActiveRoom()) {
522+
logger.debug('Skipping debounced operation - not in active workflow', {
523+
currentWorkflowId,
524+
activeWorkflowId,
525+
operation,
526+
target,
527+
})
528+
return
529+
}
530+
500531
localAction()
501532

502533
emitWorkflowOperation(operation, target, payload)
503534
},
504-
[emitWorkflowOperation, isShowingDiff]
535+
[emitWorkflowOperation, isShowingDiff, isInActiveRoom, currentWorkflowId, activeWorkflowId]
505536
)
506537

507538
const collaborativeAddBlock = useCallback(
@@ -521,6 +552,14 @@ export function useCollaborativeWorkflow() {
521552
return
522553
}
523554

555+
if (!isInActiveRoom()) {
556+
logger.debug('Skipping collaborative add block - not in active workflow', {
557+
currentWorkflowId,
558+
activeWorkflowId,
559+
})
560+
return
561+
}
562+
524563
const blockConfig = getBlock(type)
525564

526565
// Handle loop/parallel blocks that don't use BlockConfig
@@ -647,7 +686,15 @@ export function useCollaborativeWorkflow() {
647686
workflowStore.addEdge(autoConnectEdge)
648687
}
649688
},
650-
[workflowStore, emitWorkflowOperation, addToQueue, session?.user?.id, isShowingDiff]
689+
[
690+
workflowStore,
691+
activeWorkflowId,
692+
addToQueue,
693+
session?.user?.id,
694+
isShowingDiff,
695+
isInActiveRoom,
696+
currentWorkflowId,
697+
]
651698
)
652699

653700
const collaborativeRemoveBlock = useCallback(
@@ -674,14 +721,7 @@ export function useCollaborativeWorkflow() {
674721
workflowStore.updateBlockName(id, name)
675722
})
676723
},
677-
[
678-
executeQueuedOperation,
679-
workflowStore,
680-
addToQueue,
681-
subBlockStore,
682-
activeWorkflowId,
683-
session?.user?.id,
684-
]
724+
[executeQueuedOperation, workflowStore]
685725
)
686726

687727
const collaborativeToggleBlockEnabled = useCallback(
@@ -795,7 +835,7 @@ export function useCollaborativeWorkflow() {
795835
return
796836
}
797837

798-
if (!currentWorkflowId || activeWorkflowId !== currentWorkflowId) {
838+
if (!isInActiveRoom()) {
799839
logger.debug('Skipping subblock update - not in active workflow', {
800840
currentWorkflowId,
801841
activeWorkflowId,
@@ -847,12 +887,12 @@ export function useCollaborativeWorkflow() {
847887
},
848888
[
849889
subBlockStore,
850-
emitSubblockUpdate,
851890
currentWorkflowId,
852891
activeWorkflowId,
853892
addToQueue,
854893
session?.user?.id,
855894
isShowingDiff,
895+
isInActiveRoom,
856896
]
857897
)
858898

@@ -861,7 +901,7 @@ export function useCollaborativeWorkflow() {
861901
(blockId: string, subblockId: string, value: any) => {
862902
if (isApplyingRemoteChange.current) return
863903

864-
if (!currentWorkflowId || activeWorkflowId !== currentWorkflowId) {
904+
if (!isInActiveRoom()) {
865905
logger.debug('Skipping tag selection - not in active workflow', {
866906
currentWorkflowId,
867907
activeWorkflowId,
@@ -884,16 +924,32 @@ export function useCollaborativeWorkflow() {
884924
target: 'subblock',
885925
payload: { blockId, subblockId, value },
886926
},
887-
workflowId: activeWorkflowId,
927+
workflowId: activeWorkflowId || '',
888928
userId: session?.user?.id || 'unknown',
889929
immediate: true,
890930
})
891931
},
892-
[subBlockStore, addToQueue, currentWorkflowId, activeWorkflowId, session?.user?.id]
932+
[
933+
subBlockStore,
934+
addToQueue,
935+
currentWorkflowId,
936+
activeWorkflowId,
937+
session?.user?.id,
938+
isInActiveRoom,
939+
]
893940
)
894941

895942
const collaborativeDuplicateBlock = useCallback(
896943
(sourceId: string) => {
944+
if (!isInActiveRoom()) {
945+
logger.debug('Skipping duplicate block - not in active workflow', {
946+
currentWorkflowId,
947+
activeWorkflowId,
948+
sourceId,
949+
})
950+
return
951+
}
952+
897953
const sourceBlock = workflowStore.blocks[sourceId]
898954
if (!sourceBlock) return
899955

@@ -995,7 +1051,14 @@ export function useCollaborativeWorkflow() {
9951051
}
9961052
})
9971053
},
998-
[executeQueuedOperation, workflowStore, subBlockStore, activeWorkflowId]
1054+
[
1055+
executeQueuedOperation,
1056+
workflowStore,
1057+
subBlockStore,
1058+
activeWorkflowId,
1059+
isInActiveRoom,
1060+
currentWorkflowId,
1061+
]
9991062
)
10001063

10011064
const collaborativeUpdateLoopType = useCallback(

apps/sim/stores/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export const resetAllStores = () => {
231231
useConsoleStore.setState({ entries: [], isOpen: false })
232232
useCopilotStore.setState({ messages: [], isSendingMessage: false, error: null })
233233
useCustomToolsStore.setState({ tools: {} })
234-
useVariablesStore.getState().resetLoaded() // Reset variables store tracking
234+
// Variables store has no tracking to reset; registry hydrates
235235
useSubscriptionStore.getState().reset() // Reset subscription store
236236
}
237237

0 commit comments

Comments
 (0)