@@ -19,6 +19,7 @@ import {
1919 replaceBlock ,
2020 WOS ,
2121} from "@/app/store/global" ;
22+ import { TabBarModel } from "@/app/tab/tabbar-model" ;
2223import { WorkspaceLayoutModel } from "@/app/workspace/workspace-layout-model" ;
2324import { deleteLayoutModelForTab , getLayoutModelForStaticTab , NavigateDirection } from "@/layout/index" ;
2425import * as keyutil from "@/util/keyutil" ;
@@ -104,19 +105,40 @@ function shouldDispatchToBlock(e: WaveKeyboardEvent): boolean {
104105 return true ;
105106}
106107
107- function uxCloseBlock ( blockId : string ) {
108- const workspaceLayoutModel = WorkspaceLayoutModel . getInstance ( ) ;
109- const isAIPanelOpen = workspaceLayoutModel . getAIPanelVisible ( ) ;
110-
108+ function getStaticTabBlockCount ( ) : number {
111109 const tabId = globalStore . get ( atoms . staticTabId ) ;
112110 const tabORef = WOS . makeORef ( "tab" , tabId ) ;
113111 const tabAtom = WOS . getWaveObjectAtom < Tab > ( tabORef ) ;
114112 const tabData = globalStore . get ( tabAtom ) ;
115-
116- if ( isAIPanelOpen && tabData ?. blockids ?. length === 1 ) {
113+ return tabData ?. blockids ?. length ?? 0 ;
114+ }
115+
116+ function isStaticTabPinned ( ) : boolean {
117+ const ws = globalStore . get ( atoms . workspace ) ;
118+ const tabId = globalStore . get ( atoms . staticTabId ) ;
119+ return ws . pinnedtabids ?. includes ( tabId ) ?? false ;
120+ }
121+
122+ function simpleCloseStaticTab ( ) {
123+ const ws = globalStore . get ( atoms . workspace ) ;
124+ const tabId = globalStore . get ( atoms . staticTabId ) ;
125+ getApi ( ) . closeTab ( ws . oid , tabId ) ;
126+ deleteLayoutModelForTab ( tabId ) ;
127+ }
128+
129+ function uxCloseBlock ( blockId : string ) {
130+ if ( isStaticTabPinned ( ) && getStaticTabBlockCount ( ) === 1 ) {
131+ TabBarModel . getInstance ( ) . jiggleActivePinnedTab ( ) ;
132+ return ;
133+ }
134+
135+ const workspaceLayoutModel = WorkspaceLayoutModel . getInstance ( ) ;
136+ const isAIPanelOpen = workspaceLayoutModel . getAIPanelVisible ( ) ;
137+
138+ if ( isAIPanelOpen && getStaticTabBlockCount ( ) === 1 ) {
117139 const aiModel = WaveAIModel . getInstance ( ) ;
118140 const shouldSwitchToAI = ! aiModel . isChatEmpty || aiModel . hasNonEmptyInput ( ) ;
119-
141+
120142 if ( shouldSwitchToAI ) {
121143 replaceBlock (
122144 blockId ,
@@ -131,7 +153,7 @@ function uxCloseBlock(blockId: string) {
131153 return ;
132154 }
133155 }
134-
156+
135157 const layoutModel = getLayoutModelForStaticTab ( ) ;
136158 const node = layoutModel . getNodeByBlockId ( blockId ) ;
137159 if ( node ) {
@@ -145,22 +167,13 @@ function genericClose() {
145167 WorkspaceLayoutModel . getInstance ( ) . setAIPanelVisible ( false ) ;
146168 return ;
147169 }
148- const ws = globalStore . get ( atoms . workspace ) ;
149- const tabId = globalStore . get ( atoms . staticTabId ) ;
150- const tabORef = WOS . makeORef ( "tab" , tabId ) ;
151- const tabAtom = WOS . getWaveObjectAtom < Tab > ( tabORef ) ;
152- const tabData = globalStore . get ( tabAtom ) ;
153- if ( tabData == null ) {
170+ if ( isStaticTabPinned ( ) && getStaticTabBlockCount ( ) === 1 ) {
171+ TabBarModel . getInstance ( ) . jiggleActivePinnedTab ( ) ;
154172 return ;
155173 }
156- if ( ws . pinnedtabids ?. includes ( tabId ) && tabData . blockids ?. length == 1 ) {
157- // don't allow closing the last block in a pinned tab
158- return ;
159- }
160- if ( tabData . blockids == null || tabData . blockids . length == 0 ) {
161- // close tab
162- getApi ( ) . closeTab ( ws . oid , tabId ) ;
163- deleteLayoutModelForTab ( tabId ) ;
174+ const blockCount = getStaticTabBlockCount ( ) ;
175+ if ( blockCount === 0 ) {
176+ simpleCloseStaticTab ( ) ;
164177 return ;
165178 }
166179 const layoutModel = getLayoutModelForStaticTab ( ) ;
@@ -467,16 +480,11 @@ function registerGlobalKeys() {
467480 return true ;
468481 } ) ;
469482 globalKeyMap . set ( "Cmd:Shift:w" , ( ) => {
470- const tabId = globalStore . get ( atoms . staticTabId ) ;
471- const ws = globalStore . get ( atoms . workspace ) ;
472- if ( ws . pinnedtabids ?. includes ( tabId ) ) {
473- // switch to first unpinned tab if it exists (for close spamming)
474- if ( ws . tabids != null && ws . tabids . length > 0 ) {
475- getApi ( ) . setActiveTab ( ws . tabids [ 0 ] ) ;
476- }
483+ if ( isStaticTabPinned ( ) ) {
484+ TabBarModel . getInstance ( ) . jiggleActivePinnedTab ( ) ;
477485 return true ;
478486 }
479- getApi ( ) . closeTab ( ws . oid , tabId ) ;
487+ simpleCloseStaticTab ( ) ;
480488 return true ;
481489 } ) ;
482490 globalKeyMap . set ( "Cmd:m" , ( ) => {
0 commit comments