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

Make the navigation block more internally consistent #37190

Closed
adamziel opened this issue Dec 7, 2021 · 9 comments
Closed

Make the navigation block more internally consistent #37190

adamziel opened this issue Dec 7, 2021 · 9 comments
Labels
[Block] Navigation Affects the Navigation Block [Type] Code Quality Issues or PRs that relate to code quality

Comments

@adamziel
Copy link
Contributor

adamziel commented Dec 7, 2021

There's quite some code duplication in the navigation block code, leading to inconsistent behaviors when seemingly the same feature is used using a different control.

Multiple code paths for creating new menus

There are many ways a user may create a new, empty menu, and they are handled by different code paths:

const onCreateEmptyMenu = () => {
onFinishMenuCreation( [] );
};

const onFinishMenuCreation = async (
blocks,
navigationMenuTitle = null
) => {
const navigationMenu = await createNavigationMenu(
navigationMenuTitle,
blocks
);
onFinish( navigationMenu, blocks );
};

const startWithEmptyMenu = useCallback( () => {
if ( navigationArea ) {
setAreaMenu( 0 );
}
setAttributes( {
ref: undefined,
} );
setIsPlaceholderShown( true );
}, [ clientId ] );

CleanShot 2021-12-07 at 17 05 09@2x

I would like to have a single code path that is executed for each of these buttons.

Multiple data sources for dropdown menus

There are two ways of accessing a dropdown menus, and they output inconsistent items because each is handled by a different code path:

menus

Multiple code paths for menu switching

They are more consistent with each other, but still slightly different.

Block toolbar:

<NavigationMenuSelector
onSelect={ ( { id } ) => {
setRef( id );
onClose();
} }
onCreateNew={ startWithEmptyMenu }

Placeholder:

setIsPlaceholderShown( false );
if ( post ) {
setRef( post.id );
}
selectBlock( clientId );

Unintuitive logic

( ! isEntityAvailable && ! isPlaceholderShown && (
<PlaceholderPreview isLoading />
) ) }

I found it confusing that we're showing a placeholder when ! isPlaceholderShown. It would be nice to have a <LoadingState /> component.

cc @noisysocks @talldan @draganescu @tellthemachines @getdave

@adamziel adamziel added the [Block] Navigation Affects the Navigation Block label Dec 7, 2021
@adamziel adamziel changed the title Reduce code duplication in the navigation block Make the navigation block more internally consistent Dec 7, 2021
@talldan
Copy link
Contributor

talldan commented Dec 8, 2021

The 'Navigation menu has been deleted or is unavailable' message is something I'd already deleted once (#36507).

Not sure why it was brought back again.

@adamziel
Copy link
Contributor Author

adamziel commented Dec 8, 2021

I took that screenshot before you removed it 😅

@noisysocks
Copy link
Member

Is this a bug or a code quality task? Wondering if we can punt it from the 5.9 board. Help me out by adding a [Type] label 😄

@adamziel adamziel added the [Type] Code Quality Issues or PRs that relate to code quality label Dec 15, 2021
@adamziel
Copy link
Contributor Author

@noisysocks I would say it's both :D but if I have to choose, it's more of a Code Quality thing. It should be fine to fix it in 6.0. I intuitively sense a larger refactor would be needed here, which means new bugs, which isn't great during the Beta period.

@Mamaduka
Copy link
Member

Mamaduka commented Jan 5, 2022

I'm removing this from the 5.9 board but will add it to the Navigation one.

@getdave
Copy link
Contributor

getdave commented Jan 10, 2022

@adamziel Let's work on these items. The complexity of the block code has increased a lot in the run up to 5.9 and we need to refactor now to save ourselves pain later.

Let's break this up into tasks:

  • Create loading component for Nav block.
  • Consolidate code paths for Select menu dropdowns
  • Consolidate code paths for code paths for creating new menus

Let's make Issues for tackling each one.

@talldan
Copy link
Contributor

talldan commented Jan 24, 2022

I took that screenshot before you removed it 😅

It's definitely back again 😓 :

// Show a warning if the selected menu is no longer available.
// TODO - the user should be able to select a new one?
if ( ref && isNavigationMenuMissing ) {
return (
<div { ...blockProps }>
<Warning>
{ __(
'Navigation menu has been deleted or is unavailable. '
) }
<Button onClick={ startWithEmptyMenu } variant="link">
{ __( 'Create a new menu?' ) }
</Button>
</Warning>
</div>
);
}

Must have accidentally been brought back as the result of a bad conflict resolution.

@talldan
Copy link
Contributor

talldan commented Jan 24, 2022

While we're talking about code quality, something else I noticed today is that useNavigationEntities does a lot. As does useNavigationMenu now. I think we could divide them into separate hooks:

  • usePages
  • useClassicMenus
  • useClassicMenuItems
  • useNavigationMenus
  • useNavigationMenuPermissions

One reason is that Pages don't need to be requested all the time, only when the placeholder shows.

@getdave
Copy link
Contributor

getdave commented Mar 17, 2023

This has become a bit stale. Going to close it out.

@getdave getdave closed this as completed Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Navigation Affects the Navigation Block [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

No branches or pull requests

5 participants