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

Add badge to title for posts & front pages #61718

Merged
merged 6 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
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
48 changes: 43 additions & 5 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { Button } from '@wordpress/components';
import { Button, __experimentalHStack as HStack } from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { useEntityRecords, store as coreStore } from '@wordpress/core-data';
import { decodeEntities } from '@wordpress/html-entities';
Expand Down Expand Up @@ -273,6 +273,16 @@ export default function PagePages() {
[ totalItems, totalPages ]
);

const { frontPageId, postsPageId } = useSelect( ( select ) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential follow-up: I think this one can potentially move to the "render" function instead (or should I say render component) (we should treat them as component in DataViews first though) which will make sure it only runs if the field is actually rendered.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Title is always rendered in pages, so it would be the same..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but still :) conceptually it makes sense for this to be within the field.

const { getEntityRecord } = select( coreStore );
const siteSettings = getEntityRecord( 'root', 'site' );

return {
frontPageId: siteSettings?.page_on_front,
postsPageId: siteSettings?.page_for_posts,
};
} );

const fields = useMemo(
() => [
{
Expand All @@ -293,7 +303,7 @@ export default function PagePages() {
const addLink =
[ LAYOUT_TABLE, LAYOUT_GRID ].includes( view.type ) &&
item.status !== 'trash';
return addLink ? (
const title = addLink ? (
<Link
params={ {
postId: item.id,
Expand All @@ -305,8 +315,36 @@ export default function PagePages() {
__( '(no title)' ) }
</Link>
) : (
decodeEntities( item.title?.rendered ) ||
__( '(no title)' )
<span>
{ decodeEntities( item.title?.rendered ) ||
__( '(no title)' ) }
</span>
);

let suffix = '';
if ( item.id === frontPageId ) {
suffix = (
<span className="edit-site-page-pages__title-badge">
{ __( 'Front Page' ) }
</span>
);
} else if ( item.id === postsPageId ) {
suffix = (
<span className="edit-site-page-pages__title-badge">
{ __( 'Posts Page' ) }
</span>
);
}

return (
<HStack
className="edit-site-page-pages-title"
alignment="center"
justify="flex-start"
>
{ title }
{ suffix }
</HStack>
);
},
maxWidth: 300,
Expand Down Expand Up @@ -411,7 +449,7 @@ export default function PagePages() {
},
},
],
[ authors, view.type ]
[ authors, view.type, frontPageId, postsPageId ]
);

const postTypeActions = usePostActions( 'page' );
Expand Down
15 changes: 15 additions & 0 deletions packages/edit-site/src/components/page-pages/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,18 @@
outline: 2px solid transparent;
}
}

.edit-site-page-pages-title span {
text-overflow: ellipsis;
overflow: hidden;
}

.edit-site-page-pages__title-badge {
background: $gray-100;
color: $gray-700;
padding: $grid-unit-05 $grid-unit-10;
border-radius: $radius-block-ui;
font-size: 12px;
font-weight: 400;
flex-shrink: 0;
}
Loading