Skip to content

Commit

Permalink
DataViews: consider layout url parameter when loading a default/custo…
Browse files Browse the repository at this point in the history
…m view (#64306)

Co-authored-by: oandregal <oandregal@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
  • Loading branch information
3 people authored Aug 7, 2024
1 parent e6627e0 commit ab103f2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
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,
activeView,
isCustom: isCustom ? 'true' : undefined,
} );
Expand Down

0 comments on commit ab103f2

Please sign in to comment.