web/src/components/things/SmartSuggestions.svelte
146 lines · 3927 bytes
1<script lang="ts">
2 import { onMount } from 'svelte';
3 import type { Category, Service } from '../../types/Service';
4 import { formatLink } from '@utils/parse-markdown';
5 import { 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 categories: Category[];
14 searchTerm: string;
15 }
16 const { categories, searchTerm }: Props = $props();
17
18 let results: ServiceResult[] = $state([]);
19
20 const normalize = (str: string) =>
21 str.toLowerCase().replace(/[^a-z0-9]/g, '');
22
23 onMount(async () => {
24 const apiEndpoint = `${apiBase}/v1/search?q=${encodeURIComponent(searchTerm)}`;
25 const fetchedServices = await fetch(apiEndpoint)
26 .then((response) => response.json())
27 .then((res) =>
28 (res.data || []).map((hit: { name: string }) => normalize(hit.name)),
29 );
30
31 const tmpResults: ServiceResult[] = [];
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);
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(--accent-fg);
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 border-radius: var(--curve-sm);
133
134 font-size: 10px;
135 overflow: hidden;
136 color: var(--accent);
137 }
138
139 .service-link {
140 max-width: 300px;
141 @include truncate;
142 }
143 }
144 }
145 }
146</style>