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

Template part: avoid pattern fetch on mount #60297

Merged
merged 2 commits into from
Mar 29, 2024
Merged
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
62 changes: 29 additions & 33 deletions packages/block-library/src/template-part/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ function ReplaceButton( {
isTemplatePartSelectionOpen,
setIsTemplatePartSelectionOpen,
} ) {
// This hook fetches patterns, so don't run it unconditionally in the main
// edit function!
const { templateParts } = useAlternativeTemplateParts(
area,
templatePartId
Expand Down Expand Up @@ -75,21 +77,30 @@ function ReplaceButton( {
);
}

function TemplatesList( { availableTemplates, onSelect } ) {
const shownTemplates = useAsyncList( availableTemplates );
function TemplatesList( { area, clientId, isEntityAvailable, onSelect } ) {
// This hook fetches patterns, so don't run it unconditionally in the main
// edit function!
const blockPatterns = useAlternativeBlockPatterns( area, clientId );
const canReplace =
isEntityAvailable &&
!! blockPatterns.length &&
( area === 'header' || area === 'footer' );
const shownTemplates = useAsyncList( blockPatterns );

if ( ! availableTemplates ) {
if ( ! canReplace ) {
return null;
}

return (
<BlockPatternsList
label={ __( 'Templates' ) }
blockPatterns={ availableTemplates }
shownPatterns={ shownTemplates }
onClickPattern={ onSelect }
showTitle={ false }
/>
<PanelBody title={ __( 'Design' ) }>
<BlockPatternsList
label={ __( 'Templates' ) }
blockPatterns={ blockPatterns }
shownPatterns={ shownTemplates }
onClickPattern={ onSelect }
showTitle={ false }
/>
</PanelBody>
);
}

Expand Down Expand Up @@ -155,23 +166,12 @@ export default function TemplatePartEdit( {
[ templatePartId, attributes.area, clientId ]
);

const { templateParts } = useAlternativeTemplateParts(
area,
templatePartId
);
const blockPatterns = useAlternativeBlockPatterns( area, clientId );
const hasReplacements = !! templateParts.length || !! blockPatterns.length;
Copy link
Member Author

Choose a reason for hiding this comment

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

I removed the templateParts check because it doesn't seem relevant? These templates are not included in the list.

const areaObject = useTemplatePartArea( area );
const blockProps = useBlockProps();
const isPlaceholder = ! slug;
const isEntityAvailable = ! isPlaceholder && ! isMissing && isResolved;
const TagName = tagName || areaObject.tagName;

const canReplace =
isEntityAvailable &&
hasReplacements &&
( area === 'header' || area === 'footer' );

const onPatternSelect = async ( pattern ) => {
await editEntityRecord(
'postType',
Expand Down Expand Up @@ -293,18 +293,14 @@ export default function TemplatePartEdit( {
} }
</BlockSettingsMenuControls>

{ canReplace && blockPatterns.length > 0 && (
<InspectorControls>
<PanelBody title={ __( 'Design' ) }>
<TemplatesList
availableTemplates={ blockPatterns }
onSelect={ ( pattern ) =>
onPatternSelect( pattern )
}
/>
</PanelBody>
</InspectorControls>
) }
<InspectorControls>
<TemplatesList
area={ area }
clientId={ clientId }
isEntityAvailable={ isEntityAvailable }
onSelect={ ( pattern ) => onPatternSelect( pattern ) }
/>
</InspectorControls>

{ isEntityAvailable && (
<TemplatePartInnerBlocks
Expand Down
Loading