Skip to content

Commit 61006ef

Browse files
committed
When language tags are selected, only snippet tags with snippets in that language will appear
1 parent d526aa3 commit 61006ef

File tree

2 files changed

+59
-23
lines changed

2 files changed

+59
-23
lines changed

src/CodeSnippetDisplay.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,30 @@ export class CodeSnippetDisplay extends React.Component<
14271427
return [tags, languages];
14281428
}
14291429

1430+
getActiveTagsDictionary = (): Map<string, string[]> => {
1431+
const tagsAndLangs: Map<string, string[]> = new Map<string, string[]>();
1432+
for (const codeSnippet of this.props.codeSnippets) {
1433+
if (codeSnippet.tags) {
1434+
// check if tag is in dict, if it is add lang to value (if not already present)
1435+
// if tag not in dict add tag as key and lang as first val
1436+
for (const tag of codeSnippet.tags) {
1437+
if (tag !== codeSnippet.language) {
1438+
if (tagsAndLangs.has(tag)) {
1439+
const langs = tagsAndLangs.get(tag);
1440+
if (!langs.includes(codeSnippet.language)) {
1441+
langs.push(codeSnippet.language);
1442+
}
1443+
tagsAndLangs.set(tag, langs);
1444+
} else {
1445+
tagsAndLangs.set(tag, [codeSnippet.language]);
1446+
}
1447+
}
1448+
}
1449+
}
1450+
}
1451+
return tagsAndLangs;
1452+
};
1453+
14301454
private deleteCommand(codeSnippet: ICodeSnippet): void {
14311455
showDialog({
14321456
title: 'Delete snippet?',
@@ -1603,8 +1627,9 @@ export class CodeSnippetDisplay extends React.Component<
16031627
</header>
16041628
<div className={CODE_SNIPPET_HEADER_BOX}>
16051629
<FilterTools
1606-
languages={this.getActiveTags()[1]}
1607-
allTags={this.getActiveTags()}
1630+
tagDictionary={this.getActiveTagsDictionary()}
1631+
languageTags={this.getActiveTags()[1]}
1632+
snippetTags={this.getActiveTags()[0]}
16081633
onFilter={this.filterSnippets}
16091634
/>
16101635
</div>

src/FilterTools.tsx

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import React from 'react';
77
//import { filter } from '@lumino/algorithm';
88

99
interface IFilterSnippetProps {
10-
languages: string[];
11-
allTags: string[][]; // change to [[snip], [lang]]
10+
tagDictionary: Map<string, string[]>;
11+
languageTags: string[]; // just lang tags
12+
snippetTags: string[]; // just snippet tags
1213
onFilter: (
1314
searchValue: string,
1415
filterTags: string[],
@@ -61,13 +62,10 @@ export class FilterTools extends React.Component<
6162
componentDidUpdate(prevProps: IFilterSnippetProps): void {
6263
if (prevProps !== this.props) {
6364
// get all the tags together in one list
64-
const flattenTags = this.props.allTags.reduce(
65-
(accumulator, value) => accumulator.concat(value),
66-
[]
67-
);
65+
const concatTags = this.props.snippetTags.concat(this.props.languageTags);
6866
this.setState((state) => ({
6967
selectedTags: state.selectedTags
70-
.filter((tag) => flattenTags.includes(tag))
68+
.filter((tag) => concatTags.includes(tag))
7169
.sort(),
7270
}));
7371
}
@@ -82,17 +80,15 @@ export class FilterTools extends React.Component<
8280
filterOption.classList.toggle('idle');
8381
}
8482

85-
renderTags(tags: string[][], type: string): JSX.Element {
86-
// get all the tags together in one list
87-
const flattenTags = tags.reduce(
88-
(accumulator, value) => accumulator.concat(value),
89-
[]
83+
renderTags(tags: string[], type: string): JSX.Element {
84+
const selectedLanguageTags = this.state.selectedTags.filter((tag) =>
85+
this.props.languageTags.includes(tag)
9086
);
9187
return (
9288
<div className={FILTER_TAGS}>
93-
{flattenTags.sort().map((tag: string, index: number) => {
89+
{tags.sort().map((tag: string, index: number) => {
9490
// language tags
95-
if (type === 'language' && this.props.languages.includes(tag)) {
91+
if (type === 'language' && this.props.languageTags.includes(tag)) {
9692
if (this.state.selectedTags.includes(tag)) {
9793
return this.renderAppliedTag(tag, index.toString());
9894
} else {
@@ -101,12 +97,25 @@ export class FilterTools extends React.Component<
10197
} else if (
10298
// snippet tags
10399
type === 'snippet' &&
104-
!this.props.languages.includes(tag)
100+
!this.props.languageTags.includes(tag)
105101
) {
106-
if (this.state.selectedTags.includes(tag)) {
107-
return this.renderAppliedTag(tag, index.toString());
102+
if (selectedLanguageTags.length !== 0) {
103+
const langsMatch = this.props.tagDictionary
104+
.get(tag)
105+
.some((r) => selectedLanguageTags.includes(r));
106+
if (langsMatch) {
107+
if (this.state.selectedTags.includes(tag)) {
108+
return this.renderAppliedTag(tag, index.toString());
109+
} else {
110+
return this.renderUnappliedTag(tag, index.toString());
111+
}
112+
}
108113
} else {
109-
return this.renderUnappliedTag(tag, index.toString());
114+
if (this.state.selectedTags.includes(tag)) {
115+
return this.renderAppliedTag(tag, index.toString());
116+
} else {
117+
return this.renderUnappliedTag(tag, index.toString());
118+
}
110119
}
111120
}
112121
})}
@@ -189,23 +198,25 @@ export class FilterTools extends React.Component<
189198
this.state.searchValue,
190199
this.state.selectedTags,
191200
this.state.selectedTags.filter((tag) =>
192-
this.props.languages.includes(tag)
201+
this.props.languageTags.includes(tag)
193202
)
194203
);
195204
}
196205

197206
renderFilterOption(): JSX.Element {
198207
//TODO: make lang tags/cell tags a dropdown
208+
// get all the tags together in one list
209+
const concatTags = this.props.snippetTags.concat(this.props.languageTags);
199210
return (
200211
<div className={`${FILTER_OPTION} idle`}>
201212
<div className={FILTER_TITLE}>
202213
<span>language tags</span>
203214
</div>
204-
{this.renderTags(this.props.allTags, 'language')}
215+
{this.renderTags(concatTags, 'language')}
205216
<div className={FILTER_TITLE}>
206217
<span>snippet tags</span>
207218
</div>
208-
{this.renderTags(this.props.allTags, 'snippet')}
219+
{this.renderTags(concatTags, 'snippet')}
209220
</div>
210221
);
211222
}

0 commit comments

Comments
 (0)