web/src/components/things/SmartSuggestions.svelte

147 lines · 3982 bytes

  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>