From 3da4c5204980fc78166d48a2a57c9f57e6ce6bdd Mon Sep 17 00:00:00 2001 From: Marin Atanasov <8436925+tyxla@users.noreply.github.com> Date: Mon, 28 Oct 2024 18:53:52 +0200 Subject: [PATCH] Navigation: Improve trigger for fallback navigation (#66478) Co-authored-by: tyxla Co-authored-by: Mamaduka Co-authored-by: draganescu --- .../index.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/index.js b/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/index.js index 18e6a4210ee1b..ece549f57378b 100644 --- a/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/index.js +++ b/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/index.js @@ -43,9 +43,6 @@ function buildMenuLabel( title, id, status ) { ); } -// Save a boolean to prevent us creating a fallback more than once per session. -let hasCreatedFallback = false; - export default function SidebarNavigationScreenNavigationMenus( { backPath } ) { const { records: navigationMenus, @@ -61,21 +58,22 @@ export default function SidebarNavigationScreenNavigationMenus( { backPath } ) { isResolvingNavigationMenus && ! hasResolvedNavigationMenus; const { getNavigationFallbackId } = unlock( useSelect( coreStore ) ); + const isCreatingNavigationFallback = useSelect( + ( select ) => + select( coreStore ).isResolving( 'getNavigationFallbackId' ), + [] + ); const firstNavigationMenu = navigationMenus?.[ 0 ]; - // Save a boolean to prevent us creating a fallback more than once per session. - if ( firstNavigationMenu ) { - hasCreatedFallback = true; - } - // If there is no navigation menu found // then trigger fallback algorithm to create one. if ( ! firstNavigationMenu && ! isResolvingNavigationMenus && hasResolvedNavigationMenus && - ! hasCreatedFallback + // Ensure a fallback navigation is created only once + ! isCreatingNavigationFallback ) { getNavigationFallbackId(); }