-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
116 additions
and
61 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import clsx from 'clsx'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useState } from '@wordpress/element'; | ||
import { commentAuthorAvatar as authorIcon } from '@wordpress/icons'; | ||
import { __experimentalHStack as HStack, Icon } from '@wordpress/components'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import type { User } from '@wordpress/core-data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { BasePost } from '../../types'; | ||
|
||
function AuthorView( { item }: { item: BasePost } ) { | ||
const { text, imageUrl } = useSelect( | ||
( select ) => { | ||
const { getEntityRecord } = select( coreStore ); | ||
let user: User | undefined; | ||
if ( !! item.author ) { | ||
user = getEntityRecord( 'root', 'user', item.author ); | ||
} | ||
return { | ||
imageUrl: user?.avatar_urls?.[ 48 ], | ||
text: user?.name, | ||
}; | ||
}, | ||
[ item ] | ||
); | ||
const [ isImageLoaded, setIsImageLoaded ] = useState( false ); | ||
return ( | ||
<HStack alignment="left" spacing={ 0 }> | ||
{ !! imageUrl && ( | ||
<div | ||
className={ clsx( 'page-templates-author-field__avatar', { | ||
'is-loaded': isImageLoaded, | ||
} ) } | ||
> | ||
<img | ||
onLoad={ () => setIsImageLoaded( true ) } | ||
alt={ __( 'Author avatar' ) } | ||
src={ imageUrl } | ||
/> | ||
</div> | ||
) } | ||
{ ! imageUrl && ( | ||
<div className="page-templates-author-field__icon"> | ||
<Icon icon={ authorIcon } /> | ||
</div> | ||
) } | ||
<span className="page-templates-author-field__name">{ text }</span> | ||
</HStack> | ||
); | ||
} | ||
|
||
export default AuthorView; |
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,32 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import type { Field } from '@wordpress/dataviews'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { BasePost } from '../../types'; | ||
import AuthorView from './author-view'; | ||
|
||
const authorField: Field< BasePost > = { | ||
label: __( 'Author' ), | ||
id: 'author', | ||
type: 'integer', | ||
elements: [], | ||
render: AuthorView, | ||
sort: ( a, b, direction ) => { | ||
const nameA = a._embedded?.author?.[ 0 ]?.name || ''; | ||
const nameB = b._embedded?.author?.[ 0 ]?.name || ''; | ||
|
||
return direction === 'asc' | ||
? nameA.localeCompare( nameB ) | ||
: nameB.localeCompare( nameA ); | ||
}, | ||
}; | ||
|
||
/** | ||
* Author field for BasePost. | ||
*/ | ||
export default authorField; |
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