-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathindex.js
More file actions
252 lines (236 loc) · 7 KB
/
index.js
File metadata and controls
252 lines (236 loc) · 7 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
* WordPress dependencies
*/
import { Page } from '@wordpress/admin-ui';
import { __ } from '@wordpress/i18n';
import { useMemo } from '@wordpress/element';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import { DataViews, filterSortAndPaginate } from '@wordpress/dataviews';
import { useEntityRecords, store as coreStore } from '@wordpress/core-data';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useView } from '@wordpress/views';
import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import {
LAYOUT_GRID,
LAYOUT_TABLE,
PATTERN_TYPES,
TEMPLATE_PART_POST_TYPE,
PATTERN_DEFAULT_CATEGORY,
} from '../../utils/constants';
import usePatternSettings from './use-pattern-settings';
import { unlock } from '../../lock-unlock';
import usePatterns, { useAugmentPatternsWithPermissions } from './use-patterns';
import PatternsActions from './actions';
import { useEditPostAction } from '../dataviews-actions';
import {
patternStatusField,
previewField,
templatePartAuthorField,
} from './fields';
import { addQueryArgs } from '@wordpress/url';
import usePatternCategories from '../sidebar-navigation-screen-patterns/use-pattern-categories';
import { Button } from '@wordpress/components';
const { ExperimentalBlockEditorProvider } = unlock( blockEditorPrivateApis );
const { usePostActions, patternTitleField } = unlock( editorPrivateApis );
const { useLocation, useHistory } = unlock( routerPrivateApis );
const EMPTY_ARRAY = [];
const defaultLayouts = {
[ LAYOUT_TABLE ]: {
layout: {
styles: {
author: {
width: '1%',
},
},
},
},
[ LAYOUT_GRID ]: {
layout: {
badgeFields: [ 'sync-status' ],
},
},
};
const DEFAULT_VIEW = {
type: LAYOUT_GRID,
perPage: 20,
titleField: 'title',
mediaField: 'preview',
fields: [ 'sync-status' ],
filters: [],
...defaultLayouts[ LAYOUT_GRID ],
};
function usePagePatternsHeader( type, categoryId ) {
const { patternCategories } = usePatternCategories();
const templatePartAreas = useSelect(
( select ) =>
select( coreStore ).getCurrentTheme()
?.default_template_part_areas || [],
[]
);
let title, description, patternCategory;
if ( type === TEMPLATE_PART_POST_TYPE ) {
const templatePartArea = templatePartAreas.find(
( area ) => area.area === categoryId
);
title = templatePartArea?.label || __( 'All Template Parts' );
description =
templatePartArea?.description ||
__( 'Includes every template part defined for any area.' );
} else if ( type === PATTERN_TYPES.user && !! categoryId ) {
patternCategory = patternCategories.find(
( category ) => category.name === categoryId
);
title = patternCategory?.label;
description = patternCategory?.description;
}
return { title, description };
}
export default function DataviewsPatterns() {
const { path, query } = useLocation();
const { postType = 'wp_block', categoryId: categoryIdFromURL } = query;
const history = useHistory();
const categoryId = categoryIdFromURL || PATTERN_DEFAULT_CATEGORY;
const { view, updateView, isModified, resetToDefault } = useView( {
kind: 'postType',
name: postType,
slug: categoryId,
defaultView: DEFAULT_VIEW,
queryParams: {
page: Number( query.pageNumber ?? 1 ),
search: query.search,
},
onChangeQueryParams: ( params ) => {
history.navigate(
addQueryArgs( path, {
...query,
pageNumber: params.page,
search: params.search,
} )
);
},
} );
const viewSyncStatus = view.filters?.find(
( { field } ) => field === 'sync-status'
)?.value;
const { patterns, isResolving } = usePatterns( postType, categoryId, {
search: view.search,
syncStatus: viewSyncStatus,
} );
const { records } = useEntityRecords( 'postType', TEMPLATE_PART_POST_TYPE, {
per_page: -1,
} );
const authors = useMemo( () => {
if ( ! records ) {
return EMPTY_ARRAY;
}
const authorsSet = new Set();
records.forEach( ( template ) => {
authorsSet.add( template.author_text );
} );
return Array.from( authorsSet ).map( ( author ) => ( {
value: author,
label: author,
} ) );
}, [ records ] );
const fields = useMemo( () => {
const _fields = [ previewField, patternTitleField ];
if ( postType === PATTERN_TYPES.user ) {
_fields.push( patternStatusField );
} else if ( postType === TEMPLATE_PART_POST_TYPE ) {
_fields.push( {
...templatePartAuthorField,
elements: authors,
} );
}
return _fields;
}, [ postType, authors ] );
const { data, paginationInfo } = useMemo( () => {
// Search is managed server-side as well as filters for patterns.
// However, the author filter in template parts is done client-side.
const viewWithoutFilters = { ...view };
delete viewWithoutFilters.search;
if ( postType !== TEMPLATE_PART_POST_TYPE ) {
viewWithoutFilters.filters = [];
}
return filterSortAndPaginate( patterns, viewWithoutFilters, fields );
}, [ patterns, view, fields, postType ] );
const dataWithPermissions = useAugmentPatternsWithPermissions( data );
const templatePartActions = usePostActions( {
postType: TEMPLATE_PART_POST_TYPE,
context: 'list',
} );
const patternActions = usePostActions( {
postType: PATTERN_TYPES.user,
context: 'list',
} );
const editAction = useEditPostAction();
const actions = useMemo( () => {
if ( postType === TEMPLATE_PART_POST_TYPE ) {
return [ editAction, ...templatePartActions ].filter( Boolean );
}
return [ editAction, ...patternActions ].filter( Boolean );
}, [ editAction, postType, templatePartActions, patternActions ] );
const settings = usePatternSettings();
const { title, description } = usePagePatternsHeader(
postType,
categoryId
);
// Wrap everything in a block editor provider.
// This ensures 'styles' that are needed for the previews are synced
// from the site editor store to the block editor store.
return (
<ExperimentalBlockEditorProvider settings={ settings }>
<Page
className="edit-site-page-patterns-dataviews"
title={ title }
subTitle={ description }
hasPadding
actions={
<>
{ isModified && (
<Button
__next40pxDefaultSize
onClick={ resetToDefault }
>
{ __( 'Reset view' ) }
</Button>
) }
<PatternsActions />
</>
}
>
<DataViews
key={ categoryId + postType }
paginationInfo={ paginationInfo }
fields={ fields }
actions={ actions }
data={ dataWithPermissions || EMPTY_ARRAY }
getItemId={ ( item ) => item.name ?? item.id }
isLoading={ isResolving }
isItemClickable={ ( item ) =>
item.type !== PATTERN_TYPES.theme
}
onClickItem={ ( item ) => {
history.navigate(
`/${ item.type }/${
[
PATTERN_TYPES.user,
TEMPLATE_PART_POST_TYPE,
].includes( item.type )
? item.id
: item.name
}?canvas=edit`
);
} }
view={ view }
onChangeView={ updateView }
defaultLayouts={ defaultLayouts }
/>
</Page>
</ExperimentalBlockEditorProvider>
);
}