-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
193 lines (180 loc) · 5.47 KB
/
index.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
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useState, useMemo, useId, useEffect } from '@wordpress/element';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import { DataViews, filterSortAndPaginate } from '@wordpress/dataviews';
import { usePrevious } from '@wordpress/compose';
import { useEntityRecords } from '@wordpress/core-data';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
import { privateApis as routerPrivateApis } from '@wordpress/router';
/**
* Internal dependencies
*/
import Page from '../page';
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 PatternsHeader from './header';
import { useEditPostAction } from '../dataviews-actions';
import {
patternStatusField,
previewField,
titleField,
templatePartAuthorField,
} from './fields';
const { ExperimentalBlockEditorProvider } = unlock( blockEditorPrivateApis );
const { usePostActions } = unlock( editorPrivateApis );
const { useLocation } = unlock( routerPrivateApis );
const EMPTY_ARRAY = [];
const defaultLayouts = {
[ LAYOUT_TABLE ]: {
layout: {
primaryField: 'title',
styles: {
preview: {
width: '1%',
},
author: {
width: '1%',
},
},
},
},
[ LAYOUT_GRID ]: {
layout: {
mediaField: 'preview',
primaryField: 'title',
badgeFields: [ 'sync-status' ],
},
},
};
const DEFAULT_VIEW = {
type: LAYOUT_GRID,
search: '',
page: 1,
perPage: 20,
layout: defaultLayouts[ LAYOUT_GRID ].layout,
fields: [ 'title', 'sync-status' ],
filters: [],
};
export default function DataviewsPatterns() {
const {
query: { postType = 'wp_block', categoryId: categoryIdFromURL },
} = useLocation();
const categoryId = categoryIdFromURL || PATTERN_DEFAULT_CATEGORY;
const [ view, setView ] = useState( DEFAULT_VIEW );
const previousCategoryId = usePrevious( categoryId );
const previousPostType = usePrevious( postType );
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, titleField ];
if ( postType === PATTERN_TYPES.user ) {
_fields.push( patternStatusField );
} else if ( postType === TEMPLATE_PART_POST_TYPE ) {
_fields.push( {
...templatePartAuthorField,
elements: authors,
} );
}
return _fields;
}, [ postType, authors ] );
// Reset the page number when the category changes.
useEffect( () => {
if (
previousCategoryId !== categoryId ||
previousPostType !== postType
) {
setView( ( prevView ) => ( { ...prevView, page: 1 } ) );
}
}, [ categoryId, previousCategoryId, previousPostType, postType ] );
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 id = useId();
const settings = usePatternSettings();
// 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
title={ __( 'Patterns content' ) }
className="edit-site-page-patterns-dataviews"
hideTitleFromUI
>
<PatternsHeader
categoryId={ categoryId }
type={ postType }
titleId={ `${ id }-title` }
descriptionId={ `${ id }-description` }
/>
<DataViews
key={ categoryId + postType }
paginationInfo={ paginationInfo }
fields={ fields }
actions={ actions }
data={ dataWithPermissions || EMPTY_ARRAY }
getItemId={ ( item ) => item.name ?? item.id }
isLoading={ isResolving }
view={ view }
onChangeView={ setView }
defaultLayouts={ defaultLayouts }
/>
</Page>
</ExperimentalBlockEditorProvider>
);
}