From 1b22268fdb99ba491555ccd202bcbf50d3855761 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Thu, 22 Jun 2023 10:04:02 +0100 Subject: [PATCH] Conditionally trigger fallback creation if no menus are found --- .../index.js | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 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 2234f1e1bbdee..099612151f774 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 @@ -2,7 +2,8 @@ * WordPress dependencies */ import { __, sprintf } from '@wordpress/i18n'; -import { useEntityRecords } from '@wordpress/core-data'; +import { useEntityRecords, store as coreStore } from '@wordpress/core-data'; +import { useSelect } from '@wordpress/data'; import { decodeEntities } from '@wordpress/html-entities'; import { @@ -41,15 +42,35 @@ function buildMenuLabel( title, id, status ) { } export default function SidebarNavigationScreenNavigationMenus() { - const { records: navigationMenus, isResolving: isLoading } = - useEntityRecords( - 'postType', - `wp_navigation`, - PRELOADED_NAVIGATION_MENUS_QUERY - ); + const { + records: navigationMenus, + isResolving: isResolvingNavigationMenus, + hasResolved: hasResolvedNavigationMenus, + } = useEntityRecords( + 'postType', + `wp_navigation`, + PRELOADED_NAVIGATION_MENUS_QUERY + ); + + const isLoading = + isResolvingNavigationMenus && ! hasResolvedNavigationMenus; + + const getNavigationFallbackId = useSelect( + ( select ) => select( coreStore ).getNavigationFallbackId + ); const firstNavigationMenu = navigationMenus?.[ 0 ]; + // If there is no navigation menu found + // then trigger fallback algorithm to create one. + if ( + ! firstNavigationMenu && + ! isResolvingNavigationMenus && + hasResolvedNavigationMenus + ) { + getNavigationFallbackId(); + } + const { handleSave, handleDelete, handleDuplicate } = useNavigationMenuHandlers();