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