forked from parse-community/parse-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCategoryList.react.js
More file actions
280 lines (267 loc) · 10.4 KB
/
CategoryList.react.js
File metadata and controls
280 lines (267 loc) · 10.4 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import styles from 'components/CategoryList/CategoryList.scss';
import Icon from 'components/Icon/Icon.react';
import { CurrentApp } from 'context/currentApp';
import generatePath from 'lib/generatePath';
import PropTypes from 'lib/PropTypes';
import React from 'react';
import { Link } from 'react-router-dom';
export default class CategoryList extends React.Component {
static contextType = CurrentApp;
constructor() {
super();
this.listWrapperRef = React.createRef();
this.state = {
openClasses: [],
hoveredFilter: null,
hoveredClass: null,
};
}
componentDidMount() {
const listWrapper = this.listWrapperRef.current;
if (listWrapper) {
this.highlight = document.createElement('div');
this.highlight.className = styles.highlight;
listWrapper.appendChild(this.highlight);
this._autoExpandForSelectedFilter();
this._updateHighlight();
}
}
componentDidUpdate(prevProps) {
// Auto-expand if the URL params changed (e.g., user navigated to a different filter)
// OR if categories changed (e.g., filters finished loading asynchronously)
if (prevProps.params !== this.props.params || prevProps.categories !== this.props.categories) {
this._autoExpandForSelectedFilter();
}
this._updateHighlight();
}
componentWillUnmount() {
if (this.highlight) {
this.highlight.parentNode.removeChild(this.highlight);
}
}
_findMatchingFilterIndex(filters, queryFilter, queryFilterId) {
// Find the index of a filter that matches the URL parameters
// Prioritize filterId matching, then fall back to content comparison
for (let i = 0; i < filters?.length; i++) {
const filter = filters[i];
// First priority: match by filterId if both URL and filter have an ID
if (queryFilterId && queryFilterId === filter.id) {
return i;
}
}
// Second priority: match by filter content (legacy fallback)
for (let i = 0; i < filters?.length; i++) {
const filter = filters[i];
if (queryFilter === filter.filter) {
return i;
}
}
return -1;
}
_autoExpandForSelectedFilter() {
// Check if a saved filter is selected in the URL and auto-expand the corresponding class
const query = new URLSearchParams(this.props.params);
if (query.has('filters')) {
const queryFilter = query.get('filters');
const filterId = query.get('filterId');
// Find the class that contains the selected filter
for (let i = 0; i < this.props.categories.length; i++) {
const c = this.props.categories[i];
const id = c.id || c.name;
// Check if this is the current class and it has filters
if (id === this.props.current && c.filters && c.filters.length > 0) {
// Check if any filter in this class matches the URL filter
const matchIndex = this._findMatchingFilterIndex(c.filters, queryFilter, filterId);
// If a matching filter is found, auto-expand this class
if (matchIndex !== -1 && !this.state.openClasses.includes(id)) {
this.setState(prevState => ({ openClasses: [...prevState.openClasses, id] }));
}
break;
}
}
}
}
_updateHighlight() {
if (this.highlight) {
let height = 0;
for (let i = 0; i < this.props.categories.length; i++) {
const c = this.props.categories[i];
const id = c.id || c.name;
if (id === this.props.current) {
if (this.state.openClasses.includes(id)) {
const query = new URLSearchParams(this.props.params);
if (query.has('filters')) {
const queryFilter = query.get('filters');
const filterId = query.get('filterId');
const matchIndex = this._findMatchingFilterIndex(c.filters, queryFilter, filterId);
if (matchIndex !== -1) {
height += (matchIndex + 1) * 20;
}
}
}
this.highlight.style.display = 'block';
this.highlight.style.top = height + 'px';
return;
}
if (id === 'classSeparator') {
height += 13;
} else if (this.state.openClasses.includes(id)) {
height = height + 20 * (c.filters.length + 1);
} else {
height += 20;
}
}
this.highlight.style.display = 'none';
}
}
toggleDropdown(e, id) {
e.preventDefault();
const openClasses = [...this.state.openClasses];
const index = openClasses.indexOf(id);
if (openClasses.includes(id)) {
openClasses.splice(index, 1);
} else {
openClasses.push(id);
}
this.setState({ openClasses });
}
render() {
if (this.props.categories.length === 0) {
return null;
}
return (
<div ref={this.listWrapperRef} className={styles.class_list}>
{this.props.categories.map(c => {
const id = c.id || c.name;
if (c.type === 'separator') {
return <hr key={id} className={styles.separator} />;
}
const count = c.count;
let className = id === this.props.current ? styles.active : '';
let selectedFilter = null;
if (this.state.openClasses.includes(id) && id === this.props.current) {
const query = new URLSearchParams(this.props.params);
if (query.has('filters')) {
const queryFilter = query.get('filters');
const queryFilterId = query.get('filterId');
selectedFilter = this._findMatchingFilterIndex(c.filters, queryFilter, queryFilterId);
if (selectedFilter !== -1) {
className = '';
}
}
}
const link = generatePath(this.context, (this.props.linkPrefix || '') + (c.link || id));
return (
<div key={id}>
<div
className={styles.link}
onMouseEnter={() => this.setState({ hoveredClass: id })}
onMouseLeave={() => this.setState({ hoveredClass: null })}
>
<Link
title={c.name}
to={{ pathname: link }}
className={className}
onClick={() => {
if (typeof this.props.classClicked === 'function') {
this.props.classClicked();
}
// Toggle filter list when clicking on a class that has filters
if ((c.filters || []).length > 0) {
this.setState(prevState => ({
openClasses: prevState.openClasses.includes(id)
? prevState.openClasses.filter(c => c !== id)
: [...prevState.openClasses, id]
}));
}
}}
>
{c.name}
</Link>
{c.onEdit && (
<a
className={styles.edit}
style={{ opacity: this.state.hoveredClass === id ? 1 : 0 }}
onClick={e => {
e.preventDefault();
c.onEdit();
}}
>
<Icon name="edit-solid" width={14} height={14} />
</a>
)}
<span className={styles.count}>{count}</span>
{(c.filters || []).length !== 0 && (
<a
className={styles.expand}
onClick={e => this.toggleDropdown(e, id)}
style={{
transform: this.state.openClasses.includes(id) ? 'scaleY(-1)' : 'scaleY(1)',
}}
></a>
)}
</div>
{this.state.openClasses.includes(id) &&
c.filters.map((filterData, index) => {
const { name, filter, id } = filterData;
// Only include filterId in URL if the filter has an ID (modern filters)
// Legacy filters without ID should work with just the filter content
const url = id
? `${this.props.linkPrefix}${c.name}?filters=${encodeURIComponent(filter)}&filterId=${id}`
: `${this.props.linkPrefix}${c.name}?filters=${encodeURIComponent(filter)}`;
const filterKey = `${c.name}-${index}`;
return (
<div
key={index}
className={styles.childLink}
onMouseEnter={() => this.setState({ hoveredFilter: filterKey })}
onMouseLeave={() => this.setState({ hoveredFilter: null })}
>
<Link
className={selectedFilter === index ? styles.active : ''}
onClick={e => {
e.preventDefault();
this.props.filterClicked(url);
}}
key={name + index}
>
<span>{name}</span>
</Link>
{this.props.onEditFilter && (
<a
className={styles.editFilter}
style={{ opacity: this.state.hoveredFilter === filterKey ? 1 : 0 }}
onClick={e => {
e.preventDefault();
this.props.onEditFilter(c.name, filterData);
}}
>
<Icon name="edit-solid" width={14} height={14} />
</a>
)}
</div>
);
})}
</div>
);
})}
</div>
);
}
}
CategoryList.propTypes = {
categories: PropTypes.arrayOf(PropTypes.object).describe(
'Array of categories used to populate list.'
),
current: PropTypes.string.describe('Id of current category to be highlighted.'),
linkPrefix: PropTypes.string.describe('Link prefix used to generate link path.'),
classClicked: PropTypes.func.describe('Callback function invoked when a class is clicked.'),
onEditFilter: PropTypes.func.describe('Callback function for editing a filter.'),
};