web/src/components/things/SearchResults.svelte

332 lines · 8526 bytes

  1<script lang="ts">
  2  import { onMount } from 'svelte';
  3
  4  import ServiceCard from './ServiceCard.svelte';
  5  import FontAwesome from '@components/form/FontAwesome.svelte';
  6  import { runSearch } from '@utils/do-searchy-searchy';
  7  import type { SearchItem } from '@utils/do-searchy-searchy';
  8  import { slugify } from '@utils/fetch-data';
  9
 10  interface Props {
 11    searchTerm: string;
 12  }
 13  const { searchTerm }: Props = $props();
 14
 15  type Sort = 'relevance' | 'category';
 16  const sorts: { key: Sort; label: string }[] = [
 17    { key: 'relevance', label: 'Relevance' },
 18    { key: 'category', label: 'Category' },
 19  ];
 20
 21  const PAGE_SIZE = 50;
 22
 23  let results: SearchItem[] = $state([]);
 24  let loading = $state(true);
 25  let sort: Sort = $state('relevance');
 26  let shown = $state(PAGE_SIZE);
 27
 28  const visible = $derived(results.slice(0, shown));
 29
 30  onMount(async () => {
 31    const wanted = new URLSearchParams(location.search).get('sort');
 32    if (wanted === 'category') sort = wanted;
 33    try {
 34      results = (await runSearch(searchTerm)).filter(
 35        (r) => r.type === 'Service',
 36      );
 37    } finally {
 38      loading = false;
 39    }
 40  });
 41
 42  const setSort = (next: Sort) => {
 43    sort = next;
 44    const url = new URL(location.href);
 45    if (next === 'relevance') url.searchParams.delete('sort');
 46    else url.searchParams.set('sort', next);
 47    history.replaceState(null, '', url);
 48  };
 49
 50  // Group into category > section, keeping the relevance order within each
 51  const grouped = $derived.by(() => {
 52    const categories: Record<string, Record<string, SearchItem[]>> = {};
 53    for (const item of visible) {
 54      const sections = (categories[item.category] ??= {});
 55      (sections[item.sectionName || ''] ??= []).push(item);
 56    }
 57    return Object.entries(categories).map(([name, sections]) => ({
 58      name,
 59      sections: Object.entries(sections).map(([sectionName, items]) => ({
 60        sectionName,
 61        items,
 62      })),
 63    }));
 64  });
 65
 66  const onlySection = $derived(
 67    grouped.length === 1 && grouped[0].sections.length === 1
 68      ? { category: grouped[0].name, name: grouped[0].sections[0].sectionName }
 69      : null,
 70  );
 71  const canGroup = $derived(!onlySection && results.length > 0);
 72  const view = $derived(canGroup ? sort : 'relevance');
 73</script>
 74
 75<section class="results-head">
 76  <div>
 77    <h3>Results</h3>
 78    <p>
 79      {#if loading}
 80        Searching for "{searchTerm}"...
 81      {:else if visible.length < results.length}
 82        Showing {visible.length} of {results.length} results for "{searchTerm}"
 83      {:else}
 84        {results.length}
 85        {results.length === 1 ? 'result' : 'results'} for "{searchTerm}"
 86      {/if}
 87    </p>
 88  </div>
 89  {#if onlySection}
 90    <a
 91      class="view-all"
 92      href={`/${slugify(onlySection.category)}/${slugify(onlySection.name)}/`}
 93    >
 94      View all {onlySection.name} &rarr;
 95    </a>
 96  {:else if canGroup}
 97    <div class="sort-by" role="group" aria-label="Sort results by">
 98      <span>Sort by:</span>
 99      {#each sorts as option (option.key)}
100        <button
101          type="button"
102          class:active={sort === option.key}
103          aria-pressed={sort === option.key}
104          onclick={() => setSort(option.key)}
105        >
106          {option.label}
107        </button>
108      {/each}
109    </div>
110  {/if}
111</section>
112
113{#if !loading && !results.length}
114  <section class="nothing-found">
115    <p>Nothing matched "{searchTerm}"</p>
116    <small>
117      Try a tool's name, the product you're replacing, or a platform. Or browse
118      <a href="/all/">all listings</a>.
119    </small>
120  </section>
121{:else if view === 'relevance'}
122  <section class="service-grid">
123    {#each visible as item (item.path)}
124      {#if item.service}
125        <ServiceCard
126          service={item.service}
127          categoryName={item.category}
128          sectionName={item.sectionName || ''}
129        />
130      {/if}
131    {/each}
132  </section>
133{:else}
134  <section class="by-category">
135    {#each grouped as category (category.name)}
136      <div class="category">
137        <a class="category-title" href={`/${slugify(category.name)}/`}>
138          <h3>{category.name}</h3>
139          <FontAwesome iconName={slugify(category.name)} />
140        </a>
141        {#each category.sections as section (section.sectionName)}
142          <h4>
143            <a
144              href={`/${slugify(category.name)}/${slugify(section.sectionName)}/`}
145            >
146              {section.sectionName}
147            </a>
148          </h4>
149          <div class="service-grid">
150            {#each section.items as item (item.path)}
151              {#if item.service}
152                <ServiceCard
153                  service={item.service}
154                  categoryName={item.category}
155                  sectionName={item.sectionName || ''}
156                />
157              {/if}
158            {/each}
159          </div>
160        {/each}
161      </div>
162    {/each}
163  </section>
164{/if}
165
166{#if shown < results.length}
167  <section class="load-more">
168    <button type="button" onclick={() => (shown += PAGE_SIZE)}>
169      Show {Math.min(PAGE_SIZE, results.length - shown)} more
170    </button>
171  </section>
172{/if}
173
174<style lang="scss">
175  .results-head {
176    display: flex;
177    flex-wrap: wrap;
178    gap: var(--space-md);
179    align-items: end;
180    justify-content: space-between;
181    max-width: var(--width-container);
182    width: calc(100% - 2rem);
183    margin: var(--space-lg) auto var(--space-md) auto;
184
185    h3 {
186      margin: 0;
187      color: var(--accent-3-text);
188      font-size: var(--text-xl);
189    }
190    p {
191      margin: 0;
192      opacity: var(--opacity-muted);
193    }
194  }
195
196  .load-more {
197    display: flex;
198    justify-content: center;
199
200    button {
201      cursor: pointer;
202      font-family: inherit;
203      font-size: var(--text-md);
204      padding: var(--space-sm) var(--space-lg);
205      color: var(--accent-3-text);
206      background: var(--surface);
207      border: var(--border-heavy);
208      border-radius: var(--curve-sm);
209      transition: var(--transition-normal);
210
211      &:hover {
212        box-shadow: var(--shadow-md);
213      }
214    }
215  }
216
217  .view-all {
218    color: var(--accent-3-text);
219    text-decoration: none;
220    font-size: var(--text-sm);
221    padding: var(--space-xs) var(--space-sm);
222    border: var(--border-light);
223    border-radius: var(--curve-sm);
224    background: var(--surface);
225    transition: var(--transition-normal);
226
227    &:hover {
228      border-color: var(--accent-3-text);
229    }
230  }
231
232  .sort-by {
233    display: flex;
234    align-items: center;
235    gap: var(--space-sm);
236
237    span {
238      opacity: var(--opacity-muted);
239      font-size: var(--text-sm);
240    }
241    button {
242      cursor: pointer;
243      font-family: inherit;
244      font-size: var(--text-sm);
245      padding: var(--space-xs) var(--space-sm);
246      color: var(--foreground);
247      background: var(--surface);
248      border: var(--border-light);
249      border-radius: var(--curve-sm);
250      transition: var(--transition-normal);
251
252      &:hover {
253        border-color: var(--accent-3-text);
254      }
255      &.active {
256        color: var(--accent-3-text);
257        border-color: var(--accent-3-text);
258        font-weight: bold;
259      }
260    }
261  }
262
263  .service-grid {
264    display: grid;
265    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
266    gap: var(--space-md);
267  }
268
269  .by-category,
270  .service-grid,
271  .nothing-found,
272  .load-more {
273    max-width: var(--width-container);
274    width: calc(100% - 2rem);
275    margin: 0 auto var(--space-lg) auto;
276  }
277
278  .by-category {
279    display: flex;
280    flex-direction: column;
281    gap: var(--space-lg);
282
283    .category-title {
284      display: flex;
285      align-items: center;
286      gap: var(--space-sm);
287      text-decoration: none;
288      color: var(--foreground);
289
290      h3 {
291        margin: 0;
292        font-family: var(--font-subtitle);
293        font-size: var(--text-2xl);
294      }
295      :global(svg) {
296        width: var(--text-xl);
297        height: var(--text-xl);
298        color: var(--accent-3-text);
299        opacity: var(--opacity-dim);
300      }
301    }
302    h4 {
303      margin: var(--space-md) 0 var(--space-sm) 0;
304      opacity: var(--opacity-muted);
305
306      a {
307        color: inherit;
308        text-decoration: none;
309
310        &:hover {
311          color: var(--accent-3-text);
312        }
313      }
314    }
315  }
316
317  .nothing-found {
318    display: flex;
319    flex-direction: column;
320    gap: var(--space-sm);
321    padding: var(--space-lg);
322    text-align: center;
323    background: var(--surface);
324    border: var(--border-heavy);
325    border-radius: var(--curve-sm);
326
327    p {
328      margin: 0;
329      font-size: var(--text-lg);
330    }
331  }
332</style>