Skip to content

Commit

Permalink
Ensure there is always a Navigation available in the browse mode side…
Browse files Browse the repository at this point in the history
…bar via fallback algorithm (#50321)

* Normalize menu used in sidebar with fallback algorithm

* Make fallback retrieval invalidate query cache for Navigation entities

* Conditionally trigger fallback creation if no menus are found

* Make code self documenting
  • Loading branch information
getdave authored Jun 23, 2023
1 parent b9a0efa commit acdd57b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
6 changes: 5 additions & 1 deletion packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,10 +585,14 @@ export const getNavigationFallbackId =
dispatch.receiveNavigationFallbackId( fallback?.id );

if ( record ) {
const invalidateNavigationQueries = true;

dispatch.receiveEntityRecords(
'postType',
'wp_navigation',
record
record,
undefined,
invalidateNavigationQueries
);

// Resolve to avoid further network requests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();

Expand Down

1 comment on commit acdd57b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in acdd57b.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5355127123
📝 Reported issues:

Please sign in to comment.