-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated: Pages: Include avatar in Author field.
- Loading branch information
1 parent
6273def
commit 8add9e0
Showing
3 changed files
with
96 additions
and
39 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
packages/edit-site/src/components/page-templates/author-field.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import clsx from 'clsx'; | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Icon, __experimentalHStack as HStack } from '@wordpress/components'; | ||
import { useState } from '@wordpress/element'; | ||
import { commentAuthorAvatar as authorIcon } from '@wordpress/icons'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useAddedBy } from './hooks'; | ||
import { LAYOUT_LIST } from '../../utils/constants'; | ||
|
||
function BaseAuthorField( { viewType, text, icon, imageUrl } ) { | ||
const [ isImageLoaded, setIsImageLoaded ] = useState( false ); | ||
const withIcon = viewType !== LAYOUT_LIST; | ||
|
||
return ( | ||
<HStack alignment="left" spacing={ 1 }> | ||
{ withIcon && imageUrl && ( | ||
<div | ||
className={ clsx( 'page-templates-author-field__avatar', { | ||
'is-loaded': isImageLoaded, | ||
} ) } | ||
> | ||
<img | ||
onLoad={ () => setIsImageLoaded( true ) } | ||
alt="" | ||
src={ imageUrl } | ||
/> | ||
</div> | ||
) } | ||
{ withIcon && ! imageUrl && ( | ||
<div className="page-templates-author-field__icon"> | ||
<Icon icon={ icon } /> | ||
</div> | ||
) } | ||
<span className="page-templates-author-field__name">{ text }</span> | ||
</HStack> | ||
); | ||
} | ||
|
||
export function TemplateAuthorField( { item, viewType } ) { | ||
const { text, icon, imageUrl } = useAddedBy( item.type, item.id ); | ||
|
||
return ( | ||
<BaseAuthorField | ||
viewType={ viewType } | ||
text={ text } | ||
icon={ icon } | ||
imageUrl={ imageUrl } | ||
/> | ||
); | ||
} | ||
|
||
export 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 ] | ||
); | ||
return ( | ||
<BaseAuthorField | ||
viewType={ viewType } | ||
text={ text } | ||
icon={ icon } | ||
imageUrl={ imageUrl } | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters