-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Block Editor: several little refactors #57107
Conversation
Size Change: -83 B (0%) Total Size: 1.71 MB
ℹ️ View Unchanged
|
Flaky tests detected in 7b5408f. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7245101416
|
c839998
to
ecc7933
Compare
We are not displaying the reusable blocks tab in the block editor anymore so I have removed that check. |
( availableCategory ) => | ||
availableCategory.name === cat | ||
) | ||
) ?? []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this change it is now showing all patterns under the Uncategorized
category for me, but I ran out of time to work out why. I can do some more testing tomorrow, and have a look to see how we could make this code path a bit clearer as to what is happening if you don't have time to look.
To test if you add a new synced pattern with a category it will not display under uncategorized in trunk, but will on this branch.
availableCategory.name === cat | ||
) | ||
) ?? []; | ||
if ( pattern.categories ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might relate to the comment from @glendaviesnz.
Should it be if ( pattern.categories?.length ) {
?
Having said that, when there are any pattern.categories
this will return early, so the pattern.categories.some
code below would never be called when the array has values. Maybe it was supposed to be if ( ! pattern.categories?.length ) {
.
It wasn't something modified in this PR, but thought I'd mention ... I think the code could be more readable if the if
statement on line 81 is flipped to if ( category.name === 'uncategorized' ) {
. I'd be happy to follow up with that change if it doesn't happen in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that the condition needs flipping, thanks for noticing this 🙂 It should be if ( ! pattern.categorires ) return true
. The idea is to provide a shortcut for the case where pattern.categories
field is not there. In the previous code the categories?.filter
expression evaluated to undefined
, then it was defaulted to []
, and then the .length === 0
comparison of this array leads to true
return value. A four step process shortened to one step.
I also implemented the suggestion for if ( name = 'uncategorized' )
. All the special cases are now evaluated inside if
blocks, and the general case is handled last.
|
||
return availablePatternCategories.length === 0; | ||
return ! pattern.categories.some( ( catName ) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code before seemed to be wary about the type of categories
.
return ! pattern.categories.some( ( catName ) => | |
return ! pattern.categories?.some( ( catName ) => |
Nice catch, thank you! I noticed that |
c21e2bb
to
7b5408f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thank you!
* Block Editor: several little refactors * Remove check for reusable blocks as this tab is not shown in the block editor since this were merged with patterns. * Flip the no-categories condition for uncategorized * Tidy up the uncategorized condition --------- Co-authored-by: Glen Davies <glen.davies@automattic.com>
This PR contains several little refactors in the
block-editor
package that are a spinoff from block lazy loading in #51778. They are not related to each other, but each of them is quite simple.PatternCategoryPreviews
: rewrite the condition foruncategorized
category to use.some
instead of.filter
and.find
. We are interested only in yes/no boolean results.usePatternCategories
: avoiduseCallback
by extractinghasRegisteredCategory
as a standalone top-level function. TheallCategories
parameter is already a dependency of theuseEffect
hook that calls the function.InserterMenu
: calculate thehasReusableBlocks
boolean value directly inuseSelect
hook, instead of returning aninserterItems
array. Should lead to fewer rerenders, and avoids an extrauseMemo
hook.InserterTabs
: calculate thetabs
array without.push
, and remove theuseMemo
. After refactoring in #56918, thetabs
value is not used as a prop and doesn't need to be memoized.