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

Posts DataViews: Refactor the router to use route registration #67160

Merged
merged 1 commit into from
Nov 21, 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
36 changes: 36 additions & 0 deletions packages/edit-site/src/components/posts-app-routes/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* WordPress dependencies
*/
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
* Internal dependencies
*/
import Editor from '../editor';
import SidebarNavigationScreenMain from '../sidebar-navigation-screen-main';
import { unlock } from '../../lock-unlock';

const { useLocation } = unlock( routerPrivateApis );

function HomeMobileView() {
const { params = {} } = useLocation();
const { canvas = 'view' } = params;

return canvas === 'edit' ? (
<Editor isPostsList />
) : (
<SidebarNavigationScreenMain />
);
}

export const homeRoute = {
name: 'home',
match: () => {
return true;
},
areas: {
sidebar: <SidebarNavigationScreenMain />,
preview: <Editor isPostsList />,
mobile: HomeMobileView,
},
};
36 changes: 36 additions & 0 deletions packages/edit-site/src/components/posts-app-routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* WordPress dependencies
*/
import { useRegistry, useDispatch } from '@wordpress/data';
import { useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { store as siteEditorStore } from '../../store';
import { homeRoute } from './home';
import { postsListViewQuickEditRoute } from './posts-list-view-quick-edit';
import { postsListViewRoute } from './posts-list-view';
import { postsViewQuickEditRoute } from './posts-view-quick-edit';
import { postsViewRoute } from './posts-view';
import { postsEditRoute } from './posts-edit';

const routes = [
postsListViewQuickEditRoute,
postsListViewRoute,
postsViewQuickEditRoute,
postsViewRoute,
postsEditRoute,
homeRoute,
];

export function useRegisterPostsAppRoutes() {
const registry = useRegistry();
const { registerRoute } = unlock( useDispatch( siteEditorStore ) );
useEffect( () => {
registry.batch( () => {
routes.forEach( registerRoute );
} );
}, [ registry, registerRoute ] );
}
31 changes: 31 additions & 0 deletions packages/edit-site/src/components/posts-app-routes/posts-edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import PostList from '../post-list';
import DataViewsSidebarContent from '../sidebar-dataviews';
import SidebarNavigationScreen from '../sidebar-navigation-screen';
import Editor from '../editor';

export const postsEditRoute = {
name: 'posts-edit',
match: ( params ) => {
return params.postType === 'post' && params.canvas === 'edit';
},
areas: {
sidebar: (
<SidebarNavigationScreen
title={ __( 'Posts' ) }
isRoot
content={ <DataViewsSidebarContent /> }
/>
),
content: <PostList postType="post" />,
mobile: <Editor isPostsList />,
preview: <Editor isPostsList />,
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
* Internal dependencies
*/
import PostList from '../post-list';
import DataViewsSidebarContent from '../sidebar-dataviews';
import SidebarNavigationScreen from '../sidebar-navigation-screen';
import { unlock } from '../../lock-unlock';
import { PostEdit } from '../post-edit';
import Editor from '../editor';

const { useLocation } = unlock( routerPrivateApis );

function PostQuickEdit() {
const { params } = useLocation();
return <PostEdit postType="post" postId={ params.postId } />;
}

export const postsListViewQuickEditRoute = {
name: 'posts-list-view-quick-edit',
match: ( params ) => {
return (
params.isCustom !== 'true' &&
( params.layout ?? 'list' ) === 'list' &&
!! params.quickEdit &&
params.postType === 'post' &&
params.canvas !== 'edit'
);
},
areas: {
sidebar: (
<SidebarNavigationScreen
title={ __( 'Posts' ) }
isRoot
content={ <DataViewsSidebarContent /> }
/>
),
content: <PostList postType="post" />,
mobile: <PostList postType="post" />,
preview: <Editor />,
edit: <PostQuickEdit />,
},
widths: {
content: 380,
edit: 380,
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import PostList from '../post-list';
import DataViewsSidebarContent from '../sidebar-dataviews';
import SidebarNavigationScreen from '../sidebar-navigation-screen';
import Editor from '../editor';

export const postsListViewRoute = {
name: 'posts-list-view',
match: ( params ) => {
return (
params.isCustom !== 'true' &&
( params.layout ?? 'list' ) === 'list' &&
! params.quickEdit &&
params.postType === 'post' &&
params.canvas !== 'edit'
);
},
areas: {
sidebar: (
<SidebarNavigationScreen
title={ __( 'Posts' ) }
isRoot
content={ <DataViewsSidebarContent /> }
/>
),
content: <PostList postType="post" />,
preview: <Editor isPostsList />,
mobile: <PostList postType="post" />,
},
widths: {
content: 380,
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
* Internal dependencies
*/
import PostList from '../post-list';
import DataViewsSidebarContent from '../sidebar-dataviews';
import SidebarNavigationScreen from '../sidebar-navigation-screen';
import { unlock } from '../../lock-unlock';
import { PostEdit } from '../post-edit';

const { useLocation } = unlock( routerPrivateApis );

function PostQuickEdit() {
const { params } = useLocation();
return <PostEdit postType="post" postId={ params.postId } />;
}

export const postsViewQuickEditRoute = {
name: 'posts-view-quick-edit',
match: ( params ) => {
return (
( params.isCustom === 'true' ||
( params.layout ?? 'list' ) !== 'list' ) &&
!! params.quickEdit &&
params.postType === 'post' &&
params.canvas !== 'edit'
);
},
areas: {
sidebar: (
<SidebarNavigationScreen
title={ __( 'Posts' ) }
isRoot
content={ <DataViewsSidebarContent /> }
/>
),
content: <PostList postType="post" />,
mobile: <PostList postType="post" />,
edit: <PostQuickEdit />,
},
widths: {
edit: 380,
},
};
35 changes: 35 additions & 0 deletions packages/edit-site/src/components/posts-app-routes/posts-view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import PostList from '../post-list';
import DataViewsSidebarContent from '../sidebar-dataviews';
import SidebarNavigationScreen from '../sidebar-navigation-screen';

export const postsViewRoute = {
name: 'posts-view',
match: ( params ) => {
return (
( params.isCustom === 'true' ||
( params.layout ?? 'list' ) !== 'list' ) &&
! params.quickEdit &&
params.postType === 'post' &&
params.canvas !== 'edit'
);
},
areas: {
sidebar: (
<SidebarNavigationScreen
title={ __( 'Posts' ) }
isRoot
content={ <DataViewsSidebarContent /> }
/>
),
content: <PostList postType="post" />,
mobile: <PostList postType="post" />,
},
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another interesting thing is that all these routes are the exact same ones that exist in the site editor, there are just very small differences:

  • postType is "post" instead of "page"
  • isRoot in the sidebar instead of backPath={} (this is just temporary because the posts page doesn't have categories and tags yet so it's top level)
  • isPostsPage argument passed to the Editor component.

This is a good thing and an indication that we can easily support custom post types with the same routes (by making the post type dynamic)

4 changes: 3 additions & 1 deletion packages/edit-site/src/components/posts-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import { privateApis as routerPrivateApis } from '@wordpress/router';
* Internal dependencies
*/
import Layout from '../layout';
import useActiveRoute from './router';
import { useRegisterPostsAppRoutes } from '../posts-app-routes';
import { unlock } from '../../lock-unlock';
import useActiveRoute from '../layout/router';

const { RouterProvider } = unlock( routerPrivateApis );
const { GlobalStylesProvider } = unlock( editorPrivateApis );

function PostsLayout() {
useRegisterPostsAppRoutes();
const route = useActiveRoute();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

One interesting thing here, is that now this hook is the exact same one used in the site editor.

return <Layout route={ route } />;
}
Expand Down
69 changes: 0 additions & 69 deletions packages/edit-site/src/components/posts-app/router.js

This file was deleted.

Loading