Skip to content

Commit 6c4c97a

Browse files
committed
Make closeAllOtherTabs confirm and close tabs one-by-one
1 parent 0c38688 commit 6c4c97a

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

packages/compass-workspaces/src/stores/workspaces.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,13 +527,13 @@ describe('tabs behavior', function () {
527527
});
528528

529529
describe('closeAllOtherTabs', function () {
530-
it('should close all other tabs by index', function () {
530+
it('should close all other tabs by index', async function () {
531531
const store = configureStore();
532532
openTabs(store);
533533
const stateBefore = store.getState();
534534
expect(stateBefore.tabs.length).to.be.greaterThan(1);
535535

536-
store.dispatch(closeAllOtherTabs(1));
536+
await store.dispatch(closeAllOtherTabs(1));
537537
const state = store.getState();
538538
expect(state.tabs.length).to.equal(1);
539539
expect(state).to.have.property('activeTabId', stateBefore.tabs[1].id);

packages/compass-workspaces/src/stores/workspaces.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -895,23 +895,32 @@ export const closeTab = (
895895
};
896896
};
897897

898-
type CloseAllOtherTabsAction = {
899-
type: WorkspacesActions.CloseAllOtherTabs;
900-
atIndex: number;
901-
};
902-
903898
export const closeAllOtherTabs = (
904899
atIndex: number
905-
): WorkspacesThunkAction<Promise<void>, CloseAllOtherTabsAction> => {
900+
): WorkspacesThunkAction<Promise<void>, CloseTabsAction | SelectTabAction> => {
906901
return async (dispatch, getState) => {
907902
const { tabs } = getState();
908-
const otherTabs = tabs.filter((_, index) => index !== atIndex);
909-
for (const tab of otherTabs) {
910-
if (!canCloseTab(tab) && !(await confirmClosingTabs())) {
911-
return; // Abort the action
912-
}
913-
}
914-
dispatch({ type: WorkspacesActions.CloseAllOtherTabs, atIndex });
903+
const tabsToClose = await tabs.reduce(
904+
async (prev: Promise<WorkspaceTab[]>, tab, tabIndex) => {
905+
const tabsToClose = await prev;
906+
if (tabIndex === atIndex) {
907+
return tabsToClose; // Skip the tab which is not being closed
908+
}
909+
if (!canCloseTab(tab)) {
910+
// Select the closing tab - to show the confirmation dialog in context
911+
dispatch({ type: WorkspacesActions.SelectTab, atIndex: tabIndex });
912+
if (!(await confirmClosingTab())) {
913+
return tabsToClose; // Skip this tab
914+
}
915+
}
916+
return [...tabsToClose, tab];
917+
},
918+
Promise.resolve([])
919+
);
920+
dispatch({
921+
type: WorkspacesActions.CloseTabs,
922+
tabIds: tabsToClose.map((tab) => tab.id),
923+
});
915924
cleanupRemovedTabs(tabs, getState().tabs);
916925
};
917926
};

0 commit comments

Comments
 (0)