Skip to content

Commit

Permalink
Updated: Pages: Include avatar in Author field. (WordPress#63142)
Browse files Browse the repository at this point in the history
Co-authored-by: jorgefilipecosta <[email protected]>
Co-authored-by: carolinan <[email protected]>
Co-authored-by: ntsekouras <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: jameskoster <[email protected]>
Co-authored-by: ellatrix <[email protected]>
  • Loading branch information
7 people authored and huubl committed Jul 10, 2024
1 parent 914f829 commit c1c1d12
Showing 1 changed file with 58 additions and 1 deletion.
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 @@
/**
* 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 (
<PostAuthorField viewType={ view.type } item={ item } />
);
},
},
{
header: __( 'Status' ),
Expand Down

0 comments on commit c1c1d12

Please sign in to comment.