web/src/components/things/Search.svelte

217 lines · 5698 bytes

  1<script lang="ts">
  2  import { onMount } from 'svelte';
  3  import Fuse from 'fuse.js';
  4  import { slugify } from '@utils/fetch-data';
  5  import type { Category, Section, Service, ShortService } from '../../types/Service';
  6  import { formatLink } from '@utils/parse-markdown';
  7  import { prepareSearchItems, searchOptions } from '@utils/do-searchy-searchy';
  8
  9  export let data: Category[];
 10  export let previousSearch: string | undefined = undefined;
 11
 12  let fuse: Fuse<any>;
 13  let searchQuery = '';
 14  let results: any[] = [];
 15
 16  // Initialize Fuse.js
 17  onMount(() => {
 18    const items = prepareSearchItems(data);
 19    fuse = new Fuse(items, searchOptions);
 20  });
 21
 22  const makeResultLink = (cat?: string, sec?: string, itm?: string) => {
 23    if (!cat) return '/'
 24    if (!sec) return `/${slugify(cat)}`
 25    if (!itm) return `/${slugify(cat)}/${slugify(sec)}`
 26    return `/${slugify(cat)}/${slugify(sec)}/${slugify(itm)}`;
 27  };
 28
 29  const makeResultText = (cat?: string, sec?: string, itm?: string) => {
 30    if (itm) return itm;
 31    if (sec) return sec;
 32    if (cat) return cat;
 33    return '';
 34  };
 35
 36  const makeLogoSrc = (logo: string, url: string) => {
 37    if (!logo && !url) return '/broken-image.png';
 38    return logo || `https://icon.horse/icon/${formatLink(url)}`;
 39  };
 40
 41  const makeTitle = (typ: string, desc: string) => {
 42    if (desc && typ === 'Service') {
 43      return `${desc.slice(0, 60)}...`
 44    }
 45    return '';
 46  };
 47
 48  function handleKeyDown(event: KeyboardEvent) {
 49    if (event.key === 'Enter') {
 50      event.preventDefault();
 51      if (window) {
 52        window.location.href = `/search/${encodeURIComponent(searchQuery)}`;
 53      }
 54    }
 55    if (event.key === 'Escape') {
 56      searchQuery = '';
 57    }
 58  }
 59
 60  // Watch for changes in the search query and update results
 61  $: if (searchQuery) {
 62    results = fuse.search(searchQuery).map(result => result.item).splice(0, 25);
 63  } else {
 64    results = [];
 65  }
 66</script>
 67
 68<div class="search-wrap">
 69  <label for="search">
 70    What are you looking for?
 71    {#if searchQuery.length > 0}
 72      <span class="enter-hint">Press enter to view all results</span>
 73    {/if}
 74  </label>
 75  <input
 76    id="search"
 77    placeholder={previousSearch || 'Start typing...'}
 78    autocomplete="off"
 79    bind:value={searchQuery}
 80    on:keydown={handleKeyDown}
 81  />
 82  
 83  
 84  {#if searchQuery.length > 0}
 85    <div class="suggestions">
 86      <ul>
 87        {#each results as result}
 88        <li class="result-row">
 89          <a
 90            href={makeResultLink(result.category, result.sectionName, result.name)}
 91            title={makeTitle(result.type, result.description)}
 92            >
 93            <span class="name">
 94              {#if result.type === 'Service'}
 95                <img src={makeLogoSrc(result.logo, result.url)} alt={result.name} width="20" height="20" loading="lazy" />
 96              {/if}
 97              
 98              {makeResultText(result.category, result.sectionName, result.name)}
 99              
100              {#if result.itemCount}
101                <i>({result.itemCount})</i>
102              {/if}
103            </span>
104            <span class="path">
105              {result.category ? `${result.category}` : ''}
106              {result.sectionName ? `➔ ${result.sectionName}` : ''} 
107              {result.name ? `➔ ${result.name}` : ''} 
108            </span>
109          </a>
110        </li>
111        {/each}
112      </ul>
113    </div>
114  {/if}
115</div>
116
117<style lang="scss">
118
119.search-wrap {
120  display: flex;
121  flex-direction: column;
122  position: relative;
123  margin: 1rem auto;
124  max-width: 900px;
125  margin: 0 auto;
126  width: 80vw;
127  label {
128    margin: 0.5rem 0;
129    display: flex;
130    justify-content: space-between;
131    align-items: center;
132    flex-wrap: wrap;
133    .enter-hint {
134      font-size:0.8rem;
135      opacity: 0.7;
136    }
137  }
138
139  input {
140    padding: 0.5rem 1rem;
141    font-size: 1.8rem;
142    border: 2px solid var(--box-outline);
143    border-radius: var(--curve-lg);
144    box-shadow: 3px 3px 0 var(--box-outline);
145    z-index: 4;
146    background: var(--accent-fg);
147    color: var(--foreground);
148    &:focus {
149      outline: none;
150      border-color: var(--accent);
151      box-shadow: 3px 3px 0 var(--accent);
152    }
153  }
154
155  .suggestions {
156    ul {
157      position: absolute;
158      background: var(--background-form);
159      z-index: 3;
160      width: 100%;
161
162      list-style: none;
163      padding: 0;
164      margin: 0;
165      border: 2px solid var(--box-outline);
166      border-radius: 0 0 var(--curve-lg) var(--curve-lg);
167      box-shadow: 3px 3px 0 var(--box-outline);
168      transform: translateY(-0.5rem);
169      max-height: 500px;
170      overflow-y: scroll;
171      background: var(--background-form);
172      li.result-row {
173        padding: 0.5rem 1rem;
174        margin: 0.5rem 0;
175        a {
176          color: var(--foreground);
177          text-decoration: none;
178          display: flex;
179          justify-content: space-between;
180          flex-wrap: wrap;
181          .name {
182            display: flex;
183            align-items: center;
184            gap: 0.5rem;
185            i {
186              color: var(--accent);
187              font-weight: bold;
188              font-style: normal;
189            }
190            img {
191              border-radius: var(--curve-md);
192              width: 1.25rem;
193              height: 1.25rem;
194              font-size: 10px;
195              color: var(--accent);
196              overflow: hidden;
197              background: #f453974d;
198              padding: 1px;
199            }
200          }
201          .path {
202            font-size: 0.85rem;
203            opacity: 0.7;
204          }
205        }
206        &:hover {
207          background: var(--accent);
208          .name i {
209            color: var(--accent-fg);
210          }
211        }
212      }
213    }
214  }
215}
216
217</style>