From 75d86e604542f9530f763eeadbca8d28000a0379 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 8 Mar 2023 12:57:10 +0400 Subject: [PATCH 1/2] Navigation: Simplify the method for finding the fallback menu --- .../src/navigation/edit/index.js | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/packages/block-library/src/navigation/edit/index.js b/packages/block-library/src/navigation/edit/index.js index 9d61c2339d73c9..bab125e7229ff8 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, @@ -202,17 +201,9 @@ function Navigation( { 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 ] - ); + const fallbackNavigationMenuId = navigationMenus?.find( + ( menu ) => menu.status === 'publish' + )?.id; const handleUpdateMenu = useCallback( ( menuId, options = { focusNavigationBlock: false } ) => { @@ -237,7 +228,7 @@ function Navigation( { hasUncontrolledInnerBlocks || isCreatingNavigationMenu || ref || - ! fallbackNavigationMenus?.length + ! fallbackNavigationMenuId ) { return; } @@ -250,12 +241,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 +268,7 @@ function Navigation( { ! hasResolvedClassicMenus || ! hasResolvedNavigationMenus || isConvertingClassicMenu || - fallbackNavigationMenus?.length > 0 || + fallbackNavigationMenuId || hasUnsavedBlocks || ! classicMenus?.length ) { @@ -317,7 +308,7 @@ function Navigation( { classicMenus, convertClassicMenu, createNavigationMenu, - fallbackNavigationMenus?.length, + fallbackNavigationMenuId, isConvertingClassicMenu, ref, ] ); From 15372a930de2e2c93738f05ac56908a6bf973386 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 8 Mar 2023 13:23:43 +0400 Subject: [PATCH 2/2] Update the comment --- packages/block-library/src/navigation/edit/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/navigation/edit/index.js b/packages/block-library/src/navigation/edit/index.js index bab125e7229ff8..169d60ef0cc505 100644 --- a/packages/block-library/src/navigation/edit/index.js +++ b/packages/block-library/src/navigation/edit/index.js @@ -200,7 +200,8 @@ function Navigation( { const isConvertingClassicMenu = classicMenuConversionStatus === CLASSIC_MENU_CONVERSION_PENDING; - // Only autofallback to published menus. + // 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;