import { store as coreDataStore } from '@wordpress/core-data';
import { useDispatch, useSelect } from '@wordpress/data';
import { DataViews, type Field, type View } from '@wordpress/dataviews';
import { dateI18n, getSettings, humanTimeDiff } from '@wordpress/date';
import { useCallback, useMemo, useState } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
import { __ } from '@wordpress/i18n';
import {
drafts as draftsIcon,
postFeaturedImage,
trash,
} from '@wordpress/icons';
import { EmptyState, IconButton, Link, Stack, Text, Icon } from '@wordpress/ui';
import { addQueryArgs } from '@wordpress/url';
import styles from './drafts-list.module.css';
type FeaturedMedia = {
source_url?: string;
media_details?: {
sizes?: Record< string, { source_url?: string } >;
};
};
type DraftPost = {
id: number;
title: { rendered: string };
/*
* The REST schema allows a null date. A draft created through the UI
* always has one, but a record inserted without `post_date` does not, and
* both date helpers below would render a bogus relative time for it.
*/
date: string | null;
_embedded?: {
'wp:featuredmedia'?: FeaturedMedia[];
};
};
/*
* Most recent drafts across the site, newest first. `_embed` pulls the
* featured media into each record in the same request, so the thumbnail needs
* no extra fetch per row.
*/
const DRAFTS_QUERY = {
status: 'draft',
orderby: 'date',
order: 'desc',
per_page: 20,
_embed: 'wp:featuredmedia',
};
const DEFAULT_LAYOUTS = { list: {} };
const INITIAL_VIEW: View = {
type: 'list',
page: 1,
perPage: DRAFTS_QUERY.per_page,
search: '',
filters: [],
fields: [],
titleField: 'title',
descriptionField: 'date',
mediaField: 'featured',
showMedia: true,
layout: { density: 'compact' },
};
function getEditUrl( postId: number ) {
return addQueryArgs( 'post.php', { post: postId, action: 'edit' } );
}
/* Prefers the small thumbnail size, falling back to larger ones, then full. */
function getThumbnailUrl( post: DraftPost ) {
const media = post._embedded?.[ 'wp:featuredmedia' ]?.[ 0 ];
const sizes = media?.media_details?.sizes;
return (
sizes?.thumbnail?.source_url ??
sizes?.medium?.source_url ??
media?.source_url
);
}
/* Featured image, or a placeholder where an inline upload could live (follow-up). */
function DraftThumbnail( { post }: { post: DraftPost } ) {
const url = getThumbnailUrl( post );
if ( url ) {
return (
);
}
return (