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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.
There was a problem hiding this comment.
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
ntsekouras
left a comment
There was a problem hiding this comment.
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-editorpackage 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 foruncategorizedcategory to use.someinstead of.filterand.find. We are interested only in yes/no boolean results.usePatternCategories: avoiduseCallbackby extractinghasRegisteredCategoryas a standalone top-level function. TheallCategoriesparameter is already a dependency of theuseEffecthook that calls the function.InserterMenu: calculate thehasReusableBlocksboolean value directly inuseSelecthook, instead of returning aninserterItemsarray. Should lead to fewer rerenders, and avoids an extrauseMemohook.InserterTabs: calculate thetabsarray without.push, and remove theuseMemo. After refactoring in #56918, thetabsvalue is not used as a prop and doesn't need to be memoized.