Skip to content

Commit

Permalink
Update: Pages: Trash view should default to table layout try 2. (#63652)
Browse files Browse the repository at this point in the history
Co-authored-by: jorgefilipecosta <[email protected]>
Co-authored-by: jameskoster <[email protected]>
  • Loading branch information
3 people authored Jul 17, 2024
1 parent cdf31e8 commit e4ed959
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion packages/edit-site/src/components/sidebar-dataviews/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e4ed959

Please sign in to comment.