@@ -184,10 +184,12 @@ const useTabs = (apiStoreMock: ReturnType<typeof createTabsStore>) => {
184184
185185 useEffect ( ( ) => {
186186 const unsubscribe = subscribe ( dispatch ) ;
187+ // Sync state from current store (needed when store instance changed, e.g. flags)
188+ dispatch ( ) ;
187189 return ( ) => {
188190 unsubscribe ( ) ;
189191 } ;
190- } , [ ] ) ;
192+ } , [ apiStoreMock ] ) ;
191193
192194 return {
193195 tabs,
@@ -253,17 +255,30 @@ const HelpPanelCustomTabs = React.forwardRef<HelpPanelCustomTabsRef>(
253255 const intl = useIntl ( ) ;
254256 const chrome = useChrome ( ) ;
255257 const vaFlag = useFlag ( 'platform.chrome.help-panel_chatbot' ) ;
258+ const searchFlag = useFlag ( 'platform.chrome.help-panel_search' ) ;
256259
257260 const baseTabs = useMemo ( ( ) => createBaseTabs ( vaFlag ) , [ vaFlag ] ) ;
258- const apiStoreMock = useMemo ( ( ) => createTabsStore ( baseTabs ) , [ baseTabs ] ) ;
259-
260- // Find the Learn tab index (it might be 0 or 1 depending on VA flag)
261- const learnTabIndex = baseTabs . findIndex (
262- ( tab ) => tab . tabType === TabType . learn
261+ // Initialize store with find-help tab already having the correct sub-tab (Search when flag on, else Learn)
262+ // so content is correct on first paint and tests don't depend on a follow-up effect.
263+ const initialTabs = useMemo (
264+ ( ) =>
265+ baseTabs . map ( ( tab ) =>
266+ tab . id === 'find-help' && searchFlag
267+ ? { ...tab , tabType : TabType . search }
268+ : tab
269+ ) ,
270+ [ baseTabs , searchFlag ]
271+ ) ;
272+ const apiStoreMock = useMemo (
273+ ( ) => createTabsStore ( initialTabs ) ,
274+ [ initialTabs ]
263275 ) ;
264- const [ activeTab , setActiveTab ] = useState < TabDefinition > (
265- baseTabs [ learnTabIndex ]
266- ) ; // Default to 'Find help' tab (Learn)
276+
277+ // Default to 'Find help' tab; default sub-tab is Search when search flag is enabled, otherwise Learn
278+ const defaultFindHelpTab =
279+ initialTabs . find ( ( t ) => t . id === 'find-help' ) ?? initialTabs [ 0 ] ;
280+ const [ activeTab , setActiveTab ] =
281+ useState < TabDefinition > ( defaultFindHelpTab ) ;
267282
268283 const [ newActionTitle , setNewActionTitle ] = useState < string | undefined > (
269284 undefined
@@ -524,13 +539,20 @@ const HelpPanelCustomTabs = React.forwardRef<HelpPanelCustomTabsRef>(
524539 ! activeTab . closeable &&
525540 ! baseTabs . find ( ( tab ) => tab . id === activeTab . id )
526541 ) {
527- // Current active tab is no longer available, default to Learn tab
528- const learnTab = baseTabs . find ( ( tab ) => tab . tabType === TabType . learn ) ;
529- if ( learnTab ) {
530- setActiveTab ( learnTab ) ;
542+ // Current active tab is no longer available, default to Search tab when available, otherwise Learn tab
543+ const findHelpTabFallback = baseTabs . find (
544+ ( tab ) => tab . tabType === TabType . learn
545+ ) ;
546+ if ( findHelpTabFallback ) {
547+ const fallbackTab = {
548+ ...findHelpTabFallback ,
549+ tabType : searchFlag ? TabType . search : TabType . learn ,
550+ } ;
551+ updateTab ( fallbackTab ) ;
552+ setActiveTab ( fallbackTab ) ;
531553 }
532554 }
533- } , [ baseTabs , activeTab . id , activeTab . closeable ] ) ;
555+ } , [ baseTabs , activeTab . id , activeTab . closeable , searchFlag , updateTab ] ) ;
534556
535557 useEffect ( ( ) => {
536558 // Ensure the Add tab button has a stable OUIA id
0 commit comments