Skip to content
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

Update: Pages: Trash view should default to table layout try 2. #63652

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading