From e4ed9593fbc4979be674cea22745fbc8bc015b7c Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Wed, 17 Jul 2024 14:40:08 +0200 Subject: [PATCH] Update: Pages: Trash view should default to table layout try 2. (#63652) Co-authored-by: jorgefilipecosta Co-authored-by: jameskoster --- .../src/components/sidebar-dataviews/index.js | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/edit-site/src/components/sidebar-dataviews/index.js b/packages/edit-site/src/components/sidebar-dataviews/index.js index 37f16b2e1f68bc..e0a5b08a028f07 100644 --- a/packages/edit-site/src/components/sidebar-dataviews/index.js +++ b/packages/edit-site/src/components/sidebar-dataviews/index.js @@ -3,20 +3,55 @@ */ import { __experimentalItemGroup as ItemGroup } from '@wordpress/components'; import { privateApis as routerPrivateApis } from '@wordpress/router'; +import { useRef, useEffect } from '@wordpress/element'; +import { usePrevious } from '@wordpress/compose'; /** * Internal dependencies */ import { useDefaultViews } from './default-views'; import { unlock } from '../../lock-unlock'; -const { useLocation } = unlock( routerPrivateApis ); import DataViewItem from './dataview-item'; import CustomDataViewsList from './custom-dataviews-list'; +const { useLocation, useHistory } = unlock( routerPrivateApis ); + +/** + * Hook to switch to table layout when switching to the trash view. + * When going out of the trash view, it switches back to the previous layout if + * there was an automatic switch to table layout. + */ +function useSwitchToTableOnTrash() { + const { + params: { activeView, layout, ...restParams }, + } = useLocation(); + const history = useHistory(); + const viewToSwitchOutOfTrash = useRef( undefined ); + const previousActiveView = usePrevious( activeView ); + useEffect( () => { + if ( activeView === 'trash' && previousActiveView !== 'trash' ) { + viewToSwitchOutOfTrash.current = layout || 'list'; + history.push( { ...restParams, layout: 'table', activeView } ); + } else if ( + previousActiveView === 'trash' && + activeView !== 'trash' && + viewToSwitchOutOfTrash.current + ) { + history.push( { + ...restParams, + layout: viewToSwitchOutOfTrash.current, + activeView, + } ); + viewToSwitchOutOfTrash.current = undefined; + } + }, [ previousActiveView, activeView, layout, history, restParams ] ); +} + export default function DataViewsSidebarContent() { const { params: { postType, activeView = 'all', isCustom = 'false' }, } = useLocation(); + useSwitchToTableOnTrash(); const DEFAULT_VIEWS = useDefaultViews( { postType } ); if ( ! postType ) { return null;