@@ -3098,26 +3098,19 @@ function ChatViewContent(props: ChatViewProps) {
30983098 threadKey === routeThreadKey ? null : routeThreadKey ,
30993099 ) ;
31003100 } ;
3101- const closeRightPanelSurface = useCallback (
3102- ( surface : RightPanelSurface ) => {
3101+ const cleanupRightPanelSurfaces = useCallback (
3102+ ( surfaces : readonly RightPanelSurface [ ] ) => {
31033103 if ( ! activeThreadRef ) return ;
3104- if ( surface . kind === "preview" && surface . resourceId ) {
3105- usePreviewStateStore . getState ( ) . removeSession ( activeThreadRef , surface . resourceId ) ;
3106- const api = readEnvironmentApi ( activeThreadRef . environmentId ) ;
3107- void api ?. preview
3108- . close ( { threadId : activeThreadRef . threadId , tabId : surface . resourceId } )
3109- . catch ( ( ) => undefined ) ;
3110- }
3111- useRightPanelStore . getState ( ) . closeSurface ( activeThreadRef , surface . id ) ;
3112- const nextActiveSurface = selectActiveRightPanelSurface (
3113- useRightPanelStore . getState ( ) . byThreadKey ,
3114- activeThreadRef ,
3115- ) ;
3116- if ( nextActiveSurface ?. kind === "preview" && nextActiveSurface . resourceId ) {
3117- usePreviewStateStore . getState ( ) . setActiveTab ( activeThreadRef , nextActiveSurface . resourceId ) ;
3118- }
3119- if ( surface . kind === "terminal" ) {
3120- const api = readEnvironmentApi ( activeThreadRef . environmentId ) ;
3104+
3105+ const api = readEnvironmentApi ( activeThreadRef . environmentId ) ;
3106+ for ( const surface of surfaces ) {
3107+ if ( surface . kind === "preview" && surface . resourceId ) {
3108+ usePreviewStateStore . getState ( ) . removeSession ( activeThreadRef , surface . resourceId ) ;
3109+ void api ?. preview
3110+ . close ( { threadId : activeThreadRef . threadId , tabId : surface . resourceId } )
3111+ . catch ( ( ) => undefined ) ;
3112+ }
3113+ if ( surface . kind !== "terminal" ) continue ;
31213114 for ( const terminalId of surface . terminalIds ) {
31223115 void api ?. terminal
31233116 . close ( {
@@ -3128,7 +3121,8 @@ function ChatViewContent(props: ChatViewProps) {
31283121 . catch ( ( ) => undefined ) ;
31293122 }
31303123 }
3131- if ( surface . kind === "diff" && diffOpen ) {
3124+
3125+ if ( diffOpen && surfaces . some ( ( surface ) => surface . kind === "diff" ) ) {
31323126 void navigate ( {
31333127 to : "/$environmentId/$threadId" ,
31343128 params : { environmentId, threadId } ,
@@ -3139,6 +3133,93 @@ function ChatViewContent(props: ChatViewProps) {
31393133 } ,
31403134 [ activeThreadRef , diffOpen , environmentId , navigate , threadId ] ,
31413135 ) ;
3136+ const syncActivePreviewSurface = useCallback ( ( ) => {
3137+ if ( ! activeThreadRef ) return ;
3138+ const nextActiveSurface = selectActiveRightPanelSurface (
3139+ useRightPanelStore . getState ( ) . byThreadKey ,
3140+ activeThreadRef ,
3141+ ) ;
3142+ if ( nextActiveSurface ?. kind === "preview" && nextActiveSurface . resourceId ) {
3143+ usePreviewStateStore . getState ( ) . setActiveTab ( activeThreadRef , nextActiveSurface . resourceId ) ;
3144+ }
3145+ } , [ activeThreadRef ] ) ;
3146+ const closeRightPanelSurface = useCallback (
3147+ ( surface : RightPanelSurface ) => {
3148+ if ( ! activeThreadRef ) return ;
3149+ cleanupRightPanelSurfaces ( [ surface ] ) ;
3150+ useRightPanelStore . getState ( ) . closeSurface ( activeThreadRef , surface . id ) ;
3151+ syncActivePreviewSurface ( ) ;
3152+ } ,
3153+ [ activeThreadRef , cleanupRightPanelSurfaces , syncActivePreviewSurface ] ,
3154+ ) ;
3155+ const closeOtherRightPanelSurfaces = useCallback (
3156+ ( surface : RightPanelSurface ) => {
3157+ if ( ! activeThreadRef ) return ;
3158+ const surfaces = rightPanelState . surfaces . filter ( ( entry ) => entry . id !== surface . id ) ;
3159+ cleanupRightPanelSurfaces ( surfaces ) ;
3160+ useRightPanelStore . getState ( ) . closeOtherSurfaces ( activeThreadRef , surface . id ) ;
3161+ syncActivePreviewSurface ( ) ;
3162+ } ,
3163+ [
3164+ activeThreadRef ,
3165+ cleanupRightPanelSurfaces ,
3166+ rightPanelState . surfaces ,
3167+ syncActivePreviewSurface ,
3168+ ] ,
3169+ ) ;
3170+ const closeRightPanelSurfacesToRight = useCallback (
3171+ ( surface : RightPanelSurface ) => {
3172+ if ( ! activeThreadRef ) return ;
3173+ const surfaceIndex = rightPanelState . surfaces . findIndex ( ( entry ) => entry . id === surface . id ) ;
3174+ if ( surfaceIndex < 0 ) return ;
3175+ const surfaces = rightPanelState . surfaces . slice ( surfaceIndex + 1 ) ;
3176+ cleanupRightPanelSurfaces ( surfaces ) ;
3177+ useRightPanelStore . getState ( ) . closeSurfacesToRight ( activeThreadRef , surface . id ) ;
3178+ syncActivePreviewSurface ( ) ;
3179+ } ,
3180+ [
3181+ activeThreadRef ,
3182+ cleanupRightPanelSurfaces ,
3183+ rightPanelState . surfaces ,
3184+ syncActivePreviewSurface ,
3185+ ] ,
3186+ ) ;
3187+ const closeAllRightPanelSurfaces = useCallback ( ( ) => {
3188+ if ( ! activeThreadRef ) return ;
3189+ cleanupRightPanelSurfaces ( rightPanelState . surfaces ) ;
3190+ useRightPanelStore . getState ( ) . closeAllSurfaces ( activeThreadRef ) ;
3191+ } , [ activeThreadRef , cleanupRightPanelSurfaces , rightPanelState . surfaces ] ) ;
3192+ const copyRightPanelFilePath = useCallback ( ( relativePath : string ) => {
3193+ if ( typeof window === "undefined" || ! navigator . clipboard ?. writeText ) {
3194+ toastManager . add (
3195+ stackedThreadToast ( {
3196+ type : "error" ,
3197+ title : "Failed to copy path" ,
3198+ description : "Clipboard API unavailable." ,
3199+ } ) ,
3200+ ) ;
3201+ return ;
3202+ }
3203+
3204+ void navigator . clipboard . writeText ( relativePath ) . then (
3205+ ( ) => {
3206+ toastManager . add ( {
3207+ type : "success" ,
3208+ title : "Path copied" ,
3209+ description : relativePath ,
3210+ } ) ;
3211+ } ,
3212+ ( error ) => {
3213+ toastManager . add (
3214+ stackedThreadToast ( {
3215+ type : "error" ,
3216+ title : "Failed to copy path" ,
3217+ description : error instanceof Error ? error . message : "An error occurred." ,
3218+ } ) ,
3219+ ) ;
3220+ } ,
3221+ ) ;
3222+ } , [ ] ) ;
31423223 const persistThreadSettingsForNextTurn = useCallback (
31433224 async ( input : {
31443225 threadId : ThreadId ;
@@ -4836,6 +4917,10 @@ function ChatViewContent(props: ChatViewProps) {
48364917 terminalLabelsById = { activeTerminalLabelsById }
48374918 onActivate = { activateRightPanelSurface }
48384919 onCloseSurface = { closeRightPanelSurface }
4920+ onCloseOtherSurfaces = { closeOtherRightPanelSurfaces }
4921+ onCloseSurfacesToRight = { closeRightPanelSurfacesToRight }
4922+ onCloseAllSurfaces = { closeAllRightPanelSurfaces }
4923+ onCopyFilePath = { copyRightPanelFilePath }
48394924 onAddBrowser = { createBrowserSurface }
48404925 onAddTerminal = { addTerminalSurface }
48414926 onAddDiff = { addDiffSurface }
@@ -4910,6 +4995,10 @@ function ChatViewContent(props: ChatViewProps) {
49104995 terminalLabelsById = { activeTerminalLabelsById }
49114996 onActivate = { activateRightPanelSurface }
49124997 onCloseSurface = { closeRightPanelSurface }
4998+ onCloseOtherSurfaces = { closeOtherRightPanelSurfaces }
4999+ onCloseSurfacesToRight = { closeRightPanelSurfacesToRight }
5000+ onCloseAllSurfaces = { closeAllRightPanelSurfaces }
5001+ onCopyFilePath = { copyRightPanelFilePath }
49135002 onAddBrowser = { createBrowserSurface }
49145003 onAddTerminal = { addTerminalSurface }
49155004 onAddDiff = { addDiffSurface }
0 commit comments