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

Shuffle: Don't call '__experimentalGetAllowedPatterns' for every block #64736

Merged
merged 2 commits into from
Aug 23, 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
16 changes: 9 additions & 7 deletions packages/block-editor/src/components/block-toolbar/shuffle.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ export default function Shuffle( { clientId, as = Container } ) {
const _categories = attributes?.metadata?.categories || EMPTY_ARRAY;
const _patternName = attributes?.metadata?.patternName;
const rootBlock = getBlockRootClientId( clientId );
const _patterns = __experimentalGetAllowedPatterns( rootBlock );

// Calling `__experimentalGetAllowedPatterns` is expensive.
// Checking if the block can be shuffled prevents unnecessary selector calls.
// See: https://github.com/WordPress/gutenberg/pull/64736.
const _patterns =
_categories.length > 0
? __experimentalGetAllowedPatterns( rootBlock )
Copy link
Member

Choose a reason for hiding this comment

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

It's worth adding a comment why we're doing this, either here or wherever we consume the patterns.

A comment can help in the future if someone is trying to use patterns for something not related to the categories, and they might be confused as to why patterns is empty sometimes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea. I added to the comment.

We should also try to improve the __experimentalGetAllowedPatterns performance.

: EMPTY_ARRAY;
return {
categories: _categories,
patterns: _patterns,
Expand All @@ -45,12 +52,7 @@ export default function Shuffle( { clientId, as = Container } ) {
);
const { replaceBlocks } = useDispatch( blockEditorStore );
const sameCategoryPatternsWithSingleWrapper = useMemo( () => {
if (
! categories ||
categories.length === 0 ||
! patterns ||
patterns.length === 0
) {
if ( categories.length === 0 || ! patterns || patterns.length === 0 ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

The categories will always be an array.

return EMPTY_ARRAY;
}
return patterns.filter( ( pattern ) => {
Expand Down
Loading