web/src/components/things/Search.svelte
215 lines · 5572 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(--foreground);
143 border-radius: var(--curve-lg);
144 box-shadow: 3px 3px 0 var(--foreground);
145 z-index: 4;
146 &:focus {
147 outline: none;
148 border-color: var(--accent);
149 box-shadow: 3px 3px 0 var(--accent);
150 }
151 }
152
153 .suggestions {
154 ul {
155 position: absolute;
156 background: white;
157 z-index: 3;
158 width: 100%;
159
160 list-style: none;
161 padding: 0;
162 margin: 0;
163 border: 2px solid var(--foreground);
164 border-radius: 0 0 var(--curve-lg) var(--curve-lg);
165 box-shadow: 3px 3px 0 var(--foreground);
166 transform: translateY(-0.5rem);
167 max-height: 500px;
168 overflow-y: scroll;
169
170 li.result-row {
171 padding: 0.5rem 1rem;
172 margin: 0.5rem 0;
173 a {
174 color: var(--foreground);
175 text-decoration: none;
176 display: flex;
177 justify-content: space-between;
178 flex-wrap: wrap;
179 .name {
180 display: flex;
181 align-items: center;
182 gap: 0.5rem;
183 i {
184 color: var(--accent);
185 font-weight: bold;
186 font-style: normal;
187 }
188 img {
189 border-radius: var(--curve-md);
190 width: 1.25rem;
191 height: 1.25rem;
192 font-size: 10px;
193 color: var(--accent);
194 overflow: hidden;
195 background: #f453974d;
196 padding: 1px;
197 }
198 }
199 .path {
200 font-size: 0.85rem;
201 opacity: 0.7;
202 }
203 }
204 &:hover {
205 background: var(--accent);
206 .name i {
207 color: var(--accent-fg);
208 }
209 }
210 }
211 }
212 }
213}
214
215</style>