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

Improve DocumentActions performance #51432

Merged
merged 1 commit into from
Jun 13, 2023
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
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