-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
408 lines (388 loc) · 9.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';
import {
createInterpolateElement,
useMemo,
useState,
} from '@wordpress/element';
import { dateI18n, getDate, getSettings } from '@wordpress/date';
import {
trash,
drafts,
published,
scheduled,
pending,
notAllowed,
commentAuthorAvatar as authorIcon,
} from '@wordpress/icons';
import { __experimentalHStack as HStack, Icon } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useEntityRecords, store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import {
LAYOUT_GRID,
LAYOUT_TABLE,
LAYOUT_LIST,
OPERATOR_IS_ANY,
} from '../../utils/constants';
import { default as Link, useLink } from '../routes/link';
import Media from '../media';
// See https://github.com/WordPress/gutenberg/issues/55886
// We do not support custom statutes at the moment.
const STATUSES = [
{
value: 'draft',
label: __( 'Draft' ),
icon: drafts,
description: __( 'Not ready to publish.' ),
},
{
value: 'future',
label: __( 'Scheduled' ),
icon: scheduled,
description: __( 'Publish automatically on a chosen date.' ),
},
{
value: 'pending',
label: __( 'Pending Review' ),
icon: pending,
description: __( 'Waiting for review before publishing.' ),
},
{
value: 'private',
label: __( 'Private' ),
icon: notAllowed,
description: __( 'Only visible to site admins and editors.' ),
},
{
value: 'publish',
label: __( 'Published' ),
icon: published,
description: __( 'Visible to everyone.' ),
},
{ value: 'trash', label: __( 'Trash' ), icon: trash },
];
const getFormattedDate = ( dateToDisplay ) =>
dateI18n(
getSettings().formats.datetimeAbbreviated,
getDate( dateToDisplay )
);
function FeaturedImage( { item, viewType } ) {
const isDisabled = item.status === 'trash';
const { onClick } = useLink( {
postId: item.id,
postType: item.type,
canvas: 'edit',
} );
const hasMedia = !! item.featured_media;
const size =
viewType === LAYOUT_GRID
? [ 'large', 'full', 'medium', 'thumbnail' ]
: [ 'thumbnail', 'medium', 'large', 'full' ];
const media = hasMedia ? (
<Media
className="edit-site-post-list__featured-image"
id={ item.featured_media }
size={ size }
/>
) : null;
const renderButton = viewType !== LAYOUT_LIST && ! isDisabled;
return (
<div
className={ `edit-site-post-list__featured-image-wrapper is-layout-${ viewType }` }
>
{ renderButton ? (
<button
className="edit-site-post-list__featured-image-button"
type="button"
onClick={ onClick }
aria-label={ item.title?.rendered || __( '(no title)' ) }
>
{ media }
</button>
) : (
media
) }
</div>
);
}
function PostStatusField( { item } ) {
const status = STATUSES.find( ( { value } ) => value === item.status );
const label = status?.label || item.status;
const icon = status?.icon;
return (
<HStack alignment="left" spacing={ 0 }>
{ icon && (
<div className="edit-site-post-list__status-icon">
<Icon icon={ icon } />
</div>
) }
<span>{ label }</span>
</HStack>
);
}
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( viewType ) {
const { records: authors, isResolving: isLoadingAuthors } =
useEntityRecords( 'root', 'user', { per_page: -1 } );
const { frontPageId, postsPageId } = useSelect( ( select ) => {
const { getEntityRecord } = select( coreStore );
const siteSettings = getEntityRecord( 'root', 'site' );
return {
frontPageId: siteSettings?.page_on_front,
postsPageId: siteSettings?.page_for_posts,
};
}, [] );
const fields = useMemo(
() => [
{
id: 'featured-image',
label: __( 'Featured Image' ),
getValue: ( { item } ) => item.featured_media,
render: ( { item } ) => (
<FeaturedImage item={ item } viewType={ viewType } />
),
enableSorting: false,
},
{
label: __( 'Title' ),
id: 'title',
type: 'text',
getValue: ( { item } ) =>
typeof item.title === 'string'
? item.title
: item.title?.raw,
render: ( { item } ) => {
const addLink =
[ LAYOUT_TABLE, LAYOUT_GRID ].includes( viewType ) &&
item.status !== 'trash';
const renderedTitle =
typeof item.title === 'string'
? item.title
: item.title?.rendered;
const title = addLink ? (
<Link
params={ {
postId: item.id,
postType: item.type,
canvas: 'edit',
} }
>
{ decodeEntities( renderedTitle ) ||
__( '(no title)' ) }
</Link>
) : (
<span>
{ decodeEntities( renderedTitle ) ||
__( '(no title)' ) }
</span>
);
let suffix = '';
if ( item.id === frontPageId ) {
suffix = (
<span className="edit-site-post-list__title-badge">
{ __( 'Homepage' ) }
</span>
);
} else if ( item.id === postsPageId ) {
suffix = (
<span className="edit-site-post-list__title-badge">
{ __( 'Posts Page' ) }
</span>
);
}
return (
<HStack
className="edit-site-post-list__title"
alignment="center"
justify="flex-start"
>
{ title }
{ suffix }
</HStack>
);
},
enableHiding: false,
},
{
label: __( 'Author' ),
id: 'author',
type: 'integer',
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' ),
id: 'status',
type: 'text',
elements: STATUSES,
render: PostStatusField,
Edit: 'radio',
enableSorting: false,
filterBy: {
operators: [ OPERATOR_IS_ANY ],
},
},
{
label: __( 'Date' ),
id: 'date',
type: 'datetime',
render: ( { item } ) => {
const isDraftOrPrivate = [ 'draft', 'private' ].includes(
item.status
);
if ( isDraftOrPrivate ) {
return createInterpolateElement(
sprintf(
/* translators: %s: page creation date */
__( '<span>Modified: <time>%s</time></span>' ),
getFormattedDate( item.date )
),
{
span: <span />,
time: <time />,
}
);
}
const isScheduled = item.status === 'future';
if ( isScheduled ) {
return createInterpolateElement(
sprintf(
/* translators: %s: page creation date */
__( '<span>Scheduled: <time>%s</time></span>' ),
getFormattedDate( item.date )
),
{
span: <span />,
time: <time />,
}
);
}
const isPublished = item.status === 'publish';
if ( isPublished ) {
return createInterpolateElement(
sprintf(
/* translators: %s: page creation time */
__( '<span>Published: <time>%s</time></span>' ),
getFormattedDate( item.date )
),
{
span: <span />,
time: <time />,
}
);
}
// Pending posts show the modified date if it's newer.
const dateToDisplay =
getDate( item.modified ) > getDate( item.date )
? item.modified
: item.date;
const isPending = item.status === 'pending';
if ( isPending ) {
return createInterpolateElement(
sprintf(
/* translators: %s: the newest of created or modified date for the page */
__( '<span>Modified: <time>%s</time></span>' ),
getFormattedDate( dateToDisplay )
),
{
span: <span />,
time: <time />,
}
);
}
// Unknow status.
return <time>{ getFormattedDate( item.date ) }</time>;
},
},
{
id: 'comment_status',
label: __( 'Discussion' ),
type: 'text',
Edit: 'radio',
enableSorting: false,
filterBy: {
operators: [],
},
elements: [
{
value: 'open',
label: __( 'Open' ),
description: __(
'Visitors can add new comments and replies.'
),
},
{
value: 'closed',
label: __( 'Closed' ),
description: __(
'Visitors cannot add new comments or replies. Existing comments remain visible.'
),
},
],
},
],
[ authors, viewType, frontPageId, postsPageId ]
);
return {
isLoading: isLoadingAuthors,
fields,
};
}
export default usePostFields;