Skip to content

Commit

Permalink
Remove duplicate template areas from the site view sidebar (#54942)
Browse files Browse the repository at this point in the history
* Remvoe duplicate template areas from the site view sidebar

* use a set instead of an array for available template parts

---------

Co-authored-by: Andrei Draganescu <andrei.draganescu@automattic.com>
  • Loading branch information
scruffian and draganescu authored Oct 16, 2023
1 parent 2155576 commit 58064cc
Showing 1 changed file with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,28 @@ export default function HomeTemplateDetails() {
* which contains the template icon and fallback labels
*/
const templateAreas = useMemo( () => {
// Keep track of template part IDs that have already been added to the array.
const templatePartIds = new Set();
const filterOutDuplicateTemplateParts = ( currentTemplatePart ) => {
// If the template part has already been added to the array, skip it.
if ( templatePartIds.has( currentTemplatePart.templatePart.id ) ) {
return;
}
// Add to the array of template part IDs.
templatePartIds.add( currentTemplatePart.templatePart.id );
return currentTemplatePart;
};

return currentTemplateParts.length && templatePartAreas
? currentTemplateParts.map( ( { templatePart, block } ) => ( {
...templatePartAreas?.find(
( { area } ) => area === templatePart?.area
),
...templatePart,
clientId: block.clientId,
} ) )
? currentTemplateParts
.filter( filterOutDuplicateTemplateParts )
.map( ( { templatePart, block } ) => ( {
...templatePartAreas?.find(
( { area } ) => area === templatePart?.area
),
...templatePart,
clientId: block.clientId,
} ) )
: [];
}, [ currentTemplateParts, templatePartAreas ] );

Expand Down

0 comments on commit 58064cc

Please sign in to comment.