Skip to content
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

Patterns: move mapping of values from core-data selector into consumers #54576

Merged
merged 5 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference-guides/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ _Parameters_

_Returns_

- `UserPatternCategories`: User patterns category array and map keyed by id.
- `Array< UserPatternCategory >`: User patterns category array.

### getUserQueryResults

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ const usePatternsState = ( onInsert, rootClientId ) => {
} = getSettings();
return {
patterns: __experimentalGetAllowedPatterns( rootClientId ),
userPatternCategories:
__experimentalUserPatternCategories?.patternCategories,
userPatternCategories: __experimentalUserPatternCategories,
patternCategories: __experimentalBlockPatternCategories,
};
},
Expand Down
8 changes: 6 additions & 2 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }`,
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ _Parameters_

_Returns_

- `UserPatternCategories`: User patterns category array and map keyed by id.
- `Array< UserPatternCategory >`: User patterns category array.

### getUserQueryResults

Expand Down
21 changes: 4 additions & 17 deletions packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,14 @@ interface UserState {
byId: Record< EntityRecordKey, ET.User< 'edit' > >;
}

interface UserPatternCategory {
export interface UserPatternCategory {
id: number;
name: string;
label: string;
slug: string;
description: string;
}

export interface UserPatternCategories {
patternCategories: Array< UserPatternCategory >;
patternCategoriesMap: Map< number, UserPatternCategory >;
}

type Optional< T > = T | undefined;

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down
11 changes: 7 additions & 4 deletions packages/edit-site/src/components/page-patterns/use-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
);
Comment on lines +42 to +45
Copy link
Member

Choose a reason for hiding this comment

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

This will still return a new value every time mapSelect is called. You should see the same useSelect notice where this hook is used.

We should generally avoid mapping/grouping the items inside the selectors to avoid bugs like #53596.

P.S. The usePatterns already had this issue, and I've left a comment on the original PR. Cc @kevin940726.

Copy link
Contributor

Choose a reason for hiding this comment

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

Appreciate you following up on the useSelect notices here @Mamaduka 👍

I was hoping to have a look into these this afternoon.

Fixing up use-pattern-details.js is pretty straightforward, it might take me a bit longer to untangle the usePatterns hook though.

Copy link
Member

Choose a reason for hiding this comment

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

@aaronrobertshaw, yes, the usePatterns will take more refactoring to untangle :(

Copy link
Contributor

@aaronrobertshaw aaronrobertshaw Sep 19, 2023

Choose a reason for hiding this comment

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

I'll throw up a PR for usePatternDetails shortly so that small chunk isn't held up by the larger refactor.

Edit: Initial PR is available - #54584

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @Mamaduka! I knew that there was a ping from you but I couldn't remember where 😅.

Yeah, the use-patterns one was my bad, but I don't think it's the bottleneck in most cases. Re-renders aren't bad IMO, only when the computation is heavy.

However, I agree we can optimize it, usually done by memoizing intermediate results. I'm looking into ways to memoize the selected data using rememo and let's see if it helps!

Copy link
Contributor

Choose a reason for hiding this comment

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

@kevin940726 is already working on usePatterns so I'll aim to help him on that front.

Copy link
Member

Choose a reason for hiding this comment

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

Thank you both! Happy to help out with testing/reviews.


return {
currentTheme: getCurrentTheme(),
patternCategories: getUserPatternCategories().patternCategoriesMap,
patternCategories: categories,
};
}, [] );

Expand Down
Loading