-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DataViews Sidebar: Display item count on DataViews sidebar #65223
Changes from all commits
2281876
bdda3f2
6ef55f6
b68bc0c
d0df94a
982e752
71517b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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'; | ||||
|
||||
/** | ||||
|
@@ -68,6 +68,50 @@ const DEFAULT_POST_BASE = { | |||
layout: defaultLayouts[ LAYOUT_LIST ].layout, | ||||
}; | ||||
|
||||
export function useDefaultViewsWithItemCounts( { postType } ) { | ||||
const defaultViews = useDefaultViews( { postType } ); | ||||
const { records, totalItems } = useEntityRecords( 'postType', postType, { | ||||
per_page: -1, | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I probably followed the same rabbit hole as @Souptik2001 here. Unfortunately I couldn't find another way either.
Ideally, I guess, there'd be an options request with a limited database query |
||||
status: [ 'any', 'trash' ], | ||||
} ); | ||||
|
||||
return useMemo( () => { | ||||
if ( ! defaultViews ) { | ||||
return []; | ||||
} | ||||
|
||||
// If there are no records, return the default views with no counts. | ||||
if ( ! records ) { | ||||
return defaultViews; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The list will load faster than the count, so let's return that while we wait. |
||||
} | ||||
|
||||
const counts = { | ||||
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, | ||||
}; | ||||
|
||||
// All items excluding trashed items as per the default "all" status query. | ||||
counts.all = totalItems ? totalItems - counts.trash : 0; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove trash items from the all items count as per the default view (e.g., for pages:
|
||||
|
||||
// Filter out views with > 0 item counts. | ||||
return defaultViews.map( ( _view ) => { | ||||
_view.count = counts[ _view.slug ]; | ||||
return _view; | ||||
} ); | ||||
}, [ defaultViews, records, totalItems ] ); | ||||
} | ||||
|
||||
export function useDefaultViews( { postType } ) { | ||||
const labels = useSelect( | ||||
( select ) => { | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ import { privateApis as routerPrivateApis } from '@wordpress/router'; | |
/** | ||
* Internal dependencies | ||
*/ | ||
import { useDefaultViews } from './default-views'; | ||
import { useDefaultViewsWithItemCounts } from './default-views'; | ||
import { unlock } from '../../lock-unlock'; | ||
import DataViewItem from './dataview-item'; | ||
import CustomDataViewsList from './custom-dataviews-list'; | ||
|
@@ -18,7 +18,9 @@ export default function DataViewsSidebarContent() { | |
const { | ||
params: { postType, activeView = 'all', isCustom = 'false' }, | ||
} = useLocation(); | ||
const defaultViews = useDefaultViews( { postType } ); | ||
|
||
const defaultViews = useDefaultViewsWithItemCounts( { postType } ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created a new hook because |
||
|
||
if ( ! postType ) { | ||
return null; | ||
} | ||
|
@@ -34,6 +36,9 @@ export default function DataViewsSidebarContent() { | |
slug={ dataview.slug } | ||
title={ dataview.title } | ||
icon={ dataview.icon } | ||
navigationItemSuffix={ | ||
<span>{ dataview.count }</span> | ||
} | ||
type={ dataview.view.type } | ||
isActive={ | ||
! isCustomBoolean && | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,10 @@ | |
min-width: initial; | ||
} | ||
|
||
.edit-site-sidebar-navigation-item.with-suffix { | ||
padding-right: $grid-unit-10; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to make sure the rhs padding matches the pattern view's items. |
||
|
||
&:hover, | ||
&:focus, | ||
&[aria-current] { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a new prop because:
What might be a better refactor after this PR, is for
<DataViewItem />
to take a sidebar item props object or something like that, which only applies to the child<SidebarNavigationItem />
e.g.,