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

Updated: Pages: Include avatar in Author field. #63142

Merged
merged 4 commits into from
Jul 9, 2024
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
59 changes: 58 additions & 1 deletion packages/edit-site/src/components/posts-app/posts-list.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

Unrelated with this PR but this file should be moved to its own components/post-list folder or something, it's not specific to "posts app"

* External dependencies
*/
import clsx from 'clsx';

/**
* WordPress dependencies
*/
import { Button, __experimentalHStack as HStack } from '@wordpress/components';
import {
Button,
__experimentalHStack as HStack,
Icon,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { useEntityRecords, store as coreStore } from '@wordpress/core-data';
import { decodeEntities } from '@wordpress/html-entities';
Expand All @@ -17,6 +26,7 @@ import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useSelect, useDispatch } from '@wordpress/data';
import { DataViews } from '@wordpress/dataviews';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
import { commentAuthorAvatar as authorIcon } from '@wordpress/icons';

/**
* Internal dependencies
Expand Down Expand Up @@ -207,6 +217,48 @@ function getItemId( item ) {
return item.id.toString();
}

function PostAuthorField( { item, viewType } ) {
const { text, icon, imageUrl } = useSelect(
( select ) => {
const { getUser } = select( coreStore );
const user = getUser( item.author );
return {
icon: authorIcon,
imageUrl: user?.avatar_urls?.[ 48 ],
text: user?.name,
};
},
[ item ]
);

const withAuthorImage = viewType !== LAYOUT_LIST && imageUrl;
const withAuthorIcon = viewType !== LAYOUT_LIST && ! imageUrl;
const [ isImageLoaded, setIsImageLoaded ] = useState( false );
return (
<HStack alignment="left" spacing={ 1 }>
{ withAuthorImage && (
<div
className={ clsx( 'page-templates-author-field__avatar', {
'is-loaded': isImageLoaded,
} ) }
>
<img
onLoad={ () => setIsImageLoaded( true ) }
alt={ __( 'Author avatar' ) }
src={ imageUrl }
/>
</div>
) }
{ withAuthorIcon && (
<div className="page-templates-author-field__icon">
<Icon icon={ icon } />
</div>
) }
<span className="page-templates-author-field__name">{ text }</span>
</HStack>
);
}

export default function PostsList( { postType } ) {
const [ view, setView ] = useView( postType );
const history = useHistory();
Expand Down Expand Up @@ -395,6 +447,11 @@ export default function PostsList( { postType } ) {
value: id,
label: name,
} ) ) || [],
render: ( { item } ) => {
return (
Copy link
Contributor

@ntsekouras ntsekouras Jul 8, 2024

Choose a reason for hiding this comment

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

I don't think we should reuse this render field function. Posts list is meant to be reused by every post type eventually, have an API to get the fields by postType, but I see templates a bit different than the 'normal' post types. Maybe I'm wrong, but it wouldn't hurt to duplicate just some code for now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @ntsekouras, I applied your suggestion and did not reuse the field from the templates.

<PostAuthorField viewType={ view.type } item={ item } />
);
},
},
{
header: __( 'Status' ),
Expand Down
Loading