From cbaeee2ee82f18a64e5ac9bf445a2f12864bbd77 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Wed, 13 Nov 2024 12:29:03 +0100 Subject: [PATCH] Site Editor: Remove useEditedEntityRecord hook (#66955) Co-authored-by: youknowriad Co-authored-by: mcsf Co-authored-by: Mamaduka --- .../edit-site/src/components/editor/index.js | 7 ++- .../src/components/editor/use-editor-title.js | 50 +++++++++++----- .../use-edited-entity-record/index.js | 58 ------------------- 3 files changed, 42 insertions(+), 73 deletions(-) delete mode 100644 packages/edit-site/src/components/use-edited-entity-record/index.js diff --git a/packages/edit-site/src/components/editor/index.js b/packages/edit-site/src/components/editor/index.js index 097a4f5f75ad7f..7eff0da660d274 100644 --- a/packages/edit-site/src/components/editor/index.js +++ b/packages/edit-site/src/components/editor/index.js @@ -126,12 +126,15 @@ export default function EditSiteEditor( { isPostsList = false } ) { hasSiteIcon: !! siteData?.site_icon_url, }; }, [] ); - useEditorTitle(); + const postWithTemplate = !! contextPostId; + useEditorTitle( + postWithTemplate ? contextPostType : editedPostType, + postWithTemplate ? contextPostId : editedPostId + ); const _isPreviewingTheme = isPreviewingTheme(); const hasDefaultEditorCanvasView = ! useHasEditorCanvasContainer(); const iframeProps = useEditorIframeProps(); const isEditMode = canvas === 'edit'; - const postWithTemplate = !! contextPostId; const loadingProgressId = useInstanceId( CanvasLoader, 'edit-site-editor__loading-progress' diff --git a/packages/edit-site/src/components/editor/use-editor-title.js b/packages/edit-site/src/components/editor/use-editor-title.js index 0645c2031a3af0..2ad4e94ccc3e80 100644 --- a/packages/edit-site/src/components/editor/use-editor-title.js +++ b/packages/edit-site/src/components/editor/use-editor-title.js @@ -2,34 +2,58 @@ * WordPress dependencies */ import { _x, sprintf } from '@wordpress/i18n'; +import { useSelect } from '@wordpress/data'; +import { store as coreStore } from '@wordpress/core-data'; +import { store as editorStore } from '@wordpress/editor'; +import { decodeEntities } from '@wordpress/html-entities'; /** * Internal dependencies */ -import useEditedEntityRecord from '../use-edited-entity-record'; import useTitle from '../routes/use-title'; import { POST_TYPE_LABELS, TEMPLATE_POST_TYPE } from '../../utils/constants'; -function useEditorTitle() { - const { - record: editedPost, - getTitle, - isLoaded: hasLoadedPost, - } = useEditedEntityRecord(); - let title; - if ( hasLoadedPost ) { - title = sprintf( +function useEditorTitle( postType, postId ) { + const { title, isLoaded } = useSelect( + ( select ) => { + const { getEditedEntityRecord, hasFinishedResolution } = + select( coreStore ); + const { __experimentalGetTemplateInfo: getTemplateInfo } = + select( editorStore ); + const _record = getEditedEntityRecord( + 'postType', + postType, + postId + ); + const _isLoaded = hasFinishedResolution( 'getEditedEntityRecord', [ + 'postType', + postType, + postId, + ] ); + const templateInfo = getTemplateInfo( _record ); + + return { + title: templateInfo.title, + isLoaded: _isLoaded, + }; + }, + [ postType, postId ] + ); + + let editorTitle; + if ( isLoaded ) { + editorTitle = sprintf( // translators: A breadcrumb trail for the Admin document title. 1: title of template being edited, 2: type of template (Template or Template Part). _x( '%1$s ‹ %2$s', 'breadcrumb trail' ), - getTitle(), - POST_TYPE_LABELS[ editedPost.type ] ?? + decodeEntities( title ), + POST_TYPE_LABELS[ postType ] ?? POST_TYPE_LABELS[ TEMPLATE_POST_TYPE ] ); } // Only announce the title once the editor is ready to prevent "Replace" // action in from double-announcing. - useTitle( hasLoadedPost && title ); + useTitle( isLoaded && editorTitle ); } export default useEditorTitle; diff --git a/packages/edit-site/src/components/use-edited-entity-record/index.js b/packages/edit-site/src/components/use-edited-entity-record/index.js deleted file mode 100644 index 22a8bdc32a94a0..00000000000000 --- a/packages/edit-site/src/components/use-edited-entity-record/index.js +++ /dev/null @@ -1,58 +0,0 @@ -/** - * WordPress dependencies - */ -import { useSelect } from '@wordpress/data'; -import { store as coreStore } from '@wordpress/core-data'; -import { store as editorStore } from '@wordpress/editor'; -import { decodeEntities } from '@wordpress/html-entities'; - -/** - * Internal dependencies - */ -import { store as editSiteStore } from '../../store'; - -export default function useEditedEntityRecord( postType, postId ) { - const { record, title, description, isLoaded, icon } = useSelect( - ( select ) => { - const { getEditedPostType, getEditedPostId } = - select( editSiteStore ); - const { getEditedEntityRecord, hasFinishedResolution } = - select( coreStore ); - const { __experimentalGetTemplateInfo: getTemplateInfo } = - select( editorStore ); - const usedPostType = postType ?? getEditedPostType(); - const usedPostId = postId ?? getEditedPostId(); - const _record = getEditedEntityRecord( - 'postType', - usedPostType, - usedPostId - ); - const _isLoaded = - usedPostId && - hasFinishedResolution( 'getEditedEntityRecord', [ - 'postType', - usedPostType, - usedPostId, - ] ); - const templateInfo = getTemplateInfo( _record ); - - return { - record: _record, - title: templateInfo.title, - description: templateInfo.description, - isLoaded: _isLoaded, - icon: templateInfo.icon, - }; - }, - [ postType, postId ] - ); - - return { - isLoaded, - icon, - record, - getTitle: () => ( title ? decodeEntities( title ) : null ), - getDescription: () => - description ? decodeEntities( description ) : null, - }; -}