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

Fix: Improve the DocumentBar post type label for the Homepage and Posts Page cases #66355

Merged
merged 8 commits into from
Oct 29, 2024
15 changes: 13 additions & 2 deletions packages/editor/src/components/document-bar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { decodeEntities } from '@wordpress/html-entities';
*/
import { TEMPLATE_POST_TYPES, GLOBAL_POST_TYPES } from '../../store/constants';
import { store as editorStore } from '../../store';
import usePageTypeBadge from '../../utils/pageTypeBadge';

/** @typedef {import("@wordpress/components").IconType} IconType */

Expand Down Expand Up @@ -110,6 +111,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;
Expand Down Expand Up @@ -184,11 +187,19 @@ export default function DocumentBar( props ) {
? decodeEntities( title )
: __( 'No title' ) }
</span>
{ postTypeLabel && ! props.title && (
{ pageTypeBadge && ! props.title && (
<span className="editor-document-bar__post-type-label">
{ '· ' + decodeEntities( postTypeLabel ) }
{ '. ' + pageTypeBadge }
</span>
) }
{ postTypeLabel &&
! props.title &&
! pageTypeBadge && (
<span className="editor-document-bar__post-type-label">
hbhalodia marked this conversation as resolved.
Show resolved Hide resolved
{ '· ' +
decodeEntities( postTypeLabel ) }
</span>
) }
</Text>
</motion.div>
<span className="editor-document-bar__shortcut">
Expand Down
25 changes: 8 additions & 17 deletions packages/editor/src/components/post-card-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,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, isSync } = useSelect(
const { title, icon, isSync } = 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,
Expand Down Expand Up @@ -71,12 +66,13 @@ export default function PostCardPanel( {
area: _record?.area,
} ),
isSync: _isSync,
isFrontPage: siteSettings?.page_on_front === postId,
isPostsPage: siteSettings?.page_for_posts === postId,
};
},
[ postId, postType ]
);

const pageTypeBadge = usePageTypeBadge();

return (
<div className="editor-post-card-panel">
<HStack
Expand All @@ -99,14 +95,9 @@ export default function PostCardPanel( {
lineHeight="20px"
>
{ title ? decodeEntities( title ) : __( 'No title' ) }
{ isFrontPage && (
<span className="editor-post-card-panel__title-badge">
{ __( 'Homepage' ) }
</span>
) }
{ isPostsPage && (
{ pageTypeBadge && (
<span className="editor-post-card-panel__title-badge">
{ __( 'Posts Page' ) }
{ pageTypeBadge }
</span>
) }
</Text>
Expand Down
41 changes: 41 additions & 0 deletions packages/editor/src/utils/pageTypeBadge.js
Original file line number Diff line number Diff line change
@@ -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();
hbhalodia marked this conversation as resolved.
Show resolved Hide resolved
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;
}
Loading