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

Avoid fetching ALL reusable blocks (user patterns) on post/site editor load #58239

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion packages/block-editor/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ import { usesContextKey } from './components/rich-text/format-edit';
import { ExperimentalBlockCanvas } from './components/block-canvas';
import { getDuotoneFilter } from './components/duotone/utils';
import { useFlashEditableBlocks } from './components/use-flash-editable-blocks';
import { selectBlockPatternsKey } from './store/private-keys';
import {
selectBlockPatternsKey,
reusableBlocksSelectKey,
} from './store/private-keys';
import { requiresWrapperOnCopy } from './components/writing-flow/utils';
import { PrivateRichText } from './components/rich-text/';

Expand Down Expand Up @@ -70,4 +73,5 @@ lock( privateApis, {
selectBlockPatternsKey,
requiresWrapperOnCopy,
PrivateRichText,
reusableBlocksSelectKey,
} );
1 change: 1 addition & 0 deletions packages/block-editor/src/store/private-keys.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const selectBlockPatternsKey = Symbol( 'selectBlockPatternsKey' );
export const reusableBlocksSelectKey = Symbol( 'reusableBlocksSelect' );
57 changes: 36 additions & 21 deletions packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import {
import { INSERTER_PATTERN_TYPES } from '../components/inserter/block-patterns-tab/utils';
import { STORE_NAME } from './constants';
import { unlock } from '../lock-unlock';
import { selectBlockPatternsKey } from './private-keys';
import {
selectBlockPatternsKey,
reusableBlocksSelectKey,
} from './private-keys';

export { getBlockSettings } from './get-block-settings';

Expand Down Expand Up @@ -300,26 +303,27 @@ export const getAllPatterns = createRegistrySelector( ( select ) =>
__experimentalUserPatternCategories = [],
__experimentalReusableBlocks = [],
} = state.settings;
const userPatterns = ( __experimentalReusableBlocks ?? [] ).map(
( userPattern ) => {
return {
name: `core/block/${ userPattern.id }`,
id: userPattern.id,
type: INSERTER_PATTERN_TYPES.user,
title: userPattern.title.raw,
categories: userPattern.wp_pattern_category.map(
( catId ) => {
const category = (
__experimentalUserPatternCategories ?? []
).find( ( { id } ) => id === catId );
return category ? category.slug : catId;
}
),
content: userPattern.content.raw,
syncStatus: userPattern.wp_pattern_sync_status,
};
}
);
const reusableBlocksSelect = state.settings[ reusableBlocksSelectKey ];
const userPatterns = (
reusableBlocksSelect
? reusableBlocksSelect( select )
: __experimentalReusableBlocks ?? []
Copy link
Member

Choose a reason for hiding this comment

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

reusableBlockSelect( select ) can return null if the underlying REST endpoint is still fetching. The .map that follows will crash on it.

The ?? [] bit defaults only the __experimentalReusableBlocks value. a ? b : c ?? d groups like a ? b : ( c ?? d ).

Also, it's unfortunate that the setting for patterns is a "fetch" function and the setting for reusable blocks is a "select" function. Is there a way to make the API consistent?

Copy link
Member Author

Choose a reason for hiding this comment

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

Also, it's unfortunate that the setting for patterns is a "fetch" function and the setting for reusable blocks is a "select" function. Is there a way to make the API consistent?

The problem is that reusable blocks is a post type that fits with entity records, while patterns is something provided by the theme. I'm not sure what's best here. I guess we could directly fetch the reusable blocks without the entity records API. Or we move patterns back to the core-data store and leave it as a select function. The latter is a bit more restrictive, since you can't access the raw state to use in memoized selector dependencies (you have to call the selector which will resolve it).

Copy link
Member Author

Choose a reason for hiding this comment

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

Btw, this is why I made it a private setting, so we have room to change it later. Maybe we should make the fetch patterns function also private.

).map( ( userPattern ) => {
return {
Copy link
Member

Choose a reason for hiding this comment

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

For clarity, could this function be extracted as function reusableBlockToPattern( block )? The const userPatterns = ... expression is becoming quite big 🙂

name: `core/block/${ userPattern.id }`,
id: userPattern.id,
type: INSERTER_PATTERN_TYPES.user,
title: userPattern.title.raw,
categories: userPattern.wp_pattern_category.map( ( catId ) => {
const category = (
__experimentalUserPatternCategories ?? []
).find( ( { id } ) => id === catId );
return category ? category.slug : catId;
} ),
content: userPattern.content.raw,
syncStatus: userPattern.wp_pattern_sync_status,
};
} );
return [
...userPatterns,
...__experimentalBlockPatterns,
Expand All @@ -331,6 +335,17 @@ export const getAllPatterns = createRegistrySelector( ( select ) =>
}, getAllPatternsDependants( select ) )
);

const EMPTY_ARRAY = [];

export const getReusableBlocks = createRegistrySelector(
( select ) => ( state ) => {
const reusableBlocksSelect = state.settings[ reusableBlocksSelectKey ];
return reusableBlocksSelect
? reusableBlocksSelect( select )
: state.settings.__experimentalReusableBlocks ?? EMPTY_ARRAY;
}
);

/**
* Returns the element of the last element that had focus when focus left the editor canvas.
*
Expand Down
Loading
Loading