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