@@ -94,6 +94,20 @@ export interface PrimaryNavProps {
9494 isCollapsed : boolean ;
9595}
9696
97+ /** Indices for product-family accordions when the user has not saved `collapsedSideNavProductFamilies` yet. */
98+ const DEFAULT_COLLAPSED_SIDE_NAV_INDICES = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ;
99+
100+ function getActiveSideNavGroupIndex (
101+ groups : ProductFamilyLinkGroup < PrimaryLinkType [ ] > [ ] ,
102+ pathname : string
103+ ) {
104+ return groups . findIndex ( ( group ) => {
105+ const filteredLinks = group . links . filter ( ( link ) => ! link . hide ) ;
106+
107+ return filteredLinks . some ( ( link ) => linkIsActive ( pathname , link . to ) ) ;
108+ } ) ;
109+ }
110+
97111export const PrimaryNav = ( props : PrimaryNavProps ) => {
98112 const { closeMenu, desktopMenuToggle, isCollapsed } = props ;
99113 const navItemsRef = React . useRef < HTMLDivElement > ( null ) ;
@@ -131,20 +145,11 @@ export const PrimaryNav = (props: PrimaryNavProps) => {
131145
132146 const { isReserveIpEnabled } = useIsReserveIpEnabled ( ) ;
133147
134- const {
135- data : preferences ,
136- error : preferencesError ,
137- isLoading : preferencesLoading ,
138- } = usePreferences ( ) ;
148+ const { data : preferences } = usePreferences ( ) ;
139149
140150 const collapsedSideNavPreference =
141151 preferences ?. collapsedSideNavProductFamilies ;
142152
143- const collapsedAccordions = React . useMemo (
144- ( ) => collapsedSideNavPreference ?? [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] , // by default, we collapse all categories if no preference is set;
145- [ collapsedSideNavPreference ]
146- ) ;
147-
148153 const { mutateAsync : updatePreferences } = useMutatePreferences ( ) ;
149154
150155 const productFamilyLinkGroups : ProductFamilyLinkGroup < PrimaryLinkType [ ] > [ ] =
@@ -371,6 +376,30 @@ export const PrimaryNav = (props: PrimaryNavProps) => {
371376 ]
372377 ) ;
373378
379+ const collapsedAccordions = React . useMemo ( ( ) => {
380+ if ( preferences === undefined ) {
381+ return DEFAULT_COLLAPSED_SIDE_NAV_INDICES ;
382+ }
383+ if ( collapsedSideNavPreference !== undefined ) {
384+ return collapsedSideNavPreference ;
385+ }
386+ const activeGroupIndex = getActiveSideNavGroupIndex (
387+ productFamilyLinkGroups ,
388+ location . pathname
389+ ) ;
390+ if ( activeGroupIndex === - 1 ) {
391+ return DEFAULT_COLLAPSED_SIDE_NAV_INDICES ;
392+ }
393+ return DEFAULT_COLLAPSED_SIDE_NAV_INDICES . filter (
394+ ( idx ) => idx !== activeGroupIndex
395+ ) ;
396+ } , [
397+ collapsedSideNavPreference ,
398+ preferences ,
399+ productFamilyLinkGroups ,
400+ location . pathname ,
401+ ] ) ;
402+
374403 const accordionClicked = React . useCallback (
375404 ( index : number ) => {
376405 let updatedCollapsedAccordions : number [ ] ;
@@ -434,48 +463,6 @@ export const PrimaryNav = (props: PrimaryNavProps) => {
434463 } ;
435464 } , [ checkOverflow ] ) ;
436465
437- // This effect will only run if the collapsedSideNavPreference is not set
438- // When a user lands on a page and does not have any preference set,
439- // we want to expand the accordion that contains the active link for convenience and discoverability
440- React . useEffect ( ( ) => {
441- // Wait for preferences to load or if there's an error
442- if ( preferencesLoading || preferencesError ) {
443- return ;
444- }
445-
446- // Wait for preferences data to be available (not just the field, but the whole object)
447- if ( ! preferences ) {
448- return ;
449- }
450-
451- // If user has already set collapsedSideNavProductFamilies preference, don't override it
452- if ( collapsedSideNavPreference ) {
453- return ;
454- }
455-
456- // Find the index of the group containing the active link and expand it
457- const activeGroupIndex = productFamilyLinkGroups . findIndex ( ( group ) => {
458- const filteredLinks = group . links . filter ( ( link ) => ! link . hide ) ;
459-
460- return filteredLinks . some ( ( link ) =>
461- linkIsActive ( location . pathname , link . to )
462- ) ;
463- } ) ;
464-
465- if ( activeGroupIndex !== - 1 ) {
466- accordionClicked ( activeGroupIndex ) ;
467- }
468- } , [
469- accordionClicked ,
470- location . pathname ,
471- location . search ,
472- productFamilyLinkGroups ,
473- collapsedSideNavPreference ,
474- preferences ,
475- preferencesLoading ,
476- preferencesError ,
477- ] ) ;
478-
479466 let activeProductFamily = '' ;
480467
481468 return (
0 commit comments