From 7ca9567e85350789e47ed41e71a6fc8af3fc1da8 Mon Sep 17 00:00:00 2001 From: Hit Bhalodia <58802366+hbhalodia@users.noreply.github.com> Date: Tue, 29 Oct 2024 16:42:03 +0530 Subject: [PATCH] Fix: Improve the DocumentBar post type label for the Homepage and Posts Page cases (#66355) * Add the correct consistent label on document bar * Add the utililty custom hook to get the page badge based on current view * Change the variable name to be more precise * Update the document bar label display logic * Address the PR feedbacks Co-authored-by: hbhalodia Co-authored-by: Vrishabhsk Co-authored-by: afercia --- .../src/components/document-bar/index.js | 16 +++++++- .../src/components/post-card-panel/index.js | 25 ++++------- packages/editor/src/utils/pageTypeBadge.js | 41 +++++++++++++++++++ 3 files changed, 63 insertions(+), 19 deletions(-) create mode 100644 packages/editor/src/utils/pageTypeBadge.js diff --git a/packages/editor/src/components/document-bar/index.js b/packages/editor/src/components/document-bar/index.js index 3c522189a2632f..031262f52f7268 100644 --- a/packages/editor/src/components/document-bar/index.js +++ b/packages/editor/src/components/document-bar/index.js @@ -28,6 +28,7 @@ import { decodeEntities } from '@wordpress/html-entities'; */ import { TEMPLATE_POST_TYPES } from '../../store/constants'; import { store as editorStore } from '../../store'; +import usePageTypeBadge from '../../utils/pageTypeBadge'; /** @typedef {import("@wordpress/components").IconType} IconType */ @@ -107,6 +108,8 @@ export default function DocumentBar( props ) { const title = props.title || entityTitle; const icon = props.icon; + const pageTypeBadge = usePageTypeBadge(); + const mountedRef = useRef( false ); useEffect( () => { mountedRef.current = true; @@ -180,11 +183,20 @@ export default function DocumentBar( props ) { ? decodeEntities( title ) : __( 'No title' ) } - { postTypeLabel && ! props.title && ( + { pageTypeBadge && ( - { '· ' + decodeEntities( postTypeLabel ) } + { `· ${ pageTypeBadge }` } ) } + { postTypeLabel && + ! props.title && + ! pageTypeBadge && ( + + { `· ${ decodeEntities( + postTypeLabel + ) }` } + + ) } diff --git a/packages/editor/src/components/post-card-panel/index.js b/packages/editor/src/components/post-card-panel/index.js index 1365659f708147..410b8cfd4447d9 100644 --- a/packages/editor/src/components/post-card-panel/index.js +++ b/packages/editor/src/components/post-card-panel/index.js @@ -21,22 +21,17 @@ import { } from '../../store/constants'; import { unlock } from '../../lock-unlock'; import PostActions from '../post-actions'; +import usePageTypeBadge from '../../utils/pageTypeBadge'; export default function PostCardPanel( { postType, postId, onActionPerformed, } ) { - const { isFrontPage, isPostsPage, title, icon } = useSelect( + const { title, icon } = useSelect( ( select ) => { const { __experimentalGetTemplateInfo } = select( editorStore ); - const { canUser, getEditedEntityRecord } = select( coreStore ); - const siteSettings = canUser( 'read', { - kind: 'root', - name: 'site', - } ) - ? getEditedEntityRecord( 'root', 'site' ) - : undefined; + const { getEditedEntityRecord } = select( coreStore ); const _record = getEditedEntityRecord( 'postType', postType, @@ -51,12 +46,13 @@ export default function PostCardPanel( { icon: unlock( select( editorStore ) ).getPostIcon( postType, { area: _record?.area, } ), - isFrontPage: siteSettings?.page_on_front === postId, - isPostsPage: siteSettings?.page_for_posts === postId, }; }, [ postId, postType ] ); + + const pageTypeBadge = usePageTypeBadge(); + return (
{ title ? decodeEntities( title ) : __( 'No title' ) } - { isFrontPage && ( - - { __( 'Homepage' ) } - - ) } - { isPostsPage && ( + { pageTypeBadge && ( - { __( 'Posts Page' ) } + { pageTypeBadge } ) } diff --git a/packages/editor/src/utils/pageTypeBadge.js b/packages/editor/src/utils/pageTypeBadge.js new file mode 100644 index 00000000000000..321b9caa17769e --- /dev/null +++ b/packages/editor/src/utils/pageTypeBadge.js @@ -0,0 +1,41 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { useSelect } from '@wordpress/data'; +import { store as coreStore } from '@wordpress/core-data'; + +/** + * Internal dependencies + */ +import { store as editorStore } from '../store'; + +/** + * Custom hook to get the page type badge for the current post on edit site view. + */ +export default function usePageTypeBadge() { + const { isFrontPage, isPostsPage } = useSelect( ( select ) => { + const { getCurrentPostId } = select( editorStore ); + const { canUser, getEditedEntityRecord } = select( coreStore ); + const postId = getCurrentPostId(); + const siteSettings = canUser( 'read', { + kind: 'root', + name: 'site', + } ) + ? getEditedEntityRecord( 'root', 'site' ) + : undefined; + + return { + isFrontPage: siteSettings?.page_on_front === postId, + isPostsPage: siteSettings?.page_for_posts === postId, + }; + } ); + + if ( isFrontPage ) { + return __( 'Homepage' ); + } else if ( isPostsPage ) { + return __( 'Posts Page' ); + } + + return false; +}