Skip to content

Commit d526aa3

Browse files
committed
Modified filter so that it works on a narrowing basis when using language tags with snippet tags.
1 parent 9410048 commit d526aa3

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

src/CodeSnippetDisplay.tsx

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,12 @@ export class CodeSnippetDisplay extends React.Component<
13041304
return null;
13051305
}
13061306

1307-
filterSnippets = (searchValue: string, filterTags: string[]): void => {
1307+
filterSnippets = (
1308+
searchValue: string,
1309+
filterTags: string[],
1310+
selectedLangTags: string[]
1311+
): void => {
1312+
// TODO: when language tag is selected, only display tags that have that tag AND the snippet tags selected.
13081313
// filter with search
13091314
let matchIndices: number[][] = [];
13101315
const matchResults: StringExt.IMatchResult[] = [];
@@ -1351,12 +1356,34 @@ export class CodeSnippetDisplay extends React.Component<
13511356
const newMatchIndices = matchIndices.slice();
13521357
filteredSnippets = filteredSnippets.filter((codeSnippet, id) => {
13531358
return filterTags.some((tag) => {
1359+
// check if filterTags has a language tag or pass in selectedLang tags
1360+
// then check if codeSnippet.language matches that lang. If it matches then
1361+
// filter by the rest of the selected snippet tags.
13541362
if (codeSnippet.tags) {
1355-
if (
1356-
codeSnippet.tags.includes(tag) ||
1357-
codeSnippet.language === tag
1358-
) {
1359-
return true;
1363+
if (selectedLangTags.length !== 0) {
1364+
// lang tags selected
1365+
console.log(selectedLangTags);
1366+
if (
1367+
codeSnippet.tags.includes(tag) &&
1368+
selectedLangTags.includes(codeSnippet.language)
1369+
) {
1370+
return true;
1371+
} else if (
1372+
filterTags.length === selectedLangTags.length &&
1373+
filterTags.every((value) => selectedLangTags.includes(value))
1374+
) {
1375+
//if only language tags are selected
1376+
console.log('hi');
1377+
if (selectedLangTags.includes(codeSnippet.language)) {
1378+
return true;
1379+
}
1380+
}
1381+
} else {
1382+
// no lang tags selected
1383+
console.log('reached');
1384+
if (codeSnippet.tags.includes(tag)) {
1385+
return true;
1386+
}
13601387
}
13611388
}
13621389
// if the snippet does not have the tag, remove its mathed index

src/FilterTools.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import React from 'react';
99
interface IFilterSnippetProps {
1010
languages: string[];
1111
allTags: string[][]; // change to [[snip], [lang]]
12-
onFilter: (searchValue: string, filterTags: string[]) => void;
12+
onFilter: (
13+
searchValue: string,
14+
filterTags: string[],
15+
selectedLangTags: string[]
16+
) => void;
1317
}
1418

1519
interface IFilterSnippetState {
@@ -181,7 +185,13 @@ export class FilterTools extends React.Component<
181185
};
182186

183187
filterSnippets(): void {
184-
this.props.onFilter(this.state.searchValue, this.state.selectedTags);
188+
this.props.onFilter(
189+
this.state.searchValue,
190+
this.state.selectedTags,
191+
this.state.selectedTags.filter((tag) =>
192+
this.props.languages.includes(tag)
193+
)
194+
);
185195
}
186196

187197
renderFilterOption(): JSX.Element {

0 commit comments

Comments
 (0)