11import { EditorContext } from "@/components/providers/editor-context-provider" ;
22import { IMCContext } from "@/components/providers/imc-provider" ;
3+ import { addToast } from "@heroui/react" ;
34import { ViewModeEnum } from "@pulse-editor/shared-utils" ;
45import { useContext , useEffect , useState } from "react" ;
56import { v4 } from "uuid" ;
@@ -206,6 +207,15 @@ export function useTabViewManager() {
206207 tabIndex : newIdx ,
207208 } ;
208209 } ) ;
210+
211+ if ( view . type === ViewModeEnum . Canvas ) {
212+ // Remove the app nodes' view IDs from IMC context
213+ ( view . config as CanvasViewConfig ) . appConfigs ?. forEach ( ( appConfig ) => {
214+ imcContext ?. polyIMC ?. removeChannel ( appConfig . viewId ) ;
215+ } ) ;
216+ } else if ( view . type === ViewModeEnum . App ) {
217+ imcContext ?. polyIMC ?. removeChannel ( ( view . config as AppViewConfig ) . viewId ) ;
218+ }
209219 }
210220
211221 /**
@@ -222,6 +232,19 @@ export function useTabViewManager() {
222232 tabIndex : - 1 ,
223233 } ;
224234 } ) ;
235+
236+ // For all tab views, remove their view IDs from IMC context
237+ tabViews . forEach ( ( view ) => {
238+ if ( view . type === ViewModeEnum . Canvas ) {
239+ ( view . config as CanvasViewConfig ) . appConfigs ?. forEach ( ( appConfig ) => {
240+ imcContext ?. polyIMC ?. removeChannel ( appConfig . viewId ) ;
241+ } ) ;
242+ } else if ( view . type === ViewModeEnum . App ) {
243+ imcContext ?. polyIMC ?. removeChannel (
244+ ( view . config as AppViewConfig ) . viewId ,
245+ ) ;
246+ }
247+ } ) ;
225248 }
226249
227250 function viewCount ( ) : number {
@@ -265,6 +288,20 @@ export function useTabViewManager() {
265288 throw new Error ( "IMC context is not available" ) ;
266289 }
267290
291+ // Prohibit creating canvas if any app's view ID in the canvas already exists
292+ const existViewId = canvasConfig . appConfigs ?. find ( ( appConfig ) =>
293+ imcContext ?. polyIMC ?. hasChannel ( appConfig . viewId ) ,
294+ ) ;
295+
296+ if ( existViewId ) {
297+ addToast ( {
298+ title : "Error creating canvas" ,
299+ description : `Same app nodes already exist. Your workflow might already be opened in another tab.` ,
300+ color : "danger" ,
301+ } ) ;
302+ return undefined ;
303+ }
304+
268305 const newTabView : TabView = {
269306 type : ViewModeEnum . Canvas ,
270307 config : canvasConfig ,
@@ -297,6 +334,10 @@ export function useTabViewManager() {
297334 currentTab = await createCanvasTabView ( {
298335 viewId : `canvas-${ v4 ( ) } ` ,
299336 } as CanvasViewConfig ) ;
337+ if ( ! currentTab ) {
338+ console . error ( "Failed to create a new canvas tab" ) ;
339+ return ;
340+ }
300341 }
301342
302343 const newCanvasConfig : CanvasViewConfig = {
0 commit comments