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: Improve trigger for fallback navigation #66478

Merged
merged 1 commit into from
Oct 28, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ function buildMenuLabel( title, id, status ) {
);
}

// Save a boolean to prevent us creating a fallback more than once per session.
let hasCreatedFallback = false;

export default function SidebarNavigationScreenNavigationMenus( { backPath } ) {
const {
records: navigationMenus,
Expand All @@ -61,21 +58,22 @@ export default function SidebarNavigationScreenNavigationMenus( { backPath } ) {
isResolvingNavigationMenus && ! hasResolvedNavigationMenus;

const { getNavigationFallbackId } = unlock( useSelect( coreStore ) );
const isCreatingNavigationFallback = useSelect(
( select ) =>
select( coreStore ).isResolving( 'getNavigationFallbackId' ),
[]
);

const firstNavigationMenu = navigationMenus?.[ 0 ];

// Save a boolean to prevent us creating a fallback more than once per session.
if ( firstNavigationMenu ) {
hasCreatedFallback = true;
}

// If there is no navigation menu found
// then trigger fallback algorithm to create one.
if (
! firstNavigationMenu &&
! isResolvingNavigationMenus &&
hasResolvedNavigationMenus &&
! hasCreatedFallback
// Ensure a fallback navigation is created only once
! isCreatingNavigationFallback
) {
getNavigationFallbackId();
Copy link
Member

Choose a reason for hiding this comment

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

Calling a selector during the render seems odd. I know that useSelect isn't the best place to "fire and forget" pattern, but I can't think of a better alternative.

Copy link
Member Author

Choose a reason for hiding this comment

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

I admit that's weird, however, with the mechanics of selectors and resolvers, it does make sense to some degree. It's also documented why we're doing it, so it felt OK to leave as-is. This PR attempts to only fix the React Compiler error after all.

}
Expand Down
Loading