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