Skip to content

Commit

Permalink
Navigation: Simplify the method for finding the fallback menu (#48916)
Browse files Browse the repository at this point in the history
* Navigation: Simplify the method for finding the fallback menu

* Update the comment
  • Loading branch information
Mamaduka authored Mar 8, 2023
1 parent c3e7db8 commit 1fc8018
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
useEffect,
useRef,
Platform,
useMemo,
} from '@wordpress/element';
import {
InspectorControls,
Expand Down Expand Up @@ -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 } ) => {
Expand All @@ -237,7 +229,7 @@ function Navigation( {
hasUncontrolledInnerBlocks ||
isCreatingNavigationMenu ||
ref ||
! fallbackNavigationMenus?.length
! fallbackNavigationMenuId
) {
return;
}
Expand All @@ -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,
] );
Expand All @@ -277,7 +269,7 @@ function Navigation( {
! hasResolvedClassicMenus ||
! hasResolvedNavigationMenus ||
isConvertingClassicMenu ||
fallbackNavigationMenus?.length > 0 ||
fallbackNavigationMenuId ||
hasUnsavedBlocks ||
! classicMenus?.length
) {
Expand Down Expand Up @@ -317,7 +309,7 @@ function Navigation( {
classicMenus,
convertClassicMenu,
createNavigationMenu,
fallbackNavigationMenus?.length,
fallbackNavigationMenuId,
isConvertingClassicMenu,
ref,
] );
Expand Down

0 comments on commit 1fc8018

Please sign in to comment.