web/src/pages/browse.astro

216 lines · 5524 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(--foreground);
128    border-radius: var(--curve-sm);
129    font-size: 1.2rem;
130    box-shadow: 3px 3px 0 var(--foreground);
131    color: var(--accent-3);
132    &:focus {
133      outline: none;
134      box-shadow: 4px 4px 0 var(--foreground);
135    }
136  }
137  h2 {
138    font-family: "Lekton", sans-serif;
139    font-weight: bold;
140    font-size: 3rem;
141    margin: 0;
142    color: var(--accent-3);
143  }
144}
145
146.sitemap-link {
147  margin: 0 auto;
148  width: 85vw;
149  a {
150    color: var(--accent-3);
151  }
152}
153
154.no-results {
155  text-align: center;
156  width: 85vw;
157  margin: 2rem auto 4rem auto;
158  display: none;
159  .zilch {
160    font-size: 2rem;
161    color: var(--accent-3);
162    opacity: 0.5;
163    font-weight: bold;
164  }
165  p {
166    margin: 0.25rem;
167  }
168}
169
170.categories {
171  columns: 6 300px;
172  column-gap: 1rem;
173  margin: 0 auto;
174  width: 85vw;
175  max-width: 1800px;
176  padding: 1rem 0 2rem 0;
177  @media(max-width: 768px) {
178    padding: 0;
179    width: 95%;
180  }
181  .category {
182    background: var(--accent-fg);
183    border: 2px solid var(--foreground);
184    border-radius: var(--curve-sm);
185    box-shadow: 4px 4px 0 var(--foreground);
186    padding: 1rem;
187    width: 85%;
188    display: inline-flex;
189    flex-direction: column;
190    margin: 1rem;
191    .category-title {
192      text-decoration: none;
193      color: var(--foreground);
194    }
195    h3 {
196      font-family: "Lekton", sans-serif;
197      font-weight: bold;
198      margin: 0;
199      font-size: 1.8rem;
200    }
201    .sections {
202      padding-left: 1rem;
203      .section {
204        a {
205          text-decoration: none;
206          color: var(--foreground);
207        }
208        .service-count {
209          color: var(--accent-3);
210        }
211      }
212    }
213  }
214}
215
216</style>