From 884c4948345196196184824f9f6c90a4c3663c9c Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 8 Feb 2024 16:16:06 +0400 Subject: [PATCH] Template Part: Derive 'hasInnerBlocks' inside selector (#58680) * Template Part: Derive 'hasInnerBlocks' inside selector * Remove leftover comment Co-authored-by: Mamaduka Co-authored-by: draganescu --- .../block-library/src/template-part/edit/index.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/block-library/src/template-part/edit/index.js b/packages/block-library/src/template-part/edit/index.js index 72fc06338e7807..70078e5935836a 100644 --- a/packages/block-library/src/template-part/edit/index.js +++ b/packages/block-library/src/template-part/edit/index.js @@ -82,14 +82,11 @@ export default function TemplatePartEdit( { const [ isTemplatePartSelectionOpen, setIsTemplatePartSelectionOpen ] = useState( false ); - // Set the postId block attribute if it did not exist, - // but wait until the inner blocks have loaded to allow - // new edits to trigger this. - const { isResolved, innerBlocks, isMissing, area } = useSelect( + const { isResolved, hasInnerBlocks, isMissing, area } = useSelect( ( select ) => { const { getEditedEntityRecord, hasFinishedResolution } = select( coreStore ); - const { getBlocks } = select( blockEditorStore ); + const { getBlockCount } = select( blockEditorStore ); const getEntityArgs = [ 'postType', @@ -108,7 +105,7 @@ export default function TemplatePartEdit( { : false; return { - innerBlocks: getBlocks( clientId ), + hasInnerBlocks: getBlockCount( clientId ) > 0, isResolved: hasResolvedEntity, isMissing: hasResolvedEntity && @@ -129,7 +126,7 @@ export default function TemplatePartEdit( { // We don't want to render a missing state if we have any inner blocks. // A new template part is automatically created if we have any inner blocks but no entity. if ( - innerBlocks.length === 0 && + ! hasInnerBlocks && ( ( slug && ! theme ) || ( slug && isMissing ) ) ) { return ( @@ -167,7 +164,7 @@ export default function TemplatePartEdit( { isEntityAvailable={ isEntityAvailable } templatePartId={ templatePartId } defaultWrapper={ areaObject.tagName } - hasInnerBlocks={ innerBlocks.length > 0 } + hasInnerBlocks={ hasInnerBlocks } /> { isPlaceholder && ( @@ -215,7 +212,7 @@ export default function TemplatePartEdit( { tagName={ TagName } blockProps={ blockProps } postId={ templatePartId } - hasInnerBlocks={ innerBlocks.length > 0 } + hasInnerBlocks={ hasInnerBlocks } layout={ layout } /> ) }