Skip to content

Commit

Permalink
Block tree reducer: avoid nested update for insertUsage
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Mar 7, 2024
1 parent 2071a26 commit 4a6849b
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1697,36 +1697,42 @@ export function settings( state = SETTINGS_DEFAULTS, action ) {
export function preferences( state = PREFERENCES_DEFAULTS, action ) {
switch ( action.type ) {
case 'INSERT_BLOCKS':
case 'REPLACE_BLOCKS':
return action.blocks.reduce( ( prevState, block ) => {
const { attributes, name: blockName } = block;
let id = blockName;
// If a block variation match is found change the name to be the same with the
// one that is used for block variations in the Inserter (`getItemFromVariation`).
const match = select( blocksStore ).getActiveBlockVariation(
blockName,
attributes
);
if ( match?.name ) {
id += '/' + match.name;
}
if ( blockName === 'core/block' ) {
id += '/' + attributes.ref;
}
case 'REPLACE_BLOCKS': {
const nextInsertUsage = action.blocks.reduce(
( prevUsage, block ) => {
const { attributes, name: blockName } = block;
let id = blockName;
// If a block variation match is found change the name to be the same with the
// one that is used for block variations in the Inserter (`getItemFromVariation`).
const match = select( blocksStore ).getActiveBlockVariation(
blockName,
attributes
);
if ( match?.name ) {
id += '/' + match.name;
}
if ( blockName === 'core/block' ) {
id += '/' + attributes.ref;
}

return {
...prevState,
insertUsage: {
...prevState.insertUsage,
return {
...prevUsage,
[ id ]: {
time: action.time,
count: prevState.insertUsage[ id ]
? prevState.insertUsage[ id ].count + 1
count: prevUsage[ id ]
? prevUsage[ id ].count + 1
: 1,
},
},
};
}, state );
};
},
state.insertUsage
);

return {
...state,
insertUsage: nextInsertUsage,
};
}
}

return state;
Expand Down

0 comments on commit 4a6849b

Please sign in to comment.