Skip to content

Commit

Permalink
Pulling across work from #62028 and using useEntityRecords to get rec…
Browse files Browse the repository at this point in the history
…ord counts
  • Loading branch information
ramonjd committed Sep 11, 2024
1 parent 07c7821 commit 1f6ccf6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
notAllowed,
} from '@wordpress/icons';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { store as coreStore, useEntityRecords } from '@wordpress/core-data';
import { useMemo } from '@wordpress/element';

/**
Expand All @@ -24,6 +24,7 @@ import {
LAYOUT_TABLE,
LAYOUT_GRID,
OPERATOR_IS_ANY,
OPERATOR_IS_NONE,
} from '../../utils/constants';

export const defaultLayouts = {
Expand Down Expand Up @@ -68,21 +69,58 @@ const DEFAULT_POST_BASE = {
layout: defaultLayouts[ LAYOUT_LIST ].layout,
};

function useDataViewItemCounts( { postType } ) {
const { records, totalItems } = useEntityRecords( 'postType', postType, {
status: [ 'any', 'trash' ],
} );
return useMemo( () => {
if ( ! records ) {
return {};
}
const counts = {
all: totalItems,
drafts: records.filter( ( record ) => record.status === 'draft' )
.length,
future: records.filter( ( record ) => record.status === 'future' )
.length,
pending: records.filter( ( record ) => record.status === 'pending' )
.length,
private: records.filter( ( record ) => record.status === 'private' )
.length,
published: records.filter(
( record ) => record.status === 'publish'
).length,
trash: records.filter( ( record ) => record.status === 'trash' )
.length,
};
return counts;
}, [ records, totalItems ] );
}

export function useDefaultViews( { postType } ) {
const labels = useSelect(
( select ) => {
const { getPostType } = select( coreStore );
return getPostType( postType )?.labels;
},
( select ) => select( coreStore ).getPostType( postType )?.labels,
[ postType ]
);
const counts = useDataViewItemCounts( { postType } );

return useMemo( () => {
return [
{
title: labels?.all_items || __( 'All items' ),
slug: 'all',
icon: pages,
view: DEFAULT_POST_BASE,
view: {
...DEFAULT_POST_BASE,
filters: [
{
field: 'status',
operator: OPERATOR_IS_NONE,
value: 'trash',
},
],
},
count: counts?.all,
},
{
title: __( 'Published' ),
Expand All @@ -98,6 +136,7 @@ export function useDefaultViews( { postType } ) {
},
],
},
count: counts?.published,
},
{
title: __( 'Scheduled' ),
Expand All @@ -113,6 +152,7 @@ export function useDefaultViews( { postType } ) {
},
],
},
count: counts?.future,
},
{
title: __( 'Drafts' ),
Expand All @@ -128,6 +168,7 @@ export function useDefaultViews( { postType } ) {
},
],
},
count: counts?.drafts,
},
{
title: __( 'Pending' ),
Expand All @@ -143,6 +184,7 @@ export function useDefaultViews( { postType } ) {
},
],
},
count: counts?.pending,
},
{
title: __( 'Private' ),
Expand All @@ -158,6 +200,7 @@ export function useDefaultViews( { postType } ) {
},
],
},
count: counts?.private,
},
{
title: __( 'Trash' ),
Expand All @@ -175,7 +218,8 @@ export function useDefaultViews( { postType } ) {
},
],
},
count: counts?.trash,
},
];
}, [ labels ] );
].filter( ( { count } ) => count > 0 );
}, [ labels, counts ] );
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default function DataViewsSidebarContent() {
slug={ dataview.slug }
title={ dataview.title }
icon={ dataview.icon }
suffix={ <span>{ dataview.count }</span> }
type={ dataview.view.type }
isActive={
! isCustomBoolean &&
Expand Down

0 comments on commit 1f6ccf6

Please sign in to comment.