Skip to content

Commit

Permalink
Refactor: usePatternsCategories: simplify category sorting (#47843)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsf committed Feb 8, 2023
1 parent 9590cf4 commit c4e0a21
Showing 1 changed file with 9 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import BlockPatternList from '../block-patterns-list';
import PatternsExplorerModal from './block-patterns-explorer/explorer';
import MobileTabNavigation from './mobile-tab-navigation';

// Preffered order of pattern categories. Any other categories should
// Preferred order of pattern categories. Any other categories should
// be at the bottom without any re-ordering.
const patternCategoriesOrder = [
'featured',
Expand Down Expand Up @@ -68,28 +68,14 @@ function usePatternsCategories( rootClientId ) {
pattern.categories?.includes( category.name )
)
)
.sort( ( { name: currentName }, { name: nextName } ) => {
// The pattern categories should be ordered as follows:
// 1. The categories from `patternCategoriesOrder` in that specific order should be at the top.
// 2. The rest categories should be at the bottom without any re-ordering.
if (
! [ currentName, nextName ].some( ( categoryName ) =>
patternCategoriesOrder.includes( categoryName )
)
) {
return 0;
}
if (
[ currentName, nextName ].every( ( categoryName ) =>
patternCategoriesOrder.includes( categoryName )
)
) {
return (
patternCategoriesOrder.indexOf( currentName ) -
patternCategoriesOrder.indexOf( nextName )
);
}
return patternCategoriesOrder.includes( currentName ) ? -1 : 1;
.sort( ( { name: aName }, { name: bName } ) => {
// Sort categories according to `patternCategoriesOrder`.
let aIndex = patternCategoriesOrder.indexOf( aName );
let bIndex = patternCategoriesOrder.indexOf( bName );
// All other categories should come after that.
if ( aIndex < 0 ) aIndex = patternCategoriesOrder.length;
if ( bIndex < 0 ) bIndex = patternCategoriesOrder.length;
return aIndex - bIndex;
} );

if (
Expand Down

1 comment on commit c4e0a21

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in c4e0a21.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4126453891
📝 Reported issues:

Please sign in to comment.