Skip to content

Commit

Permalink
Improve DocumentActions re-render performance (#51432)
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks authored Jun 13, 2023
1 parent 46e4e9f commit c402cde
Showing 1 changed file with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import {
chevronLeftSmall as chevronLeftSmallIcon,
page as pageIcon,
} from '@wordpress/icons';
import { useEntityRecord } from '@wordpress/core-data';
import { displayShortcut } from '@wordpress/keycodes';
import { useState, useEffect, useRef } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
Expand All @@ -36,20 +36,30 @@ export default function DocumentActions() {
}

function PageDocumentActions() {
const { hasPageContentFocus, context } = useSelect(
( select ) => ( {
hasPageContentFocus: select( editSiteStore ).hasPageContentFocus(),
context: select( editSiteStore ).getEditedPostContext(),
} ),
const { hasPageContentFocus, hasResolved, isFound, title } = useSelect(
( select ) => {
const {
hasPageContentFocus: _hasPageContentFocus,
getEditedPostContext,
} = select( editSiteStore );
const { getEditedEntityRecord, hasFinishedResolution } =
select( coreStore );
const context = getEditedPostContext();
const queryArgs = [ 'postType', context.postType, context.postId ];
const page = getEditedEntityRecord( ...queryArgs );
return {
hasPageContentFocus: _hasPageContentFocus(),
hasResolved: hasFinishedResolution(
'getEditedEntityRecord',
queryArgs
),
isFound: !! page,
title: page?.title,
};
},
[]
);

const { hasResolved, editedRecord } = useEntityRecord(
'postType',
context.postType,
context.postId
);

const { setHasPageContentFocus } = useDispatch( editSiteStore );

const [ hasEditedTemplate, setHasEditedTemplate ] = useState( false );
Expand All @@ -65,7 +75,7 @@ function PageDocumentActions() {
return null;
}

if ( ! editedRecord ) {
if ( ! isFound ) {
return (
<div className="edit-site-document-actions">
{ __( 'Document not found' ) }
Expand All @@ -80,7 +90,7 @@ function PageDocumentActions() {
} ) }
icon={ pageIcon }
>
{ editedRecord.title }
{ title }
</BaseDocumentActions>
) : (
<TemplateDocumentActions
Expand Down

0 comments on commit c402cde

Please sign in to comment.