-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
patterns-list.js
209 lines (191 loc) · 5.71 KB
/
patterns-list.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 { useState, useDeferredValue, useId, useMemo } from '@wordpress/element';
import {
SearchControl,
__experimentalVStack as VStack,
Flex,
FlexBlock,
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
__experimentalHeading as Heading,
__experimentalText as Text,
} from '@wordpress/components';
import { __, isRTL } from '@wordpress/i18n';
import { chevronLeft, chevronRight } from '@wordpress/icons';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useAsyncList, useViewportMatch } from '@wordpress/compose';
/**
* Internal dependencies
*/
import PatternsHeader from './header';
import Grid from './grid';
import NoPatterns from './no-patterns';
import usePatterns from './use-patterns';
import SidebarButton from '../sidebar-button';
import useDebouncedInput from '../../utils/use-debounced-input';
import { unlock } from '../../lock-unlock';
import { SYNC_TYPES, USER_PATTERN_CATEGORY, PATTERNS } from './utils';
import Pagination from './pagination';
const { useLocation, useHistory } = unlock( routerPrivateApis );
const SYNC_FILTERS = {
all: __( 'All' ),
[ SYNC_TYPES.full ]: __( 'Synced' ),
[ SYNC_TYPES.unsynced ]: __( 'Standard' ),
};
const SYNC_DESCRIPTIONS = {
all: '',
[ SYNC_TYPES.full ]: __(
'Patterns that are kept in sync across the site.'
),
[ SYNC_TYPES.unsynced ]: __(
'Patterns that can be changed freely without affecting the site.'
),
};
const PAGE_SIZE = 20;
export default function PatternsList( { categoryId, type } ) {
const location = useLocation();
const history = useHistory();
const isMobileViewport = useViewportMatch( 'medium', '<' );
const [ filterValue, setFilterValue, delayedFilterValue ] =
useDebouncedInput( '' );
const deferredFilterValue = useDeferredValue( delayedFilterValue );
const [ syncFilter, setSyncFilter ] = useState( 'all' );
const [ currentPage, setCurrentPage ] = useState( 1 );
const deferredSyncedFilter = useDeferredValue( syncFilter );
const isUncategorizedThemePatterns =
type === PATTERNS && categoryId === 'uncategorized';
const { patterns, isResolving } = usePatterns(
type,
isUncategorizedThemePatterns ? '' : categoryId,
{
search: deferredFilterValue,
syncStatus:
deferredSyncedFilter === 'all'
? undefined
: deferredSyncedFilter,
}
);
const id = useId();
const titleId = `${ id }-title`;
const descriptionId = `${ id }-description`;
const hasPatterns = patterns.length;
const title = SYNC_FILTERS[ syncFilter ];
const description = SYNC_DESCRIPTIONS[ syncFilter ];
const totalItems = patterns.length;
const pageIndex = currentPage - 1;
const numPages = Math.ceil( patterns.length / PAGE_SIZE );
const list = useMemo(
() =>
patterns.slice(
pageIndex * PAGE_SIZE,
pageIndex * PAGE_SIZE + PAGE_SIZE
),
[ pageIndex, patterns ]
);
const asyncList = useAsyncList( list, { step: 10 } );
const changePage = ( page ) => {
const scrollContainer = document.querySelector( '.edit-site-patterns' );
scrollContainer?.scrollTo( 0, 0 );
setCurrentPage( page );
};
return (
<>
<VStack className="edit-site-patterns__header" spacing={ 6 }>
<PatternsHeader
categoryId={ categoryId }
type={ type }
titleId={ titleId }
descriptionId={ descriptionId }
/>
<Flex alignment="stretch" wrap>
{ isMobileViewport && (
<SidebarButton
icon={ isRTL() ? chevronRight : chevronLeft }
label={ __( 'Back' ) }
onClick={ () => {
// Go back in history if we came from the Patterns page.
// Otherwise push a stack onto the history.
if (
location.state?.backPath === '/patterns'
) {
history.back();
} else {
history.push( { path: '/patterns' } );
}
} }
/>
) }
<FlexBlock className="edit-site-patterns__search-block">
<SearchControl
className="edit-site-patterns__search"
onChange={ ( value ) => setFilterValue( value ) }
placeholder={ __( 'Search patterns' ) }
label={ __( 'Search patterns' ) }
value={ filterValue }
__nextHasNoMarginBottom
/>
</FlexBlock>
{ categoryId === USER_PATTERN_CATEGORY && (
<ToggleGroupControl
className="edit-site-patterns__sync-status-filter"
hideLabelFromVision
label={ __( 'Filter by sync status' ) }
value={ syncFilter }
isBlock
onChange={ ( value ) => setSyncFilter( value ) }
__nextHasNoMarginBottom
>
{ Object.entries( SYNC_FILTERS ).map(
( [ key, label ] ) => (
<ToggleGroupControlOption
className="edit-site-patterns__sync-status-filter-option"
key={ key }
value={ key }
label={ label }
/>
)
) }
</ToggleGroupControl>
) }
</Flex>
</VStack>
<VStack
className="edit-site-patterns__section"
justify="flex-start"
spacing={ 6 }
>
{ syncFilter !== 'all' && (
<VStack className="edit-site-patterns__section-header">
<Heading as="h3" level={ 5 } id={ titleId }>
{ title }
</Heading>
{ description ? (
<Text variant="muted" as="p" id={ descriptionId }>
{ description }
</Text>
) : null }
</VStack>
) }
{ hasPatterns && (
<Grid
categoryId={ categoryId }
items={ asyncList }
aria-labelledby={ titleId }
aria-describedby={ descriptionId }
/>
) }
{ ! isResolving && ! hasPatterns && <NoPatterns /> }
</VStack>
{ numPages > 1 && (
<Pagination
currentPage={ currentPage }
numPages={ numPages }
changePage={ changePage }
totalItems={ totalItems }
/>
) }
</>
);
}