From 01d2b7d0a3f5cd721098c4bc1c0bffef117fa6cd Mon Sep 17 00:00:00 2001 From: Nawaz Dhandala Date: Fri, 20 Mar 2026 13:34:02 +0000 Subject: [PATCH] feat(tabs): refactor tab state management to use tab names for improved clarity --- Common/UI/Components/Tabs/Tabs.tsx | 37 +++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/Common/UI/Components/Tabs/Tabs.tsx b/Common/UI/Components/Tabs/Tabs.tsx index 61f46d843b..668c486d4d 100644 --- a/Common/UI/Components/Tabs/Tabs.tsx +++ b/Common/UI/Components/Tabs/Tabs.tsx @@ -3,6 +3,7 @@ import React, { FunctionComponent, ReactElement, useEffect, + useRef, useState, } from "react"; @@ -14,19 +15,39 @@ export interface ComponentProps { const Tabs: FunctionComponent = ( props: ComponentProps, ): ReactElement => { - const [currentTab, setCurrentTab] = useState(null); + const [currentTabName, setCurrentTabName] = useState(null); + const hasInitialized = useRef(false); + // Initialize current tab only once, or when the tab list names change useEffect(() => { - setCurrentTab(props.tabs.length > 0 ? (props.tabs[0] as Tab) : null); - }, [props.tabs]); + const tabNames: Array = props.tabs.map((t: Tab) => t.name); + + if (!hasInitialized.current && props.tabs.length > 0) { + hasInitialized.current = true; + setCurrentTabName(props.tabs[0]!.name); + return; + } + + // If current tab no longer exists in the list, reset to first + if (currentTabName && !tabNames.includes(currentTabName)) { + setCurrentTabName( + props.tabs.length > 0 ? props.tabs[0]!.name : null, + ); + } + }, [props.tabs.map((t: Tab) => t.name).join(",")]); + + // Find the current tab object by name + const currentTab: Tab | undefined = props.tabs.find( + (t: Tab) => t.name === currentTabName, + ); useEffect(() => { if (currentTab && props.onTabChange) { props.onTabChange(currentTab); } - }, [currentTab]); + }, [currentTabName]); - const tabPanelId: string = `tabpanel-${currentTab?.name || "default"}`; + const tabPanelId: string = `tabpanel-${currentTabName || "default"}`; return (
@@ -41,9 +62,9 @@ const Tabs: FunctionComponent = ( key={tab.name} tab={tab} onClick={() => { - setCurrentTab(tab); + setCurrentTabName(tab.name); }} - isSelected={tab === currentTab} + isSelected={tab.name === currentTabName} tabPanelId={tabPanelId} /> ); @@ -52,7 +73,7 @@ const Tabs: FunctionComponent = (
{currentTab && currentTab.children}