diff --git a/packages/edit-site/src/components/sidebar-dataviews/dataview-item.js b/packages/edit-site/src/components/sidebar-dataviews/dataview-item.js index e6ebd5b1f8ee8..3bfcd038fc962 100644 --- a/packages/edit-site/src/components/sidebar-dataviews/dataview-item.js +++ b/packages/edit-site/src/components/sidebar-dataviews/dataview-item.js @@ -21,6 +21,7 @@ const { useLocation } = unlock( routerPrivateApis ); export default function DataViewItem( { title, slug, + count, customViewId, type, icon, @@ -51,6 +52,7 @@ export default function DataViewItem( { { count } } aria-current={ isActive ? 'true' : undefined } > { title } diff --git a/packages/edit-site/src/components/sidebar-dataviews/default-views.js b/packages/edit-site/src/components/sidebar-dataviews/default-views.js index 6a10e0cd95d7b..299903a6f494f 100644 --- a/packages/edit-site/src/components/sidebar-dataviews/default-views.js +++ b/packages/edit-site/src/components/sidebar-dataviews/default-views.js @@ -12,6 +12,7 @@ import { LAYOUT_TABLE, LAYOUT_GRID, OPERATOR_IS_ANY, + OPERATOR_IS_NONE, } from '../../utils/constants'; export const DEFAULT_CONFIG_PER_VIEW_TYPE = { @@ -52,7 +53,16 @@ export const DEFAULT_VIEWS = { title: __( 'All pages' ), slug: 'all', icon: pages, - view: DEFAULT_PAGE_BASE, + view: { + ...DEFAULT_PAGE_BASE, + filters: [ + { + field: 'status', + operator: OPERATOR_IS_NONE, + value: 'trash', + }, + ], + }, }, { title: __( 'Published' ), diff --git a/packages/edit-site/src/components/sidebar-dataviews/index.js b/packages/edit-site/src/components/sidebar-dataviews/index.js index 9362308172f89..6848b4a86f6a7 100644 --- a/packages/edit-site/src/components/sidebar-dataviews/index.js +++ b/packages/edit-site/src/components/sidebar-dataviews/index.js @@ -8,16 +8,17 @@ import { privateApis as routerPrivateApis } from '@wordpress/router'; * Internal dependencies */ -import { DEFAULT_VIEWS } from './default-views'; import { unlock } from '../../lock-unlock'; const { useLocation } = unlock( routerPrivateApis ); import DataViewItem from './dataview-item'; import CustomDataViewsList from './custom-dataviews-list'; +import useDataViews from './use-data-views'; export default function DataViewsSidebarContent() { const { params: { postType, activeView = 'all', isCustom = 'false' }, } = useLocation(); + const views = useDataViews( postType ); if ( ! postType ) { return null; } @@ -26,11 +27,12 @@ export default function DataViewsSidebarContent() { return ( <> - { DEFAULT_VIEWS[ postType ].map( ( dataview ) => { + { views.map( ( dataview ) => { return ( { + return DEFAULT_VIEWS[ postType ].map( ( view ) => { + const viewFilters = view?.view?.filters; + // Filter the records matching the view filters. + view.records = data?.filter( ( record ) => { + // Check if the record matches all the filters. + return viewFilters.every( ( filter ) => { + let filterValues = filter.value; + const filterField = filter.field; + const filterOperator = filter.operator; + + // Convert string to array value if required. + if ( ! Array.isArray( filterValues ) ) { + filterValues = [ filterValues ]; + } + + if ( filterOperator === OPERATOR_IS_ANY ) { + return filterValues.includes( record[ filterField ] ); + } + + if ( filterOperator === OPERATOR_IS_NONE ) { + return ! filterValues.includes( record[ filterField ] ); + } + + return true; + } ); + } ); + + return view; + } ); + }, [ data, postType ] ); + + return views; +}