Skip to content

Commit

Permalink
Memoize the getTemplateInfo selector (WordPress#60200)
Browse files Browse the repository at this point in the history
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
  • Loading branch information
3 people authored and cbravobernal committed Apr 9, 2024
1 parent 9d5f34c commit 0033403
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
8 changes: 4 additions & 4 deletions packages/editor/src/components/post-card-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ export default function PostCardPanel( { className, actions, children } ) {
const { getEditedEntityRecord } = select( coreStore );
const _type = getCurrentPostType();
const _id = getCurrentPostId();
let _templateInfo;
const _record = getEditedEntityRecord( 'postType', _type, _id );
const _templateInfo = __experimentalGetTemplateInfo( _record );
return {
title: _templateInfo?.title || getEditedPostAttribute( 'title' ),
modified: getEditedPostAttribute( 'modified' ),
id: _id,
templateInfo: __experimentalGetTemplateInfo( _record ),
templateInfo: _templateInfo,
icon: unlock( select( editorStore ) ).getPostIcon( _type, {
area: _record?.area,
} ),
Expand Down Expand Up @@ -88,8 +88,8 @@ export default function PostCardPanel( { className, actions, children } ) {
className="editor-post-card-panel__description"
spacing={ 2 }
>
{ !! description && <Text>{ description }</Text> }
{ !! lastEditedText && (
{ description && <Text>{ description }</Text> }
{ lastEditedText && (
<Text>{ lastEditedText }</Text>
) }
</VStack>
Expand Down
59 changes: 33 additions & 26 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1701,8 +1701,8 @@ export function __experimentalGetDefaultTemplateTypes( state ) {
export const __experimentalGetDefaultTemplatePartAreas = createSelector(
( state ) => {
const areas =
getEditorSettings( state )?.defaultTemplatePartAreas || [];
return areas?.map( ( item ) => {
getEditorSettings( state )?.defaultTemplatePartAreas ?? [];
return areas.map( ( item ) => {
return { ...item, icon: getTemplatePartIcon( item.icon ) };
} );
},
Expand Down Expand Up @@ -1730,7 +1730,7 @@ export const __experimentalGetDefaultTemplateType = createSelector(
) ?? EMPTY_OBJECT
);
},
( state, slug ) => [ __experimentalGetDefaultTemplateTypes( state ), slug ]
( state ) => [ __experimentalGetDefaultTemplateTypes( state ) ]
);

/**
Expand All @@ -1741,32 +1741,39 @@ export const __experimentalGetDefaultTemplateType = createSelector(
* @param {Object} template The template for which we need information.
* @return {Object} Information about the template, including title, description, and icon.
*/
export function __experimentalGetTemplateInfo( state, template ) {
if ( ! template ) {
return EMPTY_OBJECT;
}
export const __experimentalGetTemplateInfo = createSelector(
( state, template ) => {
if ( ! template ) {
return EMPTY_OBJECT;
}

const { description, slug, title, area } = template;
const { title: defaultTitle, description: defaultDescription } =
__experimentalGetDefaultTemplateType( state, slug );
const { description, slug, title, area } = template;
const { title: defaultTitle, description: defaultDescription } =
__experimentalGetDefaultTemplateType( state, slug );

const templateTitle = typeof title === 'string' ? title : title?.rendered;
const templateDescription =
typeof description === 'string' ? description : description?.raw;
const templateIcon =
__experimentalGetDefaultTemplatePartAreas( state ).find(
( item ) => area === item.area
)?.icon || layout;
const templateTitle =
typeof title === 'string' ? title : title?.rendered;
const templateDescription =
typeof description === 'string' ? description : description?.raw;
const templateIcon =
__experimentalGetDefaultTemplatePartAreas( state ).find(
( item ) => area === item.area
)?.icon || layout;

return {
title:
templateTitle && templateTitle !== slug
? templateTitle
: defaultTitle || slug,
description: templateDescription || defaultDescription,
icon: templateIcon,
};
}
return {
title:
templateTitle && templateTitle !== slug
? templateTitle
: defaultTitle || slug,
description: templateDescription || defaultDescription,
icon: templateIcon,
};
},
( state ) => [
__experimentalGetDefaultTemplateTypes( state ),
__experimentalGetDefaultTemplatePartAreas( state ),
]
);

/**
* Returns a post type label depending on the current post.
Expand Down

0 comments on commit 0033403

Please sign in to comment.