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

DataViews: consider layout url parameter when loading a default/custom view #64306

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
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
35 changes: 21 additions & 14 deletions packages/edit-site/src/components/post-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,22 @@ function useView( postType ) {
[ activeView, isCustom ]
);
const [ view, setView ] = useState( () => {
let initialView;
if ( isCustom === 'true' ) {
return (
getCustomView( editedEntityRecord ) ?? {
type: layout ?? LAYOUT_LIST,
}
);
}
return (
getDefaultView( defaultViews, activeView ) ?? {
initialView = getCustomView( editedEntityRecord ) ?? {
type: layout ?? LAYOUT_LIST,
}
);
};
} else {
initialView = getDefaultView( defaultViews, activeView ) ?? {
type: layout ?? LAYOUT_LIST,
};
}

const type = layout ?? initialView.type;
return {
...initialView,
type,
};
} );

const setViewWithUrlUpdate = useCallback(
Expand Down Expand Up @@ -146,8 +150,7 @@ function useView( postType ) {
} ) );
}, [ layout ] );

// When activeView or isCustom URL parameters change,
// reset the view & update the layout URL param to match the view's type.
// When activeView or isCustom URL parameters change, reset the view.
useEffect( () => {
let newView;
if ( isCustom === 'true' ) {
Expand All @@ -157,9 +160,13 @@ function useView( postType ) {
}

if ( newView ) {
setViewWithUrlUpdate( newView );
const type = layout ?? newView.type;
setView( {
...newView,
type,
} );
}
}, [ activeView, isCustom, defaultViews, editedEntityRecord ] );
}, [ activeView, isCustom, layout, defaultViews, editedEntityRecord ] );

return [ view, setViewWithUrlUpdate, setViewWithUrlUpdate ];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function DataViewItem( {
suffix,
} ) {
const {
params: { postType, layout },
params: { postType },
} = useLocation();

const iconToUse =
Expand All @@ -41,7 +41,7 @@ export default function DataViewItem( {
}
const linkInfo = useLink( {
postType,
layout,
layout: type,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if we drop this argument entirely. I think it's not needed right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is. Otherwise, the dataview & the site editor's layout get out of sync (in the video below, the default trash view & the custom ThemeBuster view use table, but the site editor's layout is not updated accordingly) :

Gravacao.do.ecra.2024-08-07.as.13.55.54.mov

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the router assumes the default value is "list" if the layout is not present.

activeView,
isCustom: isCustom ? 'true' : undefined,
} );
Expand Down
Loading