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

Inserter: Return the same items when the state and parameters don't change #62263

Merged
merged 2 commits into from
Jun 4, 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
parse,
} from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import { useCallback } from '@wordpress/element';
import { useCallback, useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -25,13 +25,18 @@ import { withRootClientIdOptionKey } from '../../../store/utils';
* @return {Array} Returns the block types state. (block types, categories, collections, onSelect handler)
*/
const useBlockTypesState = ( rootClientId, onInsert, isQuick ) => {
const options = useMemo(
() => ( { [ withRootClientIdOptionKey ]: ! isQuick } ),
[ isQuick ]
);
const [ items ] = useSelect(
( select ) => [
select( blockEditorStore ).getInserterItems( rootClientId, {
[ withRootClientIdOptionKey ]: ! isQuick,
} ),
select( blockEditorStore ).getInserterItems(
rootClientId,
options
),
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we account for this in getInserterItems? Also the default there is an empty object on every call, so we should change that too?

Copy link
Member Author

Choose a reason for hiding this comment

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

Shouldn't we account for this in getInserterItems?

That would be ideal, but we need to figure out how. Functions have no control over the arguments they receive.

Also the default there is an empty object on every call, so we should change that too?

Yes. We should either pass undefined or an EMTPY_OBJECT.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, this is a good fix for now. I wonder if we should just create another private getInserterItems selector and avoid the options altogether. Actually this would also allow us to remove all the reusable blocks logic. We're adding these blocks, just to filter them back out in the UI 😄

],
[ rootClientId, isQuick ]
[ rootClientId, options ]
);

const [ categories, collections ] = useSelect( ( select ) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ const EMPTY_ARRAY = [];
*/
const EMPTY_SET = new Set();

const EMPTY_OBJECT = {};
Copy link
Member Author

Choose a reason for hiding this comment

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

I've omitted the comment here. The other ones are a bit outdated and we should probably revise them together.


/**
* Returns a block's name given its client ID, or null if no block exists with
* the client ID.
Expand Down Expand Up @@ -1996,7 +1998,7 @@ const buildBlockTypeItem =
*/
export const getInserterItems = createRegistrySelector( ( select ) =>
createSelector(
( state, rootClientId = null, options = {} ) => {
( state, rootClientId = null, options = EMPTY_OBJECT ) => {
Copy link
Member

Choose a reason for hiding this comment

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

Actually I guess this doesn't matter because createSelector is not aware of the default, it would just not be defined. Sorry my mistake :D

const buildReusableBlockInserterItem = ( reusableBlock ) => {
const icon = ! reusableBlock.wp_pattern_sync_status
? {
Expand Down
Loading