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