@@ -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 (
0 commit comments