Skip to content

Commit

Permalink
Memoize useSelect for usePatterns
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Sep 19, 2023
1 parent b06240a commit 1a6c423
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 141 deletions.
19 changes: 11 additions & 8 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import { camelCase } from 'change-case';
import createSelector from 'rememo';

/**
* WordPress dependencies
Expand Down Expand Up @@ -619,6 +620,15 @@ export const getBlockPatternCategories =
dispatch( { type: 'RECEIVE_BLOCK_PATTERN_CATEGORIES', categories } );
};

const mapPatternCategories = createSelector(
( patternCategories ) =>
patternCategories?.map( ( userCategory ) => ( {
...userCategory,
label: userCategory.name,
name: userCategory.slug,
} ) ) || []
);

export const getUserPatternCategories =
() =>
async ( { dispatch, resolveSelect } ) => {
Expand All @@ -631,16 +641,9 @@ export const getUserPatternCategories =
}
);

const mappedPatternCategories =
patternCategories?.map( ( userCategory ) => ( {
...userCategory,
label: userCategory.name,
name: userCategory.slug,
} ) ) || [];

dispatch( {
type: 'RECEIVE_USER_PATTERN_CATEGORIES',
patternCategories: mappedPatternCategories,
patternCategories: mapPatternCategories( patternCategories ),
} );
};

Expand Down
Loading

0 comments on commit 1a6c423

Please sign in to comment.