Skip to content

Commit

Permalink
Try to not use useSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
cbravobernal committed Sep 11, 2024
1 parent af79c2b commit 6ccce7c
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions packages/block-editor/src/hooks/block-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,46 +186,38 @@ export const BlockBindingsPanel = ( { name: blockName, metadata } ) => {
const bindableAttributes = getBindableAttributes( blockName );
const dropdownMenuProps = useToolsPanelDropdownMenuProps();

// `useSelect` is used purposely here to ensure `getFieldsList`
// is updated whenever there are attribute updates.
// `source.getFieldsList` may also call a selector via `registry.select`.
const { fieldsList, canUpdateBlockBindings } = useSelect(
const registeredSources =
unlock( blocksPrivateApis ).getBlockBindingsSources();

const fieldsList = {};
Object.entries( registeredSources ).forEach(
( [ sourceName, { getFieldsList, usesContext } ] ) => {
if ( getFieldsList ) {
const context = usesContext?.reduce( ( _context, key ) => {
_context[ key ] = blockContext[ key ];
return _context;
}, {} );

const sourceList = getFieldsList( { registry, context } );
if ( sourceList ) {
fieldsList[ sourceName ] = sourceList;
}
}
}
);

const { canUpdateBlockBindings } = useSelect(
( select ) => {
if ( ! bindableAttributes || bindableAttributes.length === 0 ) {
return {};
}
const _fieldsList = {};
const { getBlockBindingsSources } = unlock( blocksPrivateApis );
const registeredSources = getBlockBindingsSources();
Object.entries( registeredSources ).forEach(
( [ sourceName, { getFieldsList, usesContext } ] ) => {
if ( getFieldsList ) {
// Populate context.
const context = {};
if ( usesContext?.length ) {
for ( const key of usesContext ) {
context[ key ] = blockContext[ key ];
}
}
const sourceList = getFieldsList( {
registry,
context,
} );
// Only add source if the list is not empty.
if ( sourceList ) {
_fieldsList[ sourceName ] = { ...sourceList };
}
}
}
);
return {
fieldsList: _fieldsList,
canUpdateBlockBindings:
select( blockEditorStore ).getSettings()
.canUpdateBlockBindings,
};
},
[ blockContext, bindableAttributes, registry ]
[ bindableAttributes ]
);
// Return early if there are no bindable attributes.
if ( ! bindableAttributes || bindableAttributes.length === 0 ) {
Expand Down

0 comments on commit 6ccce7c

Please sign in to comment.