@@ -46,7 +46,11 @@ import {
4646import { CHAT_LIST_ANCHOR_OFFSET } from "@t3tools/shared/chatList" ;
4747import { projectScriptCwd , projectScriptRuntimeEnv } from "@t3tools/shared/projectScripts" ;
4848import { truncate } from "@t3tools/shared/String" ;
49- import { nextTerminalId , resolveTerminalSessionLabel } from "@t3tools/shared/terminalLabels" ;
49+ import {
50+ getTerminalLabel ,
51+ nextTerminalId ,
52+ resolveTerminalSessionLabel ,
53+ } from "@t3tools/shared/terminalLabels" ;
5054import { Debouncer } from "@tanstack/react-pacer" ;
5155import { useAtomValue } from "@effect/atom-react" ;
5256import {
@@ -192,8 +196,12 @@ import {
192196import { useNowMinute } from "../hooks/useNowMinute" ;
193197import { useNewThreadHandler } from "../hooks/useHandleNewThread" ;
194198import { resolveAppModelSelectionForInstance } from "../modelSelection" ;
199+ import { confirmTerminalClose , isTerminalCloseConfirmPending } from "../lib/terminalCloseConfirm" ;
195200import { getTerminalFocusOwner } from "../lib/terminalFocus" ;
196- import { preventRepeatedTerminalCloseShortcut } from "../lib/terminalCloseShortcut" ;
201+ import {
202+ preventRepeatedTerminalCloseShortcut ,
203+ preventTerminalCloseShortcut ,
204+ } from "../lib/terminalCloseShortcut" ;
197205import { resolveNewDraftStartFromOrigin } from "../lib/chatThreadActions" ;
198206import {
199207 derivePhysicalProjectKey ,
@@ -3453,6 +3461,24 @@ function ChatViewContent(props: ChatViewProps) {
34533461 } ,
34543462 [ activeRightPanelSurface , activeThreadRef , closeTerminalMutation , storeCloseTerminal ] ,
34553463 ) ;
3464+ const requestCloseTerminal = useCallback (
3465+ ( terminalId : string ) => {
3466+ const label = activeTerminalLabelsById . get ( terminalId ) ?? getTerminalLabel ( terminalId ) ;
3467+ void confirmTerminalClose ( [ label ] ) . then ( ( confirmed ) => {
3468+ if ( confirmed ) closeTerminal ( terminalId ) ;
3469+ } ) ;
3470+ } ,
3471+ [ activeTerminalLabelsById , closeTerminal ] ,
3472+ ) ;
3473+ const requestClosePanelTerminal = useCallback (
3474+ ( terminalId : string ) => {
3475+ const label = activeTerminalLabelsById . get ( terminalId ) ?? getTerminalLabel ( terminalId ) ;
3476+ void confirmTerminalClose ( [ label ] ) . then ( ( confirmed ) => {
3477+ if ( confirmed ) closePanelTerminal ( terminalId ) ;
3478+ } ) ;
3479+ } ,
3480+ [ activeTerminalLabelsById , closePanelTerminal ] ,
3481+ ) ;
34563482 const activateRightPanelSurface = useCallback (
34573483 ( surface : RightPanelSurface ) => {
34583484 if ( ! activeThreadRef ) return ;
@@ -3527,11 +3553,33 @@ function ChatViewContent(props: ChatViewProps) {
35273553 const closeRightPanelSurface = useCallback (
35283554 ( surface : RightPanelSurface ) => {
35293555 if ( ! activeThreadRef ) return ;
3530- cleanupRightPanelSurfaces ( [ surface ] ) ;
3531- useRightPanelStore . getState ( ) . closeSurface ( activeThreadRef , surface . id ) ;
3532- syncActivePreviewSurface ( ) ;
3556+ const finishClose = ( ) => {
3557+ cleanupRightPanelSurfaces ( [ surface ] ) ;
3558+ useRightPanelStore . getState ( ) . closeSurface ( activeThreadRef , surface . id ) ;
3559+ syncActivePreviewSurface ( ) ;
3560+ } ;
3561+ if ( surface . kind !== "terminal" ) {
3562+ finishClose ( ) ;
3563+ return ;
3564+ }
3565+ const activeLabel =
3566+ activeTerminalLabelsById . get ( surface . activeTerminalId ) ??
3567+ getTerminalLabel ( surface . activeTerminalId ) ;
3568+ const otherLabels = surface . terminalIds
3569+ . filter ( ( terminalId ) => terminalId !== surface . activeTerminalId )
3570+ . map (
3571+ ( terminalId ) => activeTerminalLabelsById . get ( terminalId ) ?? getTerminalLabel ( terminalId ) ,
3572+ ) ;
3573+ void confirmTerminalClose ( [ activeLabel , ...otherLabels ] ) . then ( ( confirmed ) => {
3574+ if ( confirmed ) finishClose ( ) ;
3575+ } ) ;
35333576 } ,
3534- [ activeThreadRef , cleanupRightPanelSurfaces , syncActivePreviewSurface ] ,
3577+ [
3578+ activeThreadRef ,
3579+ activeTerminalLabelsById ,
3580+ cleanupRightPanelSurfaces ,
3581+ syncActivePreviewSurface ,
3582+ ] ,
35353583 ) ;
35363584 const closeOtherRightPanelSurfaces = useCallback (
35373585 ( surface : RightPanelSurface ) => {
@@ -4724,6 +4772,13 @@ function ChatViewContent(props: ChatViewProps) {
47244772 event . stopPropagation ( ) ;
47254773 return ;
47264774 }
4775+ // While a close confirmation is open, terminal focus has moved to the
4776+ // dialog, so a deliberate second close shortcut would otherwise fall
4777+ // through to the native window/tab close accelerator.
4778+ if ( isTerminalCloseConfirmPending ( ) && preventTerminalCloseShortcut ( event , keybindings ) ) {
4779+ event . stopPropagation ( ) ;
4780+ return ;
4781+ }
47274782 if ( ! activeThreadId || isCommandPaletteOpen ( ) ) {
47284783 return ;
47294784 }
@@ -4807,11 +4862,11 @@ function ChatViewContent(props: ChatViewProps) {
48074862 event . preventDefault ( ) ;
48084863 event . stopPropagation ( ) ;
48094864 if ( terminalFocusOwner === "right-panel" && activeRightPanelSurface ?. kind === "terminal" ) {
4810- closePanelTerminal ( activeRightPanelSurface . activeTerminalId ) ;
4865+ requestClosePanelTerminal ( activeRightPanelSurface . activeTerminalId ) ;
48114866 return ;
48124867 }
48134868 if ( ! terminalUiState . terminalOpen ) return ;
4814- closeTerminal ( terminalUiState . activeTerminalId ) ;
4869+ requestCloseTerminal ( terminalUiState . activeTerminalId ) ;
48154870 return ;
48164871 }
48174872
@@ -4860,8 +4915,8 @@ function ChatViewContent(props: ChatViewProps) {
48604915 terminalUiState . terminalOpen ,
48614916 terminalUiState . activeTerminalId ,
48624917 activeThreadId ,
4863- closeTerminal ,
4864- closePanelTerminal ,
4918+ requestCloseTerminal ,
4919+ requestClosePanelTerminal ,
48654920 createNewTerminal ,
48664921 setTerminalOpen ,
48674922 runProjectScript ,
0 commit comments