Commit 36e0e5306e
Unsigned
Layout: unified · split
web/src/components/things/Search.svelte +28 −27
| @@ -1,13 +1,6 @@ | |||
| 1 | <script lang="ts"> | 1 | <script lang="ts"> |
| 2 | import { onMount } from 'svelte'; | ||
| 3 | import Fuse from 'fuse.js'; | ||
| 4 | import { fetchCategories } from '@utils/fetch-data'; | ||
| 5 | import { formatLink } from '@utils/parse-markdown'; | 2 | import { formatLink } from '@utils/parse-markdown'; |
| 6 | import { | 3 | import { runSearch } from '@utils/do-searchy-searchy'; |
| 7 | prepareSearchItems, | ||
| 8 | searchOptions, | ||
| 9 | isRelevant, | ||
| 10 | } from '@utils/do-searchy-searchy'; | ||
| 11 | import type { SearchItem } from '@utils/do-searchy-searchy'; | 4 | import type { SearchItem } from '@utils/do-searchy-searchy'; |
| 12 | 5 | ||
| 13 | interface Props { | 6 | interface Props { |
| @@ -15,13 +8,29 @@ | |||
| 15 | } | 8 | } |
| 16 | const { previousSearch = undefined }: Props = $props(); | 9 | const { previousSearch = undefined }: Props = $props(); |
| 17 | 10 | ||
| 18 | let fuse: Fuse<SearchItem> | null = $state(null); | 11 | let searchQuery = $state(previousSearch ?? ''); |
| 19 | let searchQuery = $state(''); | 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 | }; | ||
| 20 | 26 | ||
| 21 | // Initialize Fuse.js | 27 | // Shares one index with the results island, so both rank identically |
| 22 | onMount(async () => { | 28 | $effect(() => { |
| 23 | const items = prepareSearchItems(await fetchCategories()); | 29 | const query = searchQuery; |
| 24 | fuse = new Fuse(items, searchOptions); | 30 | runSearch(query).then((found) => { |
| 31 | if (query === searchQuery) | ||
| 32 | results = leadWithNamedGroup(found, query).slice(0, 25); | ||
| 33 | }); | ||
| 25 | }); | 34 | }); |
| 26 | 35 | ||
| 27 | const makeResultText = (cat?: string, sec?: string, itm?: string) => { | 36 | const makeResultText = (cat?: string, sec?: string, itm?: string) => { |
| @@ -45,16 +54,6 @@ | |||
| 45 | 54 | ||
| 46 | let activeIndex = $state(-1); | 55 | let activeIndex = $state(-1); |
| 47 | 56 | ||
| 48 | const results: SearchItem[] = $derived( | ||
| 49 | searchQuery && fuse | ||
| 50 | ? fuse | ||
| 51 | .search(searchQuery) | ||
| 52 | .filter(isRelevant) | ||
| 53 | .map((r) => r.item) | ||
| 54 | .slice(0, 25) | ||
| 55 | : [], | ||
| 56 | ); | ||
| 57 | |||
| 58 | function handleKeyDown(event: KeyboardEvent) { | 57 | function handleKeyDown(event: KeyboardEvent) { |
| 59 | if (event.key === 'ArrowDown' || event.key === 'ArrowUp') { | 58 | if (event.key === 'ArrowDown' || event.key === 'ArrowUp') { |
| 60 | event.preventDefault(); | 59 | event.preventDefault(); |
| @@ -71,6 +70,7 @@ | |||
| 71 | } | 70 | } |
| 72 | if (event.key === 'Escape') { | 71 | if (event.key === 'Escape') { |
| 73 | searchQuery = ''; | 72 | searchQuery = ''; |
| 73 | isTyping = false; | ||
| 74 | activeIndex = -1; | 74 | activeIndex = -1; |
| 75 | } | 75 | } |
| 76 | } | 76 | } |
| @@ -90,20 +90,21 @@ | |||
| 90 | </label> | 90 | </label> |
| 91 | <input | 91 | <input |
| 92 | id="search" | 92 | id="search" |
| 93 | placeholder={previousSearch || 'Start typing...'} | 93 | placeholder="Start typing..." |
| 94 | autocomplete="off" | 94 | autocomplete="off" |
| 95 | role="combobox" | 95 | role="combobox" |
| 96 | aria-expanded={results.length > 0} | 96 | aria-expanded={isTyping && results.length > 0} |
| 97 | aria-controls="search-results" | 97 | aria-controls="search-results" |
| 98 | aria-autocomplete="list" | 98 | aria-autocomplete="list" |
| 99 | aria-activedescendant={activeIndex >= 0 | 99 | aria-activedescendant={activeIndex >= 0 |
| 100 | ? `search-result-${activeIndex}` | 100 | ? `search-result-${activeIndex}` |
| 101 | : undefined} | 101 | : undefined} |
| 102 | bind:value={searchQuery} | 102 | bind:value={searchQuery} |
| 103 | oninput={() => (isTyping = true)} | ||
| 103 | onkeydown={handleKeyDown} | 104 | onkeydown={handleKeyDown} |
| 104 | /> | 105 | /> |
| 105 | 106 | ||
| 106 | {#if searchQuery.length > 0} | 107 | {#if isTyping && results.length > 0} |
| 107 | <div class="suggestions"> | 108 | <div class="suggestions"> |
| 108 | <ul id="search-results" role="listbox" aria-label="Search results"> | 109 | <ul id="search-results" role="listbox" aria-label="Search results"> |
| 109 | {#each results as result, i (result.name + result.category + result.sectionName)} | 110 | {#each results as result, i (result.name + result.category + result.sectionName)} |
web/src/components/things/SearchResults.svelte added +295
| @@ -0,0 +1,295 @@ | |||
| 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 | let results: SearchItem[] = $state([]); | ||
| 22 | let loading = $state(true); | ||
| 23 | let sort: Sort = $state('relevance'); | ||
| 24 | |||
| 25 | onMount(async () => { | ||
| 26 | const wanted = new URLSearchParams(location.search).get('sort'); | ||
| 27 | if (wanted === 'category') sort = wanted; | ||
| 28 | try { | ||
| 29 | results = (await runSearch(searchTerm)).filter( | ||
| 30 | (r) => r.type === 'Service', | ||
| 31 | ); | ||
| 32 | } finally { | ||
| 33 | loading = false; | ||
| 34 | } | ||
| 35 | }); | ||
| 36 | |||
| 37 | const setSort = (next: Sort) => { | ||
| 38 | sort = next; | ||
| 39 | const url = new URL(location.href); | ||
| 40 | if (next === 'relevance') url.searchParams.delete('sort'); | ||
| 41 | else url.searchParams.set('sort', next); | ||
| 42 | history.replaceState(null, '', url); | ||
| 43 | }; | ||
| 44 | |||
| 45 | // Group into category > section, keeping the relevance order within each | ||
| 46 | const grouped = $derived.by(() => { | ||
| 47 | const categories: Record<string, Record<string, SearchItem[]>> = {}; | ||
| 48 | for (const item of results) { | ||
| 49 | const sections = (categories[item.category] ??= {}); | ||
| 50 | (sections[item.sectionName || ''] ??= []).push(item); | ||
| 51 | } | ||
| 52 | return Object.entries(categories).map(([name, sections]) => ({ | ||
| 53 | name, | ||
| 54 | sections: Object.entries(sections).map(([sectionName, items]) => ({ | ||
| 55 | sectionName, | ||
| 56 | items, | ||
| 57 | })), | ||
| 58 | })); | ||
| 59 | }); | ||
| 60 | |||
| 61 | const onlySection = $derived( | ||
| 62 | grouped.length === 1 && grouped[0].sections.length === 1 | ||
| 63 | ? { category: grouped[0].name, name: grouped[0].sections[0].sectionName } | ||
| 64 | : null, | ||
| 65 | ); | ||
| 66 | const canGroup = $derived(!onlySection && results.length > 0); | ||
| 67 | const view = $derived(canGroup ? sort : 'relevance'); | ||
| 68 | </script> | ||
| 69 | |||
| 70 | <section class="results-head"> | ||
| 71 | <div> | ||
| 72 | <h3>Results</h3> | ||
| 73 | <p> | ||
| 74 | {#if loading} | ||
| 75 | Searching for "{searchTerm}"... | ||
| 76 | {:else} | ||
| 77 | {results.length} | ||
| 78 | {results.length === 1 ? 'result' : 'results'} for "{searchTerm}" | ||
| 79 | {/if} | ||
| 80 | </p> | ||
| 81 | </div> | ||
| 82 | {#if onlySection} | ||
| 83 | <a | ||
| 84 | class="view-all" | ||
| 85 | href={`/${slugify(onlySection.category)}/${slugify(onlySection.name)}/`} | ||
| 86 | > | ||
| 87 | View all {onlySection.name} → | ||
| 88 | </a> | ||
| 89 | {:else if canGroup} | ||
| 90 | <div class="sort-by" role="group" aria-label="Sort results by"> | ||
| 91 | <span>Sort by:</span> | ||
| 92 | {#each sorts as option (option.key)} | ||
| 93 | <button | ||
| 94 | type="button" | ||
| 95 | class:active={sort === option.key} | ||
| 96 | aria-pressed={sort === option.key} | ||
| 97 | onclick={() => setSort(option.key)} | ||
| 98 | > | ||
| 99 | {option.label} | ||
| 100 | </button> | ||
| 101 | {/each} | ||
| 102 | </div> | ||
| 103 | {/if} | ||
| 104 | </section> | ||
| 105 | |||
| 106 | {#if !loading && !results.length} | ||
| 107 | <section class="nothing-found"> | ||
| 108 | <p>Nothing matched "{searchTerm}"</p> | ||
| 109 | <small> | ||
| 110 | Try a tool's name, the product you're replacing, or a platform. Or browse | ||
| 111 | <a href="/all/">all listings</a>. | ||
| 112 | </small> | ||
| 113 | </section> | ||
| 114 | {:else if view === 'relevance'} | ||
| 115 | <section class="service-grid"> | ||
| 116 | {#each results as item (item.path)} | ||
| 117 | {#if item.service} | ||
| 118 | <ServiceCard | ||
| 119 | service={item.service} | ||
| 120 | categoryName={item.category} | ||
| 121 | sectionName={item.sectionName || ''} | ||
| 122 | /> | ||
| 123 | {/if} | ||
| 124 | {/each} | ||
| 125 | </section> | ||
| 126 | {:else} | ||
| 127 | <section class="by-category"> | ||
| 128 | {#each grouped as category (category.name)} | ||
| 129 | <div class="category"> | ||
| 130 | <a class="category-title" href={`/${slugify(category.name)}/`}> | ||
| 131 | <h3>{category.name}</h3> | ||
| 132 | <FontAwesome iconName={slugify(category.name)} /> | ||
| 133 | </a> | ||
| 134 | {#each category.sections as section (section.sectionName)} | ||
| 135 | <h4> | ||
| 136 | <a | ||
| 137 | href={`/${slugify(category.name)}/${slugify(section.sectionName)}/`} | ||
| 138 | > | ||
| 139 | {section.sectionName} | ||
| 140 | </a> | ||
| 141 | </h4> | ||
| 142 | <div class="service-grid"> | ||
| 143 | {#each section.items as item (item.path)} | ||
| 144 | {#if item.service} | ||
| 145 | <ServiceCard | ||
| 146 | service={item.service} | ||
| 147 | categoryName={item.category} | ||
| 148 | sectionName={item.sectionName || ''} | ||
| 149 | /> | ||
| 150 | {/if} | ||
| 151 | {/each} | ||
| 152 | </div> | ||
| 153 | {/each} | ||
| 154 | </div> | ||
| 155 | {/each} | ||
| 156 | </section> | ||
| 157 | {/if} | ||
| 158 | |||
| 159 | <style lang="scss"> | ||
| 160 | .results-head { | ||
| 161 | display: flex; | ||
| 162 | flex-wrap: wrap; | ||
| 163 | gap: var(--space-md); | ||
| 164 | align-items: end; | ||
| 165 | justify-content: space-between; | ||
| 166 | max-width: var(--width-container); | ||
| 167 | width: calc(100% - 2rem); | ||
| 168 | margin: var(--space-lg) auto var(--space-md) auto; | ||
| 169 | |||
| 170 | h3 { | ||
| 171 | margin: 0; | ||
| 172 | color: var(--accent-3-text); | ||
| 173 | font-size: var(--text-xl); | ||
| 174 | } | ||
| 175 | p { | ||
| 176 | margin: 0; | ||
| 177 | opacity: var(--opacity-muted); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | .view-all { | ||
| 182 | color: var(--accent-3-text); | ||
| 183 | text-decoration: none; | ||
| 184 | font-size: var(--text-sm); | ||
| 185 | padding: var(--space-xs) var(--space-sm); | ||
| 186 | border: var(--border-light); | ||
| 187 | border-radius: var(--curve-sm); | ||
| 188 | background: var(--surface); | ||
| 189 | transition: var(--transition-normal); | ||
| 190 | |||
| 191 | &:hover { | ||
| 192 | border-color: var(--accent-3-text); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | .sort-by { | ||
| 197 | display: flex; | ||
| 198 | align-items: center; | ||
| 199 | gap: var(--space-sm); | ||
| 200 | |||
| 201 | span { | ||
| 202 | opacity: var(--opacity-muted); | ||
| 203 | font-size: var(--text-sm); | ||
| 204 | } | ||
| 205 | button { | ||
| 206 | cursor: pointer; | ||
| 207 | font-family: inherit; | ||
| 208 | font-size: var(--text-sm); | ||
| 209 | padding: var(--space-xs) var(--space-sm); | ||
| 210 | color: var(--foreground); | ||
| 211 | background: var(--surface); | ||
| 212 | border: var(--border-light); | ||
| 213 | border-radius: var(--curve-sm); | ||
| 214 | transition: var(--transition-normal); | ||
| 215 | |||
| 216 | &:hover { | ||
| 217 | border-color: var(--accent-3-text); | ||
| 218 | } | ||
| 219 | &.active { | ||
| 220 | color: var(--accent-3-text); | ||
| 221 | border-color: var(--accent-3-text); | ||
| 222 | font-weight: bold; | ||
| 223 | } | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | .service-grid { | ||
| 228 | display: grid; | ||
| 229 | grid-template-columns: repeat(auto-fill, minmax(350px, 1fr)); | ||
| 230 | gap: var(--space-md); | ||
| 231 | } | ||
| 232 | |||
| 233 | .by-category, | ||
| 234 | .service-grid, | ||
| 235 | .nothing-found { | ||
| 236 | max-width: var(--width-container); | ||
| 237 | width: calc(100% - 2rem); | ||
| 238 | margin: 0 auto var(--space-lg) auto; | ||
| 239 | } | ||
| 240 | |||
| 241 | .by-category { | ||
| 242 | display: flex; | ||
| 243 | flex-direction: column; | ||
| 244 | gap: var(--space-lg); | ||
| 245 | |||
| 246 | .category-title { | ||
| 247 | display: flex; | ||
| 248 | align-items: center; | ||
| 249 | gap: var(--space-sm); | ||
| 250 | text-decoration: none; | ||
| 251 | color: var(--foreground); | ||
| 252 | |||
| 253 | h3 { | ||
| 254 | margin: 0; | ||
| 255 | font-family: var(--font-subtitle); | ||
| 256 | font-size: var(--text-2xl); | ||
| 257 | } | ||
| 258 | :global(svg) { | ||
| 259 | width: var(--text-xl); | ||
| 260 | height: var(--text-xl); | ||
| 261 | color: var(--accent-3-text); | ||
| 262 | opacity: var(--opacity-dim); | ||
| 263 | } | ||
| 264 | } | ||
| 265 | h4 { | ||
| 266 | margin: var(--space-md) 0 var(--space-sm) 0; | ||
| 267 | opacity: var(--opacity-muted); | ||
| 268 | |||
| 269 | a { | ||
| 270 | color: inherit; | ||
| 271 | text-decoration: none; | ||
| 272 | |||
| 273 | &:hover { | ||
| 274 | color: var(--accent-3-text); | ||
| 275 | } | ||
| 276 | } | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | .nothing-found { | ||
| 281 | display: flex; | ||
| 282 | flex-direction: column; | ||
| 283 | gap: var(--space-sm); | ||
| 284 | padding: var(--space-lg); | ||
| 285 | text-align: center; | ||
| 286 | background: var(--surface); | ||
| 287 | border: var(--border-heavy); | ||
| 288 | border-radius: var(--curve-sm); | ||
| 289 | |||
| 290 | p { | ||
| 291 | margin: 0; | ||
| 292 | font-size: var(--text-lg); | ||
| 293 | } | ||
| 294 | } | ||
| 295 | </style> | ||
web/src/components/things/SmartSuggestions.svelte deleted −147
| @@ -1,147 +0,0 @@ | |||
| 1 | <script lang="ts"> | ||
| 2 | import { onMount } from 'svelte'; | ||
| 3 | import type { Service } from '../../types/Service'; | ||
| 4 | import { formatLink } from '@utils/parse-markdown'; | ||
| 5 | import { fetchCategories, slugify } from '@utils/fetch-data'; | ||
| 6 | import { apiBase } from '@utils/api-config'; | ||
| 7 | |||
| 8 | interface ServiceResult extends Service { | ||
| 9 | path: string; | ||
| 10 | } | ||
| 11 | |||
| 12 | interface Props { | ||
| 13 | searchTerm: string; | ||
| 14 | } | ||
| 15 | const { searchTerm }: Props = $props(); | ||
| 16 | |||
| 17 | let results: ServiceResult[] = $state([]); | ||
| 18 | |||
| 19 | const normalize = (str: string) => | ||
| 20 | str.toLowerCase().replace(/[^a-z0-9]/g, ''); | ||
| 21 | |||
| 22 | onMount(async () => { | ||
| 23 | const apiEndpoint = `${apiBase}/v1/search?q=${encodeURIComponent(searchTerm)}`; | ||
| 24 | const fetchedServices = await fetch(apiEndpoint) | ||
| 25 | .then((response) => response.json()) | ||
| 26 | .then((res) => | ||
| 27 | (res.data || []).map((hit: { name: string }) => normalize(hit.name)), | ||
| 28 | ); | ||
| 29 | |||
| 30 | const tmpResults: ServiceResult[] = []; | ||
| 31 | const categories = await fetchCategories(); | ||
| 32 | categories.forEach((category) => { | ||
| 33 | (category.sections || []).forEach((section) => { | ||
| 34 | (section.services || []).forEach((service) => { | ||
| 35 | if (fetchedServices.includes(normalize(service.name))) { | ||
| 36 | const path = `/${slugify(category.name)}/${slugify(section.name)}/${slugify(service.name)}/`; | ||
| 37 | tmpResults.push({ ...service, path }); | ||
| 38 | return; | ||
| 39 | } | ||
| 40 | }); | ||
| 41 | }); | ||
| 42 | }); | ||
| 43 | results = tmpResults; | ||
| 44 | }); | ||
| 45 | </script> | ||
| 46 | |||
| 47 | {#if results.length === 1} | ||
| 48 | <h3>Top Result</h3> | ||
| 49 | {/if} | ||
| 50 | {#if results.length > 1} | ||
| 51 | <h3>Top Results</h3> | ||
| 52 | {/if} | ||
| 53 | <section> | ||
| 54 | {#each results as service (service)} | ||
| 55 | <a class="service-result" href={service.path}> | ||
| 56 | <div class="service-head"> | ||
| 57 | <img | ||
| 58 | width="40" | ||
| 59 | height="40" | ||
| 60 | loading="lazy" | ||
| 61 | decoding="async" | ||
| 62 | class="service-icon" | ||
| 63 | alt={`${service.name} Icon`} | ||
| 64 | data-service-url={formatLink(service.url)} | ||
| 65 | src={service.icon || | ||
| 66 | `https://icon.horse/icon/${formatLink(service.url)}`} | ||
| 67 | /> | ||
| 68 | <div> | ||
| 69 | <h4> | ||
| 70 | {service.name} | ||
| 71 | {#if service.followWith} | ||
| 72 | <p class="follow-with">({service.followWith})</p> | ||
| 73 | {/if} | ||
| 74 | </h4> | ||
| 75 | <span class="service-link">{formatLink(service.url)}</span> | ||
| 76 | </div> | ||
| 77 | </div> | ||
| 78 | </a> | ||
| 79 | {/each} | ||
| 80 | </section> | ||
| 81 | |||
| 82 | <style lang="scss"> | ||
| 83 | @use '../../styles/mixins' as *; | ||
| 84 | h3 { | ||
| 85 | width: 80vw; | ||
| 86 | max-width: 900px; | ||
| 87 | margin: var(--space-md) auto; | ||
| 88 | color: var(--accent-3-text); | ||
| 89 | font-size: var(--text-xl); | ||
| 90 | } | ||
| 91 | section { | ||
| 92 | display: grid; | ||
| 93 | grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); | ||
| 94 | gap: var(--space-md); | ||
| 95 | width: 80vw; | ||
| 96 | max-width: 900px; | ||
| 97 | margin: 0 auto; | ||
| 98 | .service-result { | ||
| 99 | background: var(--surface); | ||
| 100 | padding: var(--space-md); | ||
| 101 | border-radius: var(--curve-sm); | ||
| 102 | border: var(--border-light); | ||
| 103 | box-shadow: var(--shadow-sm); | ||
| 104 | color: var(--foreground); | ||
| 105 | text-decoration: none; | ||
| 106 | |||
| 107 | .service-head { | ||
| 108 | display: flex; | ||
| 109 | align-items: center; | ||
| 110 | gap: var(--space-sm); | ||
| 111 | h4 { | ||
| 112 | margin: 0; | ||
| 113 | font-size: var(--text-xl); | ||
| 114 | display: flex; | ||
| 115 | align-items: center; | ||
| 116 | gap: var(--space-sm); | ||
| 117 | .follow-with { | ||
| 118 | opacity: var(--opacity-soft); | ||
| 119 | font-style: italic; | ||
| 120 | margin: 0; | ||
| 121 | font-size: var(--text-sm); | ||
| 122 | max-width: 100px; | ||
| 123 | display: -webkit-box; | ||
| 124 | -webkit-line-clamp: 2; | ||
| 125 | -webkit-box-orient: vertical; | ||
| 126 | overflow: hidden; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | .service-icon { | ||
| 130 | width: 2.5rem; | ||
| 131 | height: 2.5rem; | ||
| 132 | object-fit: contain; | ||
| 133 | border-radius: var(--curve-sm); | ||
| 134 | |||
| 135 | font-size: 10px; | ||
| 136 | overflow: hidden; | ||
| 137 | color: var(--accent-text); | ||
| 138 | } | ||
| 139 | |||
| 140 | .service-link { | ||
| 141 | max-width: 300px; | ||
| 142 | @include truncate; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | } | ||
| 146 | } | ||
| 147 | </style> | ||
web/src/pages/api/awesome-privacy.json.ts +20 −11
| @@ -7,18 +7,27 @@ const awesomePrivacyYamlPath = | |||
| 7 | 'https://raw.githubusercontent.com/Lissy93/awesome-privacy/main/awesome-privacy.yml'; | 7 | 'https://raw.githubusercontent.com/Lissy93/awesome-privacy/main/awesome-privacy.yml'; |
| 8 | 8 | ||
| 9 | export const GET: APIRoute = async () => { | 9 | export const GET: APIRoute = async () => { |
| 10 | const yamlContent = await fetch(awesomePrivacyYamlPath) | 10 | const response = await fetch(awesomePrivacyYamlPath).catch(() => null); |
| 11 | .then((response) => response.text()) | 11 | const data = response?.ok |
| 12 | .catch((error) => { | 12 | ? (yaml.load(await response.text()) as AwesomePrivacy) |
| 13 | return JSON.stringify({ | 13 | : null; |
| 14 | error: 'Failed to fetch YAML file', | ||
| 15 | details: error, | ||
| 16 | }); | ||
| 17 | }); | ||
| 18 | 14 | ||
| 19 | const yamlObject = yaml.load(yamlContent) as AwesomePrivacy; | 15 | // Caching a failed fetch would serve a broken dataset for the whole max-age |
| 16 | if (!data?.categories) { | ||
| 17 | return new Response(JSON.stringify({ error: 'Failed to fetch dataset' }), { | ||
| 18 | status: 502, | ||
| 19 | headers: { | ||
| 20 | 'content-type': 'application/json', | ||
| 21 | 'cache-control': 'no-store', | ||
| 22 | }, | ||
| 23 | }); | ||
| 24 | } | ||
| 20 | 25 | ||
| 21 | return new Response(JSON.stringify(yamlObject), { | 26 | return new Response(JSON.stringify(data), { |
| 22 | headers: { 'content-type': 'application/json' }, | 27 | headers: { |
| 28 | 'content-type': 'application/json', | ||
| 29 | // ~250KB fetched from GitHub raw, and it only changes on merge | ||
| 30 | 'cache-control': 'public, max-age=300, stale-while-revalidate=86400', | ||
| 31 | }, | ||
| 23 | }); | 32 | }); |
| 24 | }; | 33 | }; |
web/src/pages/search/[...searchTerm].astro +5 −178
| @@ -1,121 +1,23 @@ | |||
| 1 | --- | 1 | --- |
| 2 | import Fuse from 'fuse.js'; | ||
| 3 | |||
| 4 | import Layout from '@layouts/Layout.astro'; | 2 | import Layout from '@layouts/Layout.astro'; |
| 5 | import { fetchData, slugify } from '@utils/fetch-data'; | ||
| 6 | import { | ||
| 7 | prepareSearchItems, | ||
| 8 | searchOptions, | ||
| 9 | isRelevant, | ||
| 10 | } from '@utils/do-searchy-searchy'; | ||
| 11 | import type { SearchItem } from '@utils/do-searchy-searchy'; | ||
| 12 | import Search from '@components/things/Search.svelte'; | 3 | import Search from '@components/things/Search.svelte'; |
| 13 | import SmartSuggestions from '@components/things/SmartSuggestions.svelte'; | 4 | import SearchResults from '@components/things/SearchResults.svelte'; |
| 14 | import FontAwesome from '@components/form/FontAwesome.svelte'; | ||
| 15 | 5 | ||
| 16 | export const prerender = false; | 6 | export const prerender = false; |
| 17 | 7 | ||
| 18 | let fuse: Fuse<SearchItem>; | 8 | const searchTerm = Astro.params.searchTerm || ''; |
| 19 | |||
| 20 | const categories = (await fetchData())?.categories; | ||
| 21 | |||
| 22 | const items = prepareSearchItems(categories); | ||
| 23 | |||
| 24 | fuse = new Fuse(items, searchOptions); | ||
| 25 | |||
| 26 | const searchTerm = Astro.params.searchTerm; | ||
| 27 | |||
| 28 | const searchResults = fuse | ||
| 29 | .search(searchTerm || '') | ||
| 30 | .filter(isRelevant) | ||
| 31 | .map((result) => result.item); | ||
| 32 | |||
| 33 | const services = searchResults.filter((result) => result.type === 'Service'); | ||
| 34 | |||
| 35 | interface GroupedSection { | ||
| 36 | sectionName: string; | ||
| 37 | items: SearchItem[]; | ||
| 38 | } | ||
| 39 | |||
| 40 | interface GroupedCategory { | ||
| 41 | categoryName: string; | ||
| 42 | sections: Record<string, GroupedSection>; | ||
| 43 | } | ||
| 44 | |||
| 45 | const putResultsIntoGroups = () => { | ||
| 46 | const grouped: Record<string, GroupedCategory> = {}; | ||
| 47 | |||
| 48 | for (const item of services) { | ||
| 49 | const categoryName = item.category; | ||
| 50 | const sectionName = item.sectionName || ''; | ||
| 51 | |||
| 52 | if (!grouped[categoryName]) { | ||
| 53 | grouped[categoryName] = { categoryName, sections: {} }; | ||
| 54 | } | ||
| 55 | |||
| 56 | if (!grouped[categoryName].sections[sectionName]) { | ||
| 57 | grouped[categoryName].sections[sectionName] = { sectionName, items: [] }; | ||
| 58 | } | ||
| 59 | |||
| 60 | grouped[categoryName].sections[sectionName].items.push(item); | ||
| 61 | } | ||
| 62 | |||
| 63 | return Object.values(grouped).map((category) => ({ | ||
| 64 | categoryName: category.categoryName, | ||
| 65 | sections: Object.values(category.sections), | ||
| 66 | })); | ||
| 67 | }; | ||
| 68 | |||
| 69 | const beer = putResultsIntoGroups(); | ||
| 70 | --- | 9 | --- |
| 71 | 10 | ||
| 72 | <Layout title={`Search: ${searchTerm} | Awesome Privacy`}> | 11 | <Layout title={`Search: ${searchTerm} | Awesome Privacy`}> |
| 73 | <section> | 12 | <section class="search-box"> |
| 74 | <h1>Search</h1> | 13 | <h1>Search</h1> |
| 75 | <Search client:visible previousSearch={searchTerm} /> | 14 | <Search client:visible previousSearch={searchTerm} /> |
| 76 | </section> | 15 | </section> |
| 77 | <SmartSuggestions client:visible searchTerm={searchTerm || ''} /> | 16 | <SearchResults client:load searchTerm={searchTerm} /> |
| 78 | <section class="result-count"> | ||
| 79 | <h3>Deep Search</h3> | ||
| 80 | <p> | ||
| 81 | Showing {services.length} results for "{searchTerm}" sorted by relevence | ||
| 82 | </p> | ||
| 83 | </section> | ||
| 84 | <div class="results"> | ||
| 85 | { | ||
| 86 | beer.map((category) => ( | ||
| 87 | <div class="category"> | ||
| 88 | <a | ||
| 89 | class="category-title" | ||
| 90 | href={`/${slugify(category.categoryName)}/`} | ||
| 91 | > | ||
| 92 | <h3>{category.categoryName}</h3> | ||
| 93 | </a> | ||
| 94 | <span class="section-icon"> | ||
| 95 | <FontAwesome iconName={slugify(category.categoryName)} /> | ||
| 96 | </span> | ||
| 97 | <ul class="section-list"> | ||
| 98 | {category.sections.map((section) => ( | ||
| 99 | <li class="section-item"> | ||
| 100 | <h4>{section.sectionName}</h4> | ||
| 101 | <ul class="service-list"> | ||
| 102 | {section.items.map((item) => ( | ||
| 103 | <li> | ||
| 104 | <a href={item.path}>{item.name}</a> | ||
| 105 | </li> | ||
| 106 | ))} | ||
| 107 | </ul> | ||
| 108 | </li> | ||
| 109 | ))} | ||
| 110 | </ul> | ||
| 111 | </div> | ||
| 112 | )) | ||
| 113 | } | ||
| 114 | </div> | ||
| 115 | </Layout> | 17 | </Layout> |
| 116 | 18 | ||
| 117 | <style lang="scss"> | 19 | <style lang="scss"> |
| 118 | section { | 20 | .search-box { |
| 119 | display: flex; | 21 | display: flex; |
| 120 | flex-direction: column; | 22 | flex-direction: column; |
| 121 | margin: var(--space-lg) auto; | 23 | margin: var(--space-lg) auto; |
| @@ -131,79 +33,4 @@ const beer = putResultsIntoGroups(); | |||
| 131 | margin: var(--space-md) 0; | 33 | margin: var(--space-md) 0; |
| 132 | } | 34 | } |
| 133 | } | 35 | } |
| 134 | |||
| 135 | .result-count { | ||
| 136 | padding: 0; | ||
| 137 | width: calc(100% - 2rem); | ||
| 138 | margin: var(--space-md) auto 0 auto; | ||
| 139 | h3 { | ||
| 140 | margin: 0; | ||
| 141 | color: var(--accent-3-text); | ||
| 142 | font-size: var(--text-xl); | ||
| 143 | } | ||
| 144 | p { | ||
| 145 | margin: 0; | ||
| 146 | opacity: var(--opacity-muted); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | .results { | ||
| 151 | margin: 0 auto var(--space-lg) auto; | ||
| 152 | padding: var(--space-md); | ||
| 153 | width: calc(100% - 2rem); | ||
| 154 | max-width: var(--width-container); | ||
| 155 | columns: 6 300px; | ||
| 156 | column-gap: var(--space-md); | ||
| 157 | .category { | ||
| 158 | padding: var(--space-md); | ||
| 159 | box-shadow: var(--shadow-md); | ||
| 160 | background: var(--surface); | ||
| 161 | border: var(--border-heavy); | ||
| 162 | border-radius: var(--curve-sm); | ||
| 163 | display: inline-flex; | ||
| 164 | flex-direction: column; | ||
| 165 | width: 85%; | ||
| 166 | margin: var(--space-md); | ||
| 167 | position: relative; | ||
| 168 | .category-title { | ||
| 169 | text-decoration: none; | ||
| 170 | color: var(--foreground); | ||
| 171 | z-index: 2; | ||
| 172 | position: relative; | ||
| 173 | h3 { | ||
| 174 | font-family: var(--font-subtitle); | ||
| 175 | font-weight: bold; | ||
| 176 | margin: 0; | ||
| 177 | font-size: var(--text-2xl); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | .section-icon { | ||
| 181 | position: absolute; | ||
| 182 | right: var(--space-sm); | ||
| 183 | top: var(--space-sm); | ||
| 184 | width: fit-content; | ||
| 185 | :global(svg) { | ||
| 186 | width: var(--text-3xl); | ||
| 187 | height: var(--text-3xl); | ||
| 188 | opacity: var(--opacity-dim); | ||
| 189 | text-shadow: 3px 3px 0 black; | ||
| 190 | color: var(--accent-3-text); | ||
| 191 | transition: var(--transition-normal); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | .section-list { | ||
| 195 | list-style: none; | ||
| 196 | padding: 0; | ||
| 197 | margin: 0; | ||
| 198 | h4 { | ||
| 199 | margin: var(--space-md) 0 0 0; | ||
| 200 | } | ||
| 201 | .service-list { | ||
| 202 | list-style: none; | ||
| 203 | padding: 0; | ||
| 204 | margin: 0; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | } | ||
| 208 | } | ||
| 209 | </style> | 36 | </style> |
web/src/utils/do-searchy-searchy.ts +28 −5
| @@ -1,5 +1,7 @@ | |||
| 1 | import type { Category } from '../types/Service'; | 1 | import Fuse from 'fuse.js'; |
| 2 | import { slugify } from './fetch-data'; | 2 | |
| 3 | import type { Category, Service } from '../types/Service'; | ||
| 4 | import { fetchCategories, slugify } from './fetch-data'; | ||
| 3 | 5 | ||
| 4 | export interface SearchItem { | 6 | export interface SearchItem { |
| 5 | type: 'Category' | 'Section' | 'Service'; | 7 | type: 'Category' | 'Section' | 'Service'; |
| @@ -17,6 +19,7 @@ export interface SearchItem { | |||
| 17 | platforms?: string[]; | 19 | platforms?: string[]; |
| 18 | traits?: string[]; | 20 | traits?: string[]; |
| 19 | alternativeTo?: string[]; | 21 | alternativeTo?: string[]; |
| 22 | service?: Service; | ||
| 20 | } | 23 | } |
| 21 | 24 | ||
| 22 | export const prepareSearchItems = (categories: Category[]): SearchItem[] => { | 25 | export const prepareSearchItems = (categories: Category[]): SearchItem[] => { |
| @@ -34,7 +37,7 @@ export const prepareSearchItems = (categories: Category[]): SearchItem[] => { | |||
| 34 | }); | 37 | }); |
| 35 | 38 | ||
| 36 | // Add section with category context | 39 | // Add section with category context |
| 37 | category.sections.forEach((section) => { | 40 | (category.sections || []).forEach((section) => { |
| 38 | const sectionPath = `${categoryPath}${slugify(section.name)}/`; | 41 | const sectionPath = `${categoryPath}${slugify(section.name)}/`; |
| 39 | items.push({ | 42 | items.push({ |
| 40 | type: 'Section', | 43 | type: 'Section', |
| @@ -59,7 +62,7 @@ export const prepareSearchItems = (categories: Category[]): SearchItem[] => { | |||
| 59 | sectionName: section.name, | 62 | sectionName: section.name, |
| 60 | path: `${sectionPath}${slugify(service.name)}/`, | 63 | path: `${sectionPath}${slugify(service.name)}/`, |
| 61 | logo: service.icon || '', | 64 | logo: service.icon || '', |
| 62 | // Get the plafrorm from followWith field or android/ios app | 65 | // Get the platform from followWith field or android/ios app |
| 63 | platforms: Object.keys(service) | 66 | platforms: Object.keys(service) |
| 64 | .filter((key) => key.endsWith('App')) | 67 | .filter((key) => key.endsWith('App')) |
| 65 | .map((key) => key.replace(/App$/, '')) | 68 | .map((key) => key.replace(/App$/, '')) |
| @@ -72,6 +75,8 @@ export const prepareSearchItems = (categories: Category[]): SearchItem[] => { | |||
| 72 | ), | 75 | ), |
| 73 | // The proprietary tools this section replaces, eg "whatsapp" | 76 | // The proprietary tools this section replaces, eg "whatsapp" |
| 74 | alternativeTo: section.alternativeTo || [], | 77 | alternativeTo: section.alternativeTo || [], |
| 78 | // Kept so results can render a card without a second lookup | ||
| 79 | service, | ||
| 75 | }); | 80 | }); |
| 76 | }); | 81 | }); |
| 77 | }); | 82 | }); |
| @@ -79,7 +84,7 @@ export const prepareSearchItems = (categories: Category[]): SearchItem[] => { | |||
| 79 | return items; | 84 | return items; |
| 80 | }; | 85 | }; |
| 81 | 86 | ||
| 82 | // Set the fuzziness power. 0 = perfect match, and 1 = barley matched | 87 | // Set the fuzziness power. 0 = perfect match, and 1 = barely matched |
| 83 | export const SEARCH_SCORE_CUTOFF = 0.85; | 88 | export const SEARCH_SCORE_CUTOFF = 0.85; |
| 84 | 89 | ||
| 85 | export const isRelevant = (result: { score?: number }): boolean => | 90 | export const isRelevant = (result: { score?: number }): boolean => |
| @@ -103,3 +108,21 @@ export const searchOptions = { | |||
| 103 | { name: 'description', weight: 0.1 }, | 108 | { name: 'description', weight: 0.1 }, |
| 104 | ], | 109 | ], |
| 105 | }; | 110 | }; |
| 111 | |||
| 112 | // Built once per page, and shared by the search box and the results island | ||
| 113 | let indexPromise: Promise<Fuse<SearchItem>> | null = null; | ||
| 114 | |||
| 115 | export const getSearchIndex = (): Promise<Fuse<SearchItem>> => | ||
| 116 | (indexPromise ??= fetchCategories().then((categories) => { | ||
| 117 | // Caching a failed fetch would leave search empty for the whole session | ||
| 118 | if (!categories.length) indexPromise = null; | ||
| 119 | return new Fuse(prepareSearchItems(categories), searchOptions); | ||
| 120 | })); | ||
| 121 | |||
| 122 | export const runSearch = async (term: string): Promise<SearchItem[]> => | ||
| 123 | term | ||
| 124 | ? (await getSearchIndex()) | ||
| 125 | .search(term) | ||
| 126 | .filter(isRelevant) | ||
| 127 | .map((r) => r.item) | ||
| 128 | : []; | ||