Add basic optimization for usePreviewTemplates
to improve render performance
MAILPOET-5949
This commit is contained in:
committed by
Oluwaseun Olorunsola
parent
bc0ce09d61
commit
8661a169b3
@@ -1,4 +1,4 @@
|
|||||||
// import { useMemo } from '@wordpress/element';
|
import { useMemo } from '@wordpress/element';
|
||||||
import { parse } from '@wordpress/blocks';
|
import { parse } from '@wordpress/blocks';
|
||||||
import { BlockInstance } from '@wordpress/blocks/index';
|
import { BlockInstance } from '@wordpress/blocks/index';
|
||||||
import { useSelect } from '@wordpress/data';
|
import { useSelect } from '@wordpress/data';
|
||||||
@@ -89,13 +89,16 @@ function generateTemplateCssTheme(
|
|||||||
|
|
||||||
export function usePreviewTemplates(
|
export function usePreviewTemplates(
|
||||||
customEmailContent = ''
|
customEmailContent = ''
|
||||||
): TemplatePreview[][] {
|
): [ TemplatePreview[], TemplatePreview[], boolean ] {
|
||||||
const { templates, patterns, emailPosts } = useSelect( ( select ) => {
|
const { templates, patterns, emailPosts, hasEmailPosts } = useSelect(
|
||||||
|
( select ) => {
|
||||||
const contentBlockId =
|
const contentBlockId =
|
||||||
// @ts-expect-error getBlocksByName is not defined in types
|
// @ts-expect-error getBlocksByName is not defined in types
|
||||||
select( blockEditorStore ).getBlocksByName(
|
select( blockEditorStore ).getBlocksByName(
|
||||||
'core/post-content'
|
'core/post-content'
|
||||||
)?.[ 0 ];
|
)?.[ 0 ];
|
||||||
|
|
||||||
|
const rawEmailPosts = select( storeName ).getSentEmailEditorPosts();
|
||||||
return {
|
return {
|
||||||
templates: select( storeName ).getEmailTemplates(),
|
templates: select( storeName ).getEmailTemplates(),
|
||||||
patterns:
|
patterns:
|
||||||
@@ -104,14 +107,14 @@ export function usePreviewTemplates(
|
|||||||
[ 'core/post-content' ],
|
[ 'core/post-content' ],
|
||||||
contentBlockId
|
contentBlockId
|
||||||
),
|
),
|
||||||
emailPosts: select( storeName ).getSentEmailEditorPosts(),
|
emailPosts: rawEmailPosts,
|
||||||
|
hasEmailPosts: !! ( rawEmailPosts && rawEmailPosts?.length ),
|
||||||
};
|
};
|
||||||
}, [] );
|
},
|
||||||
|
[]
|
||||||
if ( ! templates || ( ! patterns.length && ! customEmailContent ) ) {
|
);
|
||||||
return [ [] ];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const allTemplates = useMemo( () => {
|
||||||
let contentPatternBlocksGeneral = null;
|
let contentPatternBlocksGeneral = null;
|
||||||
let contentPatternBlocks = null;
|
let contentPatternBlocks = null;
|
||||||
const parsedCustomEmailContent =
|
const parsedCustomEmailContent =
|
||||||
@@ -138,7 +141,7 @@ export function usePreviewTemplates(
|
|||||||
)?.blocks as BlockInstance[];
|
)?.blocks as BlockInstance[];
|
||||||
}
|
}
|
||||||
|
|
||||||
const allTemplates = templates.map(
|
return templates?.map(
|
||||||
( template: EmailTemplatePreview ): TemplatePreview => {
|
( template: EmailTemplatePreview ): TemplatePreview => {
|
||||||
let parsedTemplate = parse( template.content?.raw );
|
let parsedTemplate = parse( template.content?.raw );
|
||||||
parsedTemplate = setPostContentInnerBlocks(
|
parsedTemplate = setPostContentInnerBlocks(
|
||||||
@@ -163,10 +166,10 @@ export function usePreviewTemplates(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
}, [ templates, patterns, customEmailContent ] );
|
||||||
|
|
||||||
return [
|
const allEmailPosts = useMemo( () => {
|
||||||
allTemplates,
|
return emailPosts?.map( ( post: EmailEditorPostType ) => {
|
||||||
emailPosts?.map( ( post: EmailEditorPostType ) => {
|
|
||||||
const parsedPostContent = parse( post.content?.raw );
|
const parsedPostContent = parse( post.content?.raw );
|
||||||
const cssThemeData = generateTemplateCssTheme( post, allTemplates );
|
const cssThemeData = generateTemplateCssTheme( post, allTemplates );
|
||||||
return {
|
return {
|
||||||
@@ -186,6 +189,8 @@ export function usePreviewTemplates(
|
|||||||
category: 'recent',
|
category: 'recent',
|
||||||
type: post.type,
|
type: post.type,
|
||||||
};
|
};
|
||||||
} ) as unknown as TemplatePreview[],
|
} ) as unknown as TemplatePreview[];
|
||||||
];
|
}, [ emailPosts, allTemplates ] );
|
||||||
|
|
||||||
|
return [ allTemplates || [], allEmailPosts || [], hasEmailPosts ];
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user