web/src/pages/browse.astro
233 lines · 5837 bytes
1---
2import Layout from '@layouts/Layout.astro';
3import SectionList from '@components/things/SectionList.astro';
4
5import { fetchData } from '@utils/fetch-data';
6import type { Category } from '../types/Service';
7
8const categories: Category[] = (await fetchData())?.categories;
9---
10
11<Layout
12 title="Browse Categories | Awesome Privacy"
13 description="Browse every category of privacy-respecting software, from browsers and VPNs to encrypted messaging, cloud storage and password managers."
14>
15 <div class="head-wrap">
16 <h1>Browse</h1>
17 <span>
18 <input
19 id="searchInput"
20 type="search"
21 placeholder="Start typing to filter..."
22 />
23 <p id="press-enter-msg" class="press-enter">
24 Press enter for deep search
25 </p>
26 </span>
27 </div>
28 <p class="sitemap-link">
29 Not sure what you're looking for? Take a look through <a href="/all/"
30 >all listings</a
31 >
32 </p>
33
34 <ul class="categories">
35 {
36 categories.map((category) => (
37 <li class="category">
38 <SectionList
39 title={category.name}
40 sections={category.sections}
41 headingLevel={2}
42 />
43 </li>
44 ))
45 }
46 </ul>
47
48 <div class="no-results">
49 <p class="zilch">Nothing found 😢</p>
50 <p>Try a <a href="/search/">deep search</a> instead</p>
51 </div>
52</Layout>
53
54<script>
55 // Time for some good ol' fashioned vanilla JS...
56 // I reccomend you not to look at this code for too long, it's not pretty.
57 const filterInput = document.querySelector('input');
58 const categories = document.querySelectorAll<HTMLElement>('.category');
59 const pressEnterMsg = document.getElementById(
60 'press-enter-msg',
61 ) as HTMLElement | null;
62 const noResults = document.querySelector('.no-results') as HTMLElement | null;
63
64 if (!pressEnterMsg || !noResults) {
65 throw new Error('No pressEnterMsg or noResults');
66 }
67
68 filterInput?.addEventListener('input', (e) => {
69 let resultsCount = 0;
70 const filter = (e.target as HTMLInputElement).value.toLowerCase();
71 if (filter.length > 0) {
72 pressEnterMsg.style.visibility = 'visible';
73 } else {
74 pressEnterMsg.style.visibility = 'hidden';
75 }
76 categories.forEach((category) => {
77 const titleElement = category.querySelector('.category-title');
78 const title = titleElement ? titleElement.textContent?.toLowerCase() : '';
79 const sections = category.querySelectorAll<HTMLElement>('.section');
80 let count = 0;
81 sections.forEach((section) => {
82 const sectionTitle = section.textContent?.toLowerCase();
83 if (sectionTitle?.includes(filter)) {
84 section.style.display = 'block';
85 count++;
86 resultsCount++;
87 } else {
88 section.style.display = 'none';
89 }
90 });
91 if (title && title.includes(filter)) {
92 category.style.display = 'inline-flex';
93 resultsCount++;
94 } else if (count === 0) {
95 category.style.display = 'none';
96 }
97 });
98 noResults.style.display = resultsCount === 0 ? 'block' : 'none';
99 });
100
101 if (typeof window !== 'undefined') {
102 document.addEventListener('DOMContentLoaded', function () {
103 const searchInput = document.getElementById(
104 'searchInput',
105 ) as HTMLInputElement | null;
106 if (searchInput === null) return;
107 searchInput.addEventListener('keydown', function (event) {
108 if (event.key === 'Enter') {
109 event.preventDefault();
110 window.location.href = `/search/${encodeURIComponent(searchInput.value.trim())}/`;
111 }
112 if (event.key === 'Escape') {
113 searchInput.value = '';
114 searchInput.blur();
115 pressEnterMsg.style.visibility = 'hidden';
116 categories.forEach((category) => {
117 category.style.display = 'inline-flex';
118 const sections = category.querySelectorAll<HTMLElement>('.section');
119 sections.forEach((section) => {
120 section.style.display = 'block';
121 });
122 });
123 }
124 });
125 });
126 }
127</script>
128
129<style lang="scss">
130 .head-wrap {
131 margin: var(--space-md) auto 0 auto;
132 width: 85vw;
133 display: flex;
134 align-items: center;
135 justify-content: space-between;
136 flex-wrap: wrap;
137 .press-enter {
138 margin: var(--space-sm) 0 0 0;
139 opacity: var(--opacity-dim);
140 visibility: hidden;
141 }
142 input {
143 padding: var(--space-sm);
144 border: var(--border-heavy);
145 border-radius: var(--curve-sm);
146 font-size: var(--text-md);
147 box-shadow: var(--shadow-sm);
148 color: var(--accent-3-text);
149 background: var(--background-form);
150 &:focus {
151 outline: none;
152 box-shadow: var(--shadow-sm-hover);
153 }
154 }
155 h1 {
156 font-family: var(--font-subtitle);
157 font-weight: bold;
158 font-size: var(--text-4xl);
159 margin: 0;
160 color: var(--accent-3-text);
161 }
162 }
163
164 .sitemap-link {
165 margin: 0 auto;
166 width: 85vw;
167 a {
168 color: var(--accent-3-text);
169 }
170 }
171
172 .no-results {
173 text-align: center;
174 width: 85vw;
175 margin: var(--space-lg) auto var(--space-2xl) auto;
176 display: none;
177 .zilch {
178 font-size: var(--text-3xl);
179 color: var(--accent-3-text);
180 opacity: var(--opacity-dim);
181 font-weight: bold;
182 }
183 p {
184 margin: var(--space-xs);
185 }
186 }
187
188 .categories {
189 columns: 6 300px;
190 column-gap: var(--space-md);
191 margin: 0 auto;
192 width: 85vw;
193 max-width: 1800px;
194 padding: var(--space-md) 0 var(--space-lg) 0;
195 @media (max-width: 768px) {
196 padding: 0;
197 width: 95%;
198 }
199 .category {
200 background: var(--surface);
201 border: var(--border-heavy);
202 border-radius: var(--curve-sm);
203 box-shadow: var(--shadow-md);
204 padding: var(--space-md);
205 width: 85%;
206 display: inline-flex;
207 flex-direction: column;
208 margin: var(--space-md);
209 .category-title {
210 text-decoration: none;
211 color: var(--foreground);
212 }
213 h3 {
214 font-family: var(--font-subtitle);
215 font-weight: bold;
216 margin: 0;
217 font-size: var(--text-2xl);
218 }
219 .sections {
220 padding-left: var(--space-md);
221 .section {
222 a {
223 text-decoration: none;
224 color: var(--foreground);
225 }
226 .service-count {
227 color: var(--accent-3-text);
228 }
229 }
230 }
231 }
232 }
233</style>