Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added quick filters based on popular tags #130

Merged
merged 2 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions site/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
toggleTag,
toggleSubHook,
searchQueryToString,
getTerms
getTerms,
sortTags
} from "../utils";

const device = {
Expand Down Expand Up @@ -83,7 +84,7 @@ const SubHook = styled.a`

const FilterInput = styled.input`
width: 100%;
margin-top: 1rem;
margin: 1rem 0;
padding: 0.4rem 0.8rem;
font-family: "Roboto", sans-serif;
`;
Expand All @@ -99,6 +100,7 @@ const allHooks = sortHooks(unsortedHooks).map((x, i) => {
x.key = i;
return x;
});
const allTags = sortTags(allHooks);

const IndexPage = () => {
const [term, setTerm] = useState("");
Expand All @@ -110,6 +112,9 @@ const IndexPage = () => {
const hooksToSearch = getSubHooks(query);
const termsToSearch = getTerms(query);

const POPULAR_TAG_COUNT = 5;
const popularTags = allTags.slice(0, POPULAR_TAG_COUNT);

return (
<Layout>
<p>
Expand All @@ -126,6 +131,22 @@ const IndexPage = () => {
}}
placeholder="filter by name"
/>
{popularTags.map((tag) => (
<Tag
key={tag.name}
href={`#${tag.name}`}
onClick={(event) => {
event.preventDefault();
setTerm(searchQueryToString(toggleTag(query, tag.name)));
}}
>
<Highlighter
searchWords={tagsToSearch}
autoEscape={true}
textToHighlight={`${tag.name} (${tag.count})`}
/>
</Tag>
))}
<ResultsCount>
Found {results.length} {results.length === 1 ? "entry" : "entries"}
</ResultsCount>
Expand Down
19 changes: 19 additions & 0 deletions site/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,22 @@ function compare(hookA, hookB) {
}

export const sortHooks = hooks => hooks.sort(compare);

const compareTags = (tagA, tagB) => tagB.count - tagA.count;

export const sortTags = (hooks) =>
hooks.reduce((tags, hook) => {
hook.tags.forEach((tag) => {
if (tags.map((t) => t.name).includes(tag)) {
const index = tags.map((t) => t.name).indexOf(tag);
tags[index].count++;
} else {
tags.push({
name: tag,
count: 1
});
}
});
return tags.sort(compareTags);
}, []);