diff --git a/site/src/pages/index.js b/site/src/pages/index.js index 9f83b3a..446aa60 100644 --- a/site/src/pages/index.js +++ b/site/src/pages/index.js @@ -14,7 +14,8 @@ import { toggleTag, toggleSubHook, searchQueryToString, - getTerms + getTerms, + sortTags } from "../utils"; const device = { @@ -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; `; @@ -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(""); @@ -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 (

@@ -126,6 +131,22 @@ const IndexPage = () => { }} placeholder="filter by name" /> + {popularTags.map((tag) => ( + { + event.preventDefault(); + setTerm(searchQueryToString(toggleTag(query, tag.name))); + }} + > + + + ))} Found {results.length} {results.length === 1 ? "entry" : "entries"} diff --git a/site/src/utils.js b/site/src/utils.js index 10ae22c..e299c66 100644 --- a/site/src/utils.js +++ b/site/src/utils.js @@ -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); + }, []); + \ No newline at end of file