14
14
* limitations under the License.
15
15
*/
16
16
17
- import { useMemo , useState } from 'react'
17
+ import { useMemo , useRef , useState } from 'react'
18
18
import dayjs from 'dayjs'
19
19
20
20
import { DynamicTabType , InitTabType , noop } from '@devtron-labs/devtron-fe-common-lib'
@@ -27,6 +27,8 @@ import { AddTabParamsType, ParsedTabsData, PopulateTabDataPropsType, UseTabsRetu
27
27
export function useTabs ( persistenceKey : string , fallbackTabIndex = FALLBACK_TAB ) : UseTabsReturnType {
28
28
const [ tabs , setTabs ] = useState < DynamicTabType [ ] > ( [ ] )
29
29
30
+ const previousActiveTabIdRef = useRef < string > ( null )
31
+
30
32
const tabIdToTabMap = useMemo (
31
33
( ) =>
32
34
tabs . reduce ( ( acc , tab ) => {
@@ -49,15 +51,6 @@ export function useTabs(persistenceKey: string, fallbackTabIndex = FALLBACK_TAB)
49
51
const getTitleFromKindAndName = ( { kind, name } : Pick < InitTabType , 'kind' | 'name' > ) : string =>
50
52
kind ? `${ kind } /${ name } ` : name
51
53
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
-
61
54
const populateTabData = ( {
62
55
id,
63
56
name,
@@ -70,7 +63,6 @@ export function useTabs(persistenceKey: string, fallbackTabIndex = FALLBACK_TAB)
70
63
isAlive = false ,
71
64
hideName = false ,
72
65
tippyConfig,
73
- lastActiveTabId,
74
66
shouldRemainMounted,
75
67
isAlpha,
76
68
defaultUrl,
@@ -88,7 +80,6 @@ export function useTabs(persistenceKey: string, fallbackTabIndex = FALLBACK_TAB)
88
80
lastSyncMoment : dayjs ( ) ,
89
81
componentKey : getNewTabComponentKey ( id ) ,
90
82
tippyConfig,
91
- lastActiveTabId,
92
83
shouldRemainMounted,
93
84
isAlpha : isAlpha || false ,
94
85
defaultUrl : defaultUrl || null ,
@@ -161,7 +152,6 @@ export function useTabs(persistenceKey: string, fallbackTabIndex = FALLBACK_TAB)
161
152
isAlive : ! ! _initTab . isAlive ,
162
153
hideName : _initTab . hideName ,
163
154
tippyConfig : _initTab . tippyConfig ,
164
- lastActiveTabId : null ,
165
155
shouldRemainMounted : _initTab . shouldRemainMounted ,
166
156
isAlpha : _initTab . isAlpha ,
167
157
defaultUrl : _initTab . defaultUrl ,
@@ -309,6 +299,11 @@ export function useTabs(persistenceKey: string, fallbackTabIndex = FALLBACK_TAB)
309
299
const _tabs = prevTabs . map ( ( tab ) => {
310
300
const matched = tab . id === _id
311
301
found = found || matched
302
+
303
+ if ( tab . isSelected && ! matched ) {
304
+ previousActiveTabIdRef . current = tab . id
305
+ }
306
+
312
307
return matched
313
308
? {
314
309
...tab ,
@@ -335,7 +330,6 @@ export function useTabs(persistenceKey: string, fallbackTabIndex = FALLBACK_TAB)
335
330
showNameOnSelect,
336
331
dynamicTitle,
337
332
tippyConfig,
338
- lastActiveTabId : getLastActiveTabIdFromTabs ( prevTabs , _id ) ,
339
333
shouldRemainMounted : false ,
340
334
isAlpha : false ,
341
335
defaultUrl : null ,
@@ -355,12 +349,12 @@ export function useTabs(persistenceKey: string, fallbackTabIndex = FALLBACK_TAB)
355
349
const tabToBeRemoved = prevTabs . find ( ( tab ) => tab . id === id ) ?? ( { } as DynamicTabType )
356
350
357
351
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
359
356
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
364
358
// Cannot use structured clone since using it reloads the whole data
365
359
// eslint-disable-next-line no-param-reassign
366
360
prevTabs [ newSelectedTabIndex ] . isSelected = true
@@ -415,22 +409,31 @@ export function useTabs(persistenceKey: string, fallbackTabIndex = FALLBACK_TAB)
415
409
416
410
setTabs ( ( prevTabs ) => {
417
411
let isTabFound = false
412
+ let previousActiveTab = null
413
+
418
414
const _tabs = prevTabs . map ( ( tab ) => {
419
415
const isMatch = tab . id === id
420
416
isTabFound = isMatch || isTabFound
421
417
418
+ if ( tab . isSelected && ! isMatch ) {
419
+ previousActiveTab = tab
420
+ }
421
+
422
422
return {
423
423
...tab ,
424
424
isSelected : isMatch ,
425
425
url : ( isMatch && url ) || tab . url ,
426
- ...( isMatch && {
427
- lastActiveTabId : getLastActiveTabIdFromTabs ( prevTabs , tab . id ) ,
428
- ...( tab . showNameOnSelect && {
426
+ ...( isMatch &&
427
+ tab . showNameOnSelect && {
429
428
isAlive : true ,
430
429
} ) ,
431
- } ) ,
432
430
}
433
431
} )
432
+
433
+ if ( isTabFound && previousActiveTab ) {
434
+ previousActiveTabIdRef . current = previousActiveTab . id
435
+ }
436
+
434
437
resolve ( isTabFound )
435
438
updateTabsInLocalStorage ( _tabs )
436
439
return _tabs
0 commit comments