Skip to content

Commit 5d2d8d4

Browse files
authored
Merge pull request #2824 from devtron-labs/fix/use-tabs
fix: switch to last active tab on tab close
2 parents cad2de4 + 5affbfe commit 5d2d8d4

File tree

4 files changed

+33
-39
lines changed

4 files changed

+33
-39
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"private": true,
55
"homepage": "/dashboard",
66
"dependencies": {
7-
"@devtron-labs/devtron-fe-common-lib": "1.17.0-pre-10",
7+
"@devtron-labs/devtron-fe-common-lib": "1.17.0-pre-11",
88
"@esbuild-plugins/node-globals-polyfill": "0.2.3",
99
"@rjsf/core": "^5.13.3",
1010
"@rjsf/utils": "^5.13.3",

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

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,9 +1722,9 @@ __metadata:
17221722
languageName: node
17231723
linkType: hard
17241724

1725-
"@devtron-labs/devtron-fe-common-lib@npm:1.17.0-pre-10":
1726-
version: 1.17.0-pre-10
1727-
resolution: "@devtron-labs/devtron-fe-common-lib@npm:1.17.0-pre-10"
1725+
"@devtron-labs/devtron-fe-common-lib@npm:1.17.0-pre-11":
1726+
version: 1.17.0-pre-11
1727+
resolution: "@devtron-labs/devtron-fe-common-lib@npm:1.17.0-pre-11"
17281728
dependencies:
17291729
"@codemirror/autocomplete": "npm:6.18.6"
17301730
"@codemirror/lang-json": "npm:6.0.1"
@@ -1773,7 +1773,7 @@ __metadata:
17731773
react-select: 5.8.0
17741774
rxjs: ^7.8.1
17751775
yaml: ^2.4.1
1776-
checksum: 10c0/828f5bd3714c5c9d990d1bbf0bf0267ad4b4f1354b214406bf2b2f559517bb864cf864b41a21570bfa6f8a77cc6fadf3d8e81ba5340f15fcbeb0e17d86aa48e7
1776+
checksum: 10c0/a06781a344bfedcc76d6543fe67e53b2b3c97180884e26b73b3fa691cb9c90c486602481c09326002eba792f9a0bef87062bd4b481600af9614c86a36a7e3b59
17771777
languageName: node
17781778
linkType: hard
17791779

@@ -5685,7 +5685,7 @@ __metadata:
56855685
version: 0.0.0-use.local
56865686
resolution: "dashboard@workspace:."
56875687
dependencies:
5688-
"@devtron-labs/devtron-fe-common-lib": "npm:1.17.0-pre-10"
5688+
"@devtron-labs/devtron-fe-common-lib": "npm:1.17.0-pre-11"
56895689
"@esbuild-plugins/node-globals-polyfill": "npm:0.2.3"
56905690
"@playwright/test": "npm:^1.32.1"
56915691
"@rjsf/core": "npm:^5.13.3"

0 commit comments

Comments
 (0)