Skip to content

Commit

Permalink
ListViewBranch: refactor to make flattenBlockTree function a reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Aug 16, 2022
1 parent 587329e commit 8ac24f7
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions packages/block-editor/src/components/list-view/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,6 @@ const countReducer =
return count + 1;
};

function getIdsTreeFlat( blocks ) {
return blocks.reduce( ( result, { clientId, innerBlocks } ) => {
return [
...result,
{ clientId, innerBlocks: [] },
...getIdsTreeFlat( innerBlocks ),
];
}, [] );
}

function ListViewBranch( props ) {
const {
parentId,
Expand All @@ -110,9 +100,16 @@ function ListViewBranch( props ) {
},
[ parentId ]
);
const flattenBlockTree = ( result, { clientId, innerBlocks } ) => {
return [
...result,
{ clientId, innerBlocks: [] },
...innerBlocks.reduce( flattenBlockTree, [] ),
];
};
const filteredBlocks = useMemo( () => {
if ( isContentLocked ) {
return getIdsTreeFlat( blocks ).filter( Boolean );
return blocks.reduce( flattenBlockTree, [] ).filter( Boolean );
}
return blocks.filter( Boolean );
}, [ isContentLocked, blocks ] );
Expand Down

0 comments on commit 8ac24f7

Please sign in to comment.