-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
router.js
209 lines (195 loc) · 5.19 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* WordPress dependencies
*/
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { __ } from '@wordpress/i18n';
import { useEffect } from '@wordpress/element';
/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { useIsSiteEditorLoading } from './hooks';
import Editor from '../editor';
import PagePages from '../page-pages';
import PagePatterns from '../page-patterns';
import PageTemplates from '../page-templates';
import SidebarNavigationScreen from '../sidebar-navigation-screen';
import SidebarNavigationScreenGlobalStyles from '../sidebar-navigation-screen-global-styles';
import SidebarNavigationScreenMain from '../sidebar-navigation-screen-main';
import SidebarNavigationScreenNavigationMenus from '../sidebar-navigation-screen-navigation-menus';
import SidebarNavigationScreenTemplatesBrowse from '../sidebar-navigation-screen-templates-browse';
import SidebarNavigationScreenPatterns from '../sidebar-navigation-screen-patterns';
import SidebarNavigationScreenNavigationMenu from '../sidebar-navigation-screen-navigation-menu';
import DataViewsSidebarContent from '../sidebar-dataviews';
import {
NAVIGATION_POST_TYPE,
PATTERN_TYPES,
TEMPLATE_PART_POST_TYPE,
TEMPLATE_POST_TYPE,
} from '../../utils/constants';
const { useLocation, useHistory } = unlock( routerPrivateApis );
function useRedirectOldPaths() {
const history = useHistory();
const { params } = useLocation();
useEffect( () => {
const { postType, path, categoryType, ...rest } = params;
if ( path === '/wp_template_part/all' ) {
history.replace( { postType: TEMPLATE_PART_POST_TYPE } );
}
if ( path === '/page' ) {
history.replace( {
postType: 'page',
...rest,
} );
}
if ( path === '/wp_template' ) {
history.replace( {
postType: TEMPLATE_POST_TYPE,
...rest,
} );
}
if ( path === '/patterns' ) {
history.replace( {
postType:
categoryType === TEMPLATE_PART_POST_TYPE
? TEMPLATE_PART_POST_TYPE
: PATTERN_TYPES.user,
...rest,
} );
}
if ( path === '/navigation' ) {
history.replace( {
postType: NAVIGATION_POST_TYPE,
...rest,
} );
}
}, [ history, params ] );
}
export default function useLayoutAreas() {
const isSiteEditorLoading = useIsSiteEditorLoading();
const { params } = useLocation();
const { postType, postId, path, layout, isCustom, canvas } = params;
useRedirectOldPaths();
// Page list
if ( postType === 'page' ) {
const isListLayout = layout === 'list' || ! layout;
return {
key: 'pages',
areas: {
sidebar: (
<SidebarNavigationScreen
title={ __( 'Pages' ) }
backPath={ {} }
content={ <DataViewsSidebarContent /> }
/>
),
content: <PagePages />,
preview: ( isListLayout || canvas === 'edit' ) && (
<Editor isLoading={ isSiteEditorLoading } />
),
mobile:
canvas === 'edit' ? (
<Editor isLoading={ isSiteEditorLoading } />
) : (
<PagePages />
),
},
widths: {
content: isListLayout ? 380 : undefined,
},
};
}
// Templates
if ( postType === TEMPLATE_POST_TYPE ) {
const isListLayout = isCustom !== 'true' && layout === 'list';
return {
key: 'templates',
areas: {
sidebar: (
<SidebarNavigationScreenTemplatesBrowse backPath={ {} } />
),
content: <PageTemplates />,
preview: ( isListLayout || canvas === 'edit' ) && (
<Editor isLoading={ isSiteEditorLoading } />
),
mobile: <PageTemplates />,
},
widths: {
content: isListLayout ? 380 : undefined,
},
};
}
// Patterns
if (
[ TEMPLATE_PART_POST_TYPE, PATTERN_TYPES.user ].includes( postType )
) {
return {
key: 'patterns',
areas: {
sidebar: <SidebarNavigationScreenPatterns backPath={ {} } />,
content: <PagePatterns />,
mobile: <PagePatterns />,
preview: canvas === 'edit' && (
<Editor isLoading={ isSiteEditorLoading } />
),
},
};
}
// Styles
if ( path === '/wp_global_styles' ) {
return {
key: 'styles',
areas: {
sidebar: (
<SidebarNavigationScreenGlobalStyles backPath={ {} } />
),
preview: <Editor isLoading={ isSiteEditorLoading } />,
mobile: canvas === 'edit' && (
<Editor isLoading={ isSiteEditorLoading } />
),
},
};
}
// Navigation
if ( postType === NAVIGATION_POST_TYPE ) {
if ( postId ) {
return {
key: 'navigation',
areas: {
sidebar: (
<SidebarNavigationScreenNavigationMenu
backPath={ { postType: NAVIGATION_POST_TYPE } }
/>
),
preview: <Editor isLoading={ isSiteEditorLoading } />,
mobile: canvas === 'edit' && (
<Editor isLoading={ isSiteEditorLoading } />
),
},
};
}
return {
key: 'navigation',
areas: {
sidebar: (
<SidebarNavigationScreenNavigationMenus backPath={ {} } />
),
preview: <Editor isLoading={ isSiteEditorLoading } />,
mobile: canvas === 'edit' && (
<Editor isLoading={ isSiteEditorLoading } />
),
},
};
}
// Fallback shows the home page preview
return {
key: 'default',
areas: {
sidebar: <SidebarNavigationScreenMain />,
preview: <Editor isLoading={ isSiteEditorLoading } />,
mobile: canvas === 'edit' && (
<Editor isLoading={ isSiteEditorLoading } />
),
},
};
}