web/src/pages/browse.astro

226 lines · 5440 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: 1rem 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: 0.5rem 0 0 0;
132			opacity: 0.5;
133			visibility: hidden;
134		}
135		input {
136			padding: 0.5rem;
137			border: 2px solid var(--box-outline);
138			border-radius: var(--curve-sm);
139			font-size: 1.2rem;
140			box-shadow: 3px 3px 0 var(--box-outline);
141			color: var(--accent-3);
142			background: var(--background-form);
143			&:focus {
144				outline: none;
145				box-shadow: 4px 4px 0 var(--box-outline);
146			}
147		}
148		h2 {
149			font-family: 'Lekton', sans-serif;
150			font-weight: bold;
151			font-size: 3rem;
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: 2rem auto 4rem auto;
169		display: none;
170		.zilch {
171			font-size: 2rem;
172			color: var(--accent-3);
173			opacity: 0.5;
174			font-weight: bold;
175		}
176		p {
177			margin: 0.25rem;
178		}
179	}
180
181	.categories {
182		columns: 6 300px;
183		column-gap: 1rem;
184		margin: 0 auto;
185		width: 85vw;
186		max-width: 1800px;
187		padding: 1rem 0 2rem 0;
188		@media (max-width: 768px) {
189			padding: 0;
190			width: 95%;
191		}
192		.category {
193			background: var(--accent-fg);
194			border: 2px solid var(--box-outline);
195			border-radius: var(--curve-sm);
196			box-shadow: 4px 4px 0 var(--box-outline);
197			padding: 1rem;
198			width: 85%;
199			display: inline-flex;
200			flex-direction: column;
201			margin: 1rem;
202			.category-title {
203				text-decoration: none;
204				color: var(--foreground);
205			}
206			h3 {
207				font-family: 'Lekton', sans-serif;
208				font-weight: bold;
209				margin: 0;
210				font-size: 1.8rem;
211			}
212			.sections {
213				padding-left: 1rem;
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>