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