From 9fa8d8de827497dff029184c3f7003f8ada7e8d1 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 19 Sep 2023 15:24:53 +1200 Subject: [PATCH] Patterns: move mapping of values from core-data selector into consumers (#54576) Co-authored-by: Kai Hao --- docs/reference-guides/data/data-core.md | 2 +- .../inserter/hooks/use-patterns-state.js | 3 +-- packages/block-editor/src/store/selectors.js | 8 +++++-- packages/core-data/README.md | 2 +- packages/core-data/src/selectors.ts | 21 ++++--------------- .../components/page-patterns/use-patterns.js | 11 ++++++---- .../use-pattern-details.js | 7 ++++++- 7 files changed, 26 insertions(+), 28 deletions(-) diff --git a/docs/reference-guides/data/data-core.md b/docs/reference-guides/data/data-core.md index a3d6bd5b6f392c..acaaa5f23f1b98 100644 --- a/docs/reference-guides/data/data-core.md +++ b/docs/reference-guides/data/data-core.md @@ -413,7 +413,7 @@ _Parameters_ _Returns_ -- `UserPatternCategories`: User patterns category array and map keyed by id. +- `Array< UserPatternCategory >`: User patterns category array. ### getUserQueryResults diff --git a/packages/block-editor/src/components/inserter/hooks/use-patterns-state.js b/packages/block-editor/src/components/inserter/hooks/use-patterns-state.js index 0c938b0474a9fa..aef299f90c1842 100644 --- a/packages/block-editor/src/components/inserter/hooks/use-patterns-state.js +++ b/packages/block-editor/src/components/inserter/hooks/use-patterns-state.js @@ -31,8 +31,7 @@ const usePatternsState = ( onInsert, rootClientId ) => { } = getSettings(); return { patterns: __experimentalGetAllowedPatterns( rootClientId ), - userPatternCategories: - __experimentalUserPatternCategories?.patternCategories, + userPatternCategories: __experimentalUserPatternCategories, patternCategories: __experimentalBlockPatternCategories, }; }, diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index c618a354bcd10a..2f73e50b602d2c 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2294,8 +2294,12 @@ const checkAllowListRecursive = ( blocks, allowedBlockTypes ) => { function getUserPatterns( state ) { const userPatterns = state?.settings?.__experimentalReusableBlocks ?? EMPTY_ARRAY; - const { patternCategoriesMap: categories } = - state?.settings?.__experimentalUserPatternCategories ?? {}; + const userPatternCategories = + state?.settings?.__experimentalUserPatternCategories ?? []; + const categories = new Map(); + userPatternCategories.forEach( ( userCategory ) => + categories.set( userCategory.id, userCategory ) + ); return userPatterns.map( ( userPattern ) => { return { name: `core/block/${ userPattern.id }`, diff --git a/packages/core-data/README.md b/packages/core-data/README.md index 43ce17f98f8e47..6024f4db2f12d4 100644 --- a/packages/core-data/README.md +++ b/packages/core-data/README.md @@ -590,7 +590,7 @@ _Parameters_ _Returns_ -- `UserPatternCategories`: User patterns category array and map keyed by id. +- `Array< UserPatternCategory >`: User patterns category array. ### getUserQueryResults diff --git a/packages/core-data/src/selectors.ts b/packages/core-data/src/selectors.ts index a75e2359261c32..7d6cbeb4daeee9 100644 --- a/packages/core-data/src/selectors.ts +++ b/packages/core-data/src/selectors.ts @@ -80,7 +80,7 @@ interface UserState { byId: Record< EntityRecordKey, ET.User< 'edit' > >; } -interface UserPatternCategory { +export interface UserPatternCategory { id: number; name: string; label: string; @@ -88,11 +88,6 @@ interface UserPatternCategory { description: string; } -export interface UserPatternCategories { - patternCategories: Array< UserPatternCategory >; - patternCategoriesMap: Map< number, UserPatternCategory >; -} - type Optional< T > = T | undefined; /** @@ -1241,21 +1236,13 @@ export function getBlockPatternCategories( state: State ): Array< any > { * * @param state Data state. * - * @return User patterns category array and map keyed by id. + * @return User patterns category array. */ export function getUserPatternCategories( state: State -): UserPatternCategories { - const patternCategoriesMap = new Map< number, UserPatternCategory >(); - state.userPatternCategories?.forEach( - ( userCategory: UserPatternCategory ) => - patternCategoriesMap.set( userCategory.id, userCategory ) - ); - return { - patternCategories: state.userPatternCategories, - patternCategoriesMap, - }; +): Array< UserPatternCategory > { + return state.userPatternCategories; } /** diff --git a/packages/edit-site/src/components/page-patterns/use-patterns.js b/packages/edit-site/src/components/page-patterns/use-patterns.js index 42455d201a87ca..daaa9dd7822da6 100644 --- a/packages/edit-site/src/components/page-patterns/use-patterns.js +++ b/packages/edit-site/src/components/page-patterns/use-patterns.js @@ -170,11 +170,14 @@ const selectUserPatterns = ( select, { search = '', syncStatus } = {} ) => { const query = { per_page: -1 }; const records = getEntityRecords( 'postType', PATTERN_TYPES.user, query ); - const categories = getUserPatternCategories(); - + const userPatternCategories = getUserPatternCategories(); + const categories = new Map(); + userPatternCategories.forEach( ( userCategory ) => + categories.set( userCategory.id, userCategory ) + ); let patterns = records ? records.map( ( record ) => - patternBlockToPattern( record, categories.patternCategoriesMap ) + patternBlockToPattern( record, categories ) ) : EMPTY_PATTERN_LIST; @@ -197,7 +200,7 @@ const selectUserPatterns = ( select, { search = '', syncStatus } = {} ) => { hasCategory: () => true, } ); - return { patterns, isResolving, categories: categories.patternCategories }; + return { patterns, isResolving, categories: userPatternCategories }; }; export const usePatterns = ( diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-pattern/use-pattern-details.js b/packages/edit-site/src/components/sidebar-navigation-screen-pattern/use-pattern-details.js index ea84610c3de589..fb6be8c7fa2f94 100644 --- a/packages/edit-site/src/components/sidebar-navigation-screen-pattern/use-pattern-details.js +++ b/packages/edit-site/src/components/sidebar-navigation-screen-pattern/use-pattern-details.js @@ -38,10 +38,15 @@ export default function usePatternDetails( postType, postId ) { const { currentTheme, patternCategories } = useSelect( ( select ) => { const { getCurrentTheme, getUserPatternCategories } = select( coreStore ); + const userPatternCategories = getUserPatternCategories(); + const categories = new Map(); + userPatternCategories.forEach( ( userCategory ) => + categories.set( userCategory.id, userCategory ) + ); return { currentTheme: getCurrentTheme(), - patternCategories: getUserPatternCategories().patternCategoriesMap, + patternCategories: categories, }; }, [] );