From 1fc801809d752f587d204444f98bf2321d17d889 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 8 Mar 2023 14:52:04 +0400 Subject: [PATCH] Navigation: Simplify the method for finding the fallback menu (#48916) * Navigation: Simplify the method for finding the fallback menu * Update the comment --- .../src/navigation/edit/index.js | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/packages/block-library/src/navigation/edit/index.js b/packages/block-library/src/navigation/edit/index.js index 9d61c2339d73c9..169d60ef0cc505 100644 --- a/packages/block-library/src/navigation/edit/index.js +++ b/packages/block-library/src/navigation/edit/index.js @@ -12,7 +12,6 @@ import { useEffect, useRef, Platform, - useMemo, } from '@wordpress/element'; import { InspectorControls, @@ -201,18 +200,11 @@ function Navigation( { const isConvertingClassicMenu = classicMenuConversionStatus === CLASSIC_MENU_CONVERSION_PENDING; - // Only autofallback to published menus. - const fallbackNavigationMenus = useMemo( - () => - navigationMenus - ?.filter( ( menu ) => menu.status === 'publish' ) - ?.sort( ( menuA, menuB ) => { - const menuADate = new Date( menuA.date ); - const menuBDate = new Date( menuB.date ); - return menuADate.getTime() < menuBDate.getTime(); - } ), - [ navigationMenus ] - ); + // Only auto-fallback to the latest published menu. + // The REST API already returns items sorted by publishing date. + const fallbackNavigationMenuId = navigationMenus?.find( + ( menu ) => menu.status === 'publish' + )?.id; const handleUpdateMenu = useCallback( ( menuId, options = { focusNavigationBlock: false } ) => { @@ -237,7 +229,7 @@ function Navigation( { hasUncontrolledInnerBlocks || isCreatingNavigationMenu || ref || - ! fallbackNavigationMenus?.length + ! fallbackNavigationMenuId ) { return; } @@ -250,12 +242,12 @@ function Navigation( { * nor to be undoable, hence why it is marked as non persistent */ __unstableMarkNextChangeAsNotPersistent(); - setRef( fallbackNavigationMenus[ 0 ].id ); + setRef( fallbackNavigationMenuId ); }, [ ref, setRef, isCreatingNavigationMenu, - fallbackNavigationMenus, + fallbackNavigationMenuId, hasUncontrolledInnerBlocks, __unstableMarkNextChangeAsNotPersistent, ] ); @@ -277,7 +269,7 @@ function Navigation( { ! hasResolvedClassicMenus || ! hasResolvedNavigationMenus || isConvertingClassicMenu || - fallbackNavigationMenus?.length > 0 || + fallbackNavigationMenuId || hasUnsavedBlocks || ! classicMenus?.length ) { @@ -317,7 +309,7 @@ function Navigation( { classicMenus, convertClassicMenu, createNavigationMenu, - fallbackNavigationMenus?.length, + fallbackNavigationMenuId, isConvertingClassicMenu, ref, ] );