-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Tidy up block patterns selectors #57913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,52 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import createSelector from 'rememo'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { INSERTER_PATTERN_TYPES } from '../components/inserter/block-patterns-tab/utils'; | ||
|
|
||
| const EMPTY_ARRAY = []; | ||
| export const getUserPatterns = createSelector( | ||
| ( state ) => { | ||
| const userPatterns = state.settings.__experimentalReusableBlocks ?? []; | ||
| const userPatternCategories = | ||
| state.settings.__experimentalUserPatternCategories ?? []; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two arrays are never returned from the selector, so the static constant
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be easy to accidentally slip in later though.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although we're anyway mapping below I see now. |
||
| return userPatterns.map( ( userPattern ) => { | ||
| return { | ||
| name: `core/block/${ userPattern.id }`, | ||
| id: userPattern.id, | ||
| type: INSERTER_PATTERN_TYPES.user, | ||
| title: userPattern.title.raw, | ||
| categories: userPattern.wp_pattern_category.map( ( catId ) => { | ||
| const category = userPatternCategories.find( | ||
| ( { id } ) => id === catId | ||
| ); | ||
| return category ? category.slug : catId; | ||
| } ), | ||
| content: userPattern.content.raw, | ||
| syncStatus: userPattern.wp_pattern_sync_status, | ||
| }; | ||
| } ); | ||
| }, | ||
| ( state ) => [ | ||
| state.settings.__experimentalReusableBlocks, | ||
| state.settings.__experimentalUserPatternCategories, | ||
| ] | ||
| ); | ||
|
|
||
| export function getUserPatterns( state ) { | ||
| const userPatterns = | ||
| state?.settings?.__experimentalReusableBlocks ?? EMPTY_ARRAY; | ||
| 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 }`, | ||
| id: userPattern.id, | ||
| type: INSERTER_PATTERN_TYPES.user, | ||
| title: userPattern.title.raw, | ||
| categories: userPattern.wp_pattern_category.map( ( catId ) => | ||
| categories && categories.get( catId ) | ||
| ? categories.get( catId ).slug | ||
| : catId | ||
| ), | ||
| content: userPattern.content.raw, | ||
| syncStatus: userPattern.wp_pattern_sync_status, | ||
| }; | ||
| } ); | ||
| } | ||
| export const getAllPatterns = createSelector( | ||
| ( state ) => { | ||
| const patterns = state.settings.__experimentalBlockPatterns; | ||
| const userPatterns = getUserPatterns( state ); | ||
| return [ ...userPatterns, ...patterns ]; | ||
| }, | ||
| ( state ) => [ | ||
| state.settings.__experimentalBlockPatterns, | ||
| getUserPatterns( state ), | ||
| ] | ||
| ); | ||
|
|
||
| export const checkAllowList = ( list, item, defaultResult = null ) => { | ||
| if ( typeof list === 'boolean' ) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes an omission from #46204: that PR uses the
rootClientIdsubstate only incanInsertBlockType, but not in other selectors that callcanInsertBlockTypeorcanIncludeBlockTypeInInserterand therefore need to declare the same dependencies, too.