Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation: Simplify the method for finding the fallback menu #48916

Merged
merged 2 commits into from
Mar 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ]
);
getdave marked this conversation as resolved.
Show resolved Hide resolved
// 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