Skip to content

Commit

Permalink
Extract author to fields package
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Nov 12, 2024
1 parent d2bc9ea commit f425bf7
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 61 deletions.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 3 additions & 61 deletions packages/edit-site/src/components/post-fields/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import clsx from 'clsx';

/**
* WordPress dependencies
*/
Expand All @@ -13,12 +8,9 @@ import {
slugField,
parentField,
passwordField,
authorField,
} from '@wordpress/fields';
import {
createInterpolateElement,
useMemo,
useState,
} from '@wordpress/element';
import { createInterpolateElement, useMemo } from '@wordpress/element';
import { dateI18n, getDate, getSettings } from '@wordpress/date';
import {
trash,
Expand All @@ -27,7 +19,6 @@ import {
scheduled,
pending,
notAllowed,
commentAuthorAvatar as authorIcon,
} from '@wordpress/icons';
import { __experimentalHStack as HStack, Icon } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
Expand Down Expand Up @@ -96,44 +87,6 @@ function PostStatusField( { item } ) {
);
}

function PostAuthorField( { item } ) {
const { text, imageUrl } = useSelect(
( select ) => {
const { getUser } = select( coreStore );
const user = getUser( 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>
);
}

function usePostFields() {
const { records: authors, isResolving: isLoadingAuthors } =
useEntityRecords( 'root', 'user', { per_page: -1 } );
Expand Down Expand Up @@ -196,23 +149,12 @@ function usePostFields() {
enableHiding: false,
},
{
label: __( 'Author' ),
id: 'author',
type: 'integer',
...authorField,
elements:
authors?.map( ( { id, name } ) => ( {
value: id,
label: name,
} ) ) || [],
render: PostAuthorField,
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 );
},
},
{
label: __( 'Status' ),
Expand Down
4 changes: 4 additions & 0 deletions packages/fields/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ npm install @wordpress/fields --save

<!-- START TOKEN(Autogenerated API docs) -->

### authorField

Author field for BasePost.

### deletePost

Undocumented declaration.
Expand Down
1 change: 1 addition & 0 deletions packages/fields/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@wordpress/warning": "*",
"change-case": "4.1.2",
"client-zip": "^2.4.5",
"clsx": "2.1.1",
"remove-accents": "^0.5.0"
},
"peerDependencies": {
Expand Down
63 changes: 63 additions & 0 deletions packages/fields/src/fields/author/author-view.tsx
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;
32 changes: 32 additions & 0 deletions packages/fields/src/fields/author/index.tsx
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;
1 change: 1 addition & 0 deletions packages/fields/src/fields/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { default as orderField } from './order';
export { default as featuredImageField } from './featured-image';
export { default as parentField } from './parent';
export { default as passwordField } from './password';
export { default as authorField } from './author';
10 changes: 10 additions & 0 deletions packages/fields/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ interface Links {
[ key: string ]: { href: string }[] | undefined;
}

interface Author {
name: string;
}

interface Embedded {
author?: Author[];
}

export interface BasePost extends CommonPost {
comment_status?: 'open' | 'closed';
excerpt?: string | { raw: string; rendered: string };
Expand All @@ -37,6 +45,8 @@ export interface BasePost extends CommonPost {
link?: string;
slug?: string;
permalink_template?: string;
author?: number;
_embedded?: Embedded;
}

export interface Template extends CommonPost {
Expand Down

0 comments on commit f425bf7

Please sign in to comment.