From 73d532f145335744f8c90b20f7d7d2a4ed2d4521 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 25 Jul 2022 12:36:21 +0530 Subject: [PATCH] fix(gui-v2): handle closing active tab Signed-off-by: Pranav C --- packages/nc-gui-v2/composables/useTabs.ts | 26 +++++++++++------------ 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/nc-gui-v2/composables/useTabs.ts b/packages/nc-gui-v2/composables/useTabs.ts index e2e5a65517..2485fb46bb 100644 --- a/packages/nc-gui-v2/composables/useTabs.ts +++ b/packages/nc-gui-v2/composables/useTabs.ts @@ -1,4 +1,6 @@ +import type { WritableComputedRef } from '@vue/reactivity' import { useState } from '#app' +import useProject from '~/composables/useProject' export interface TabItem { type: 'table' | 'view' | 'auth' @@ -43,13 +45,6 @@ export default () => { router.push(`/nc/${route.params.projectId}/table/${tabs.value?.[index]?.title}`) } }, - - // if (route.params.title) { - // const tab = tabs.value.find(t => t.id === route.params.tab) - // if (tab) { - // activeTab.value = tabs.value.indexOf(tab) - // } - // } }) const addTab = (tabMeta: TabItem) => { @@ -67,13 +62,18 @@ export default () => { const clearTabs = () => { tabs.value = [] } - - const closeTab = (key: number | Partial) => { - if (typeof key === 'number') tabs.value.splice(key, 1) - else { - const index = tabs.value.findIndex(getPredicate(key)) - if (index > -1) tabs.value.splice(index, 1) + const closeTab = async (key: number | Partial) => { + const index = typeof key === 'number' ? key : tabs.value.findIndex(getPredicate(key)) + if (activeTab.value === index) { + let newTabIndex = index - 1 + if (newTabIndex < 0 && tabs.value?.length > 1) newTabIndex = index + 1 + if (newTabIndex === -1) { + await router.push(`/nc/${route.params.projectId}`) + } else { + await router.push(`/nc/${route.params.projectId}/table/${tabs.value?.[newTabIndex]?.title}`) + } } + tabs.value.splice(index, 1) } const updateTab = (key: number | Partial, newTabItemProps: Partial) => {