Skip to content

Commit 450be62

Browse files
committed
fix: switch to last active tab on tab close
1 parent a0d4f45 commit 450be62

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

src/components/common/DynamicTabs/types.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,7 @@ export type ParsedTabsData = {
8383
export interface PopulateTabDataPropsType
8484
extends Pick<
8585
DynamicTabType,
86-
| 'tippyConfig'
87-
| 'lastActiveTabId'
88-
| 'type'
89-
| 'isSelected'
90-
| 'url'
91-
| 'name'
92-
| 'dynamicTitle'
93-
| 'isAlive'
94-
| 'hideName'
95-
| 'id'
86+
'tippyConfig' | 'type' | 'isSelected' | 'url' | 'name' | 'dynamicTitle' | 'isAlive' | 'hideName' | 'id'
9687
>,
9788
Required<
9889
Pick<DynamicTabType, 'shouldRemainMounted' | 'title' | 'showNameOnSelect' | 'isAlpha' | 'defaultUrl'>

src/components/common/DynamicTabs/useTabs.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { useMemo, useState } from 'react'
17+
import { useMemo, useRef, useState } from 'react'
1818
import dayjs from 'dayjs'
1919

2020
import { DynamicTabType, InitTabType, noop } from '@devtron-labs/devtron-fe-common-lib'
@@ -27,6 +27,8 @@ import { AddTabParamsType, ParsedTabsData, PopulateTabDataPropsType, UseTabsRetu
2727
export function useTabs(persistenceKey: string, fallbackTabIndex = FALLBACK_TAB): UseTabsReturnType {
2828
const [tabs, setTabs] = useState<DynamicTabType[]>([])
2929

30+
const previousActiveTabIdRef = useRef<string>(null)
31+
3032
const tabIdToTabMap = useMemo(
3133
() =>
3234
tabs.reduce((acc, tab) => {
@@ -49,15 +51,6 @@ export function useTabs(persistenceKey: string, fallbackTabIndex = FALLBACK_TAB)
4951
const getTitleFromKindAndName = ({ kind, name }: Pick<InitTabType, 'kind' | 'name'>): string =>
5052
kind ? `${kind}/${name}` : name
5153

52-
const getLastActiveTabIdFromTabs = (
53-
_tabs: DynamicTabType[],
54-
id: DynamicTabType['id'],
55-
): DynamicTabType['lastActiveTabId'] | null => {
56-
const selectedTabId = _tabs.find((tab) => tab.isSelected)?.id ?? null
57-
58-
return selectedTabId !== id ? selectedTabId : null
59-
}
60-
6154
const populateTabData = ({
6255
id,
6356
name,
@@ -70,7 +63,6 @@ export function useTabs(persistenceKey: string, fallbackTabIndex = FALLBACK_TAB)
7063
isAlive = false,
7164
hideName = false,
7265
tippyConfig,
73-
lastActiveTabId,
7466
shouldRemainMounted,
7567
isAlpha,
7668
defaultUrl,
@@ -88,7 +80,6 @@ export function useTabs(persistenceKey: string, fallbackTabIndex = FALLBACK_TAB)
8880
lastSyncMoment: dayjs(),
8981
componentKey: getNewTabComponentKey(id),
9082
tippyConfig,
91-
lastActiveTabId,
9283
shouldRemainMounted,
9384
isAlpha: isAlpha || false,
9485
defaultUrl: defaultUrl || null,
@@ -161,7 +152,6 @@ export function useTabs(persistenceKey: string, fallbackTabIndex = FALLBACK_TAB)
161152
isAlive: !!_initTab.isAlive,
162153
hideName: _initTab.hideName,
163154
tippyConfig: _initTab.tippyConfig,
164-
lastActiveTabId: null,
165155
shouldRemainMounted: _initTab.shouldRemainMounted,
166156
isAlpha: _initTab.isAlpha,
167157
defaultUrl: _initTab.defaultUrl,
@@ -309,6 +299,11 @@ export function useTabs(persistenceKey: string, fallbackTabIndex = FALLBACK_TAB)
309299
const _tabs = prevTabs.map((tab) => {
310300
const matched = tab.id === _id
311301
found = found || matched
302+
303+
if (tab.isSelected && !matched) {
304+
previousActiveTabIdRef.current = tab.id
305+
}
306+
312307
return matched
313308
? {
314309
...tab,
@@ -335,7 +330,6 @@ export function useTabs(persistenceKey: string, fallbackTabIndex = FALLBACK_TAB)
335330
showNameOnSelect,
336331
dynamicTitle,
337332
tippyConfig,
338-
lastActiveTabId: getLastActiveTabIdFromTabs(prevTabs, _id),
339333
shouldRemainMounted: false,
340334
isAlpha: false,
341335
defaultUrl: null,
@@ -355,12 +349,12 @@ export function useTabs(persistenceKey: string, fallbackTabIndex = FALLBACK_TAB)
355349
const tabToBeRemoved = prevTabs.find((tab) => tab.id === id) ?? ({} as DynamicTabType)
356350

357351
if (tabToBeRemoved.isSelected) {
358-
const switchFromTabIndex = tabs.findIndex((tab) => tab.id === tabToBeRemoved.lastActiveTabId)
352+
const previousSelectedTabIndex = prevTabs.findIndex(
353+
(tab) => tab.id === previousActiveTabIdRef.current,
354+
)
355+
previousActiveTabIdRef.current = null
359356
const newSelectedTabIndex =
360-
// The id and lastActiveTabId can be same when the same tab is clicked again
361-
switchFromTabIndex > -1 && tabToBeRemoved.id !== tabToBeRemoved.lastActiveTabId
362-
? switchFromTabIndex
363-
: fallbackTabIndex
357+
previousSelectedTabIndex > -1 ? previousSelectedTabIndex : fallbackTabIndex
364358
// Cannot use structured clone since using it reloads the whole data
365359
// eslint-disable-next-line no-param-reassign
366360
prevTabs[newSelectedTabIndex].isSelected = true
@@ -415,22 +409,31 @@ export function useTabs(persistenceKey: string, fallbackTabIndex = FALLBACK_TAB)
415409

416410
setTabs((prevTabs) => {
417411
let isTabFound = false
412+
let previousActiveTab = null
413+
418414
const _tabs = prevTabs.map((tab) => {
419415
const isMatch = tab.id === id
420416
isTabFound = isMatch || isTabFound
421417

418+
if (tab.isSelected && !isMatch) {
419+
previousActiveTab = tab
420+
}
421+
422422
return {
423423
...tab,
424424
isSelected: isMatch,
425425
url: (isMatch && url) || tab.url,
426-
...(isMatch && {
427-
lastActiveTabId: getLastActiveTabIdFromTabs(prevTabs, tab.id),
428-
...(tab.showNameOnSelect && {
426+
...(isMatch &&
427+
tab.showNameOnSelect && {
429428
isAlive: true,
430429
}),
431-
}),
432430
}
433431
})
432+
433+
if (isTabFound && previousActiveTab) {
434+
previousActiveTabIdRef.current = previousActiveTab.id
435+
}
436+
434437
resolve(isTabFound)
435438
updateTabsInLocalStorage(_tabs)
436439
return _tabs

0 commit comments

Comments
 (0)