Skip to content

Commit 1882870

Browse files
Allow for tags and search queries to be used at the same time
This has bothered me quite a lot, so I refactored the search function to fix this.
1 parent 77c57a6 commit 1882870

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

src/components/library/library.jsx

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,11 @@ class LibraryComponent extends React.Component {
192192
// console.log(tag, enabled);
193193
if (this.state.playingItem === null) {
194194
this.setState({
195-
filterQuery: '',
196195
selectedTags: this.state.selectedTags.concat([tag.toLowerCase()])
197196
});
198197
} else {
199198
this.props.onItemMouseLeave(this.getFilteredData()[[this.state.playingItem]]);
200199
this.setState({
201-
filterQuery: '',
202200
playingItem: null,
203201
selectedTags: this.state.selectedTags.concat([tag.toLowerCase()])
204202
});
@@ -238,42 +236,53 @@ class LibraryComponent extends React.Component {
238236
if (this.state.playingItem === null) {
239237
this.setState({
240238
filterQuery: event.target.value,
241-
selectedTags: []
242239
});
243240
} else {
244241
this.props.onItemMouseLeave(this.getFilteredData()[[this.state.playingItem]]);
245242
this.setState({
246243
filterQuery: event.target.value,
247244
playingItem: null,
248-
selectedTags: []
249245
});
250246
}
251247
}
252248
handleFilterClear () {
253249
this.setState({filterQuery: ''});
254250
}
251+
get custom_extensions() {
252+
return this.state.favorites
253+
.filter(item => (typeof item !== "string"))
254+
.map(item => Object.assign(item, { custom: true }));
255+
}
255256
createFilteredData () {
256-
if (this.state.selectedTags.length <= 0) {
257-
if (!this.state.filterQuery) return this.state.data;
258-
return this.state.data.filter(dataItem => (
259-
(dataItem.tags || [])
260-
// Second argument to map sets `this`
261-
.map(String.prototype.toLowerCase.call, String.prototype.toLowerCase)
262-
.concat(dataItem.name ?
263-
(typeof dataItem.name === 'string' ?
264-
// Use the name if it is a string, else use formatMessage to get the translated name
265-
dataItem.name : this.props.intl.formatMessage(dataItem.name.props)
266-
).toLowerCase() :
267-
null)
268-
.join('\n') // unlikely to partially match newlines
269-
.indexOf(this.state.filterQuery.toLowerCase()) !== -1
270-
));
271-
}
272-
return this.state.data.filter(dataItem => (arrayIncludesItemsFrom(
273-
dataItem.tags &&
274-
dataItem.tags
275-
.map(String.prototype.toLowerCase.call, String.prototype.toLowerCase),
276-
this.state.selectedTags)));
257+
const data = [].concat(
258+
this.state.data,
259+
);
260+
261+
const no_tags = this.state.selectedTags.length === 0;
262+
const no_query = this.state.filterQuery === "";
263+
if (no_tags && no_query) return data;
264+
265+
return data.filter(dataItem => {
266+
var tags = (dataItem.tags ?? []).map(tag => tag.toLowerCase());
267+
268+
if (!arrayIncludesItemsFrom(tags, this.state.selectedTags)) return false;
269+
270+
if (no_query) return true;
271+
272+
var name = dataItem.name;
273+
if (name) {
274+
if (typeof dataItem.name !== "string") {
275+
name = this.props.intl.formatMessage(dataItem.name.props);
276+
}
277+
tags.push(name.toLowerCase());
278+
}
279+
280+
tags = tags.join("\n");
281+
282+
const query = this.state.filterQuery.toLowerCase();
283+
284+
return tags.includes(query)
285+
});
277286
}
278287
getFilteredData () {
279288
const filtered = this.createFilteredData();
@@ -282,16 +291,13 @@ class LibraryComponent extends React.Component {
282291
return filtered;
283292
}
284293

285-
const final = [].concat(
286-
this.state.favorites
287-
.filter(item => (typeof item !== "string"))
288-
.map(item => ({ ...item, custom: true }))
289-
.reverse(),
294+
const fully_filtered = [].concat(
295+
this.custom_extensions,
290296
filtered.filter(item => (this.state.favorites.includes(item.extensionId))),
291297
filtered.filter(item => (!this.state.favorites.includes(item.extensionId)))
292-
).map(item => ({ ...item, custom: typeof item.custom === "boolean" ? item.custom : false }));
293-
294-
return final;
298+
).map(item => Object.assign(item, { custom: !!item.custom }));
299+
300+
return fully_filtered;
295301
}
296302
scrollToTop () {
297303
this.filteredDataRef.scrollTop = 0;

0 commit comments

Comments
 (0)