web/src/components/things/SmartSuggestions.svelte

145 lines · 3882 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
  7  interface ServiceResult extends Service {
  8    path: string;
  9  }
 10
 11  interface Props {
 12    categories: Category[];
 13    searchTerm: string;
 14  }
 15  const { categories, 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 = `https://awesome-privacy.as93.workers.dev/${searchTerm}`;
 24    const fetchedServices = await fetch(apiEndpoint)
 25      .then((response) => response.json())
 26      .then((data) =>
 27        (JSON.parse(data) || []).map((servName: string) => normalize(servName)),
 28      );
 29
 30    const tmpResults: ServiceResult[] = [];
 31    categories.forEach((category) => {
 32      (category.sections || []).forEach((section) => {
 33        (section.services || []).forEach((service) => {
 34          if (fetchedServices.includes(normalize(service.name))) {
 35            const path = `/${slugify(category.name)}/${slugify(section.name)}/${slugify(service.name)}`;
 36            tmpResults.push({ ...service, path });
 37            return;
 38          }
 39        });
 40      });
 41    });
 42    results = tmpResults;
 43  });
 44</script>
 45
 46{#if results.length === 1}
 47  <h3>Top Result</h3>
 48{/if}
 49{#if results.length > 1}
 50  <h3>Top Results</h3>
 51{/if}
 52<section>
 53  {#each results as service (service)}
 54    <a class="service-result" href={service.path}>
 55      <div class="service-head">
 56        <img
 57          width="40"
 58          height="40"
 59          loading="lazy"
 60          decoding="async"
 61          class="service-icon"
 62          alt={`${service.name} Icon`}
 63          data-service-url={formatLink(service.url)}
 64          src={service.icon ||
 65            `https://icon.horse/icon/${formatLink(service.url)}`}
 66        />
 67        <div>
 68          <h4>
 69            {service.name}
 70            {#if service.followWith}
 71              <p class="follow-with">({service.followWith})</p>
 72            {/if}
 73          </h4>
 74          <span class="service-link">{formatLink(service.url)}</span>
 75        </div>
 76      </div>
 77    </a>
 78  {/each}
 79</section>
 80
 81<style lang="scss">
 82  @use '../../styles/mixins' as *;
 83  h3 {
 84    width: 80vw;
 85    max-width: 900px;
 86    margin: var(--space-md) auto;
 87    color: var(--accent-3);
 88    font-size: var(--text-xl);
 89  }
 90  section {
 91    display: grid;
 92    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
 93    gap: var(--space-md);
 94    width: 80vw;
 95    max-width: 900px;
 96    margin: 0 auto;
 97    .service-result {
 98      background: var(--accent-fg);
 99      padding: var(--space-md);
100      border-radius: var(--curve-sm);
101      border: var(--border-light);
102      box-shadow: var(--shadow-sm);
103      color: var(--foreground);
104      text-decoration: none;
105
106      .service-head {
107        display: flex;
108        align-items: center;
109        gap: var(--space-sm);
110        h4 {
111          margin: 0;
112          font-size: var(--text-xl);
113          display: flex;
114          align-items: center;
115          gap: var(--space-sm);
116          .follow-with {
117            opacity: var(--opacity-soft);
118            font-style: italic;
119            margin: 0;
120            font-size: var(--text-sm);
121            max-width: 100px;
122            display: -webkit-box;
123            -webkit-line-clamp: 2;
124            -webkit-box-orient: vertical;
125            overflow: hidden;
126          }
127        }
128        .service-icon {
129          width: 2.5rem;
130          height: 2.5rem;
131          border-radius: var(--curve-sm);
132
133          font-size: 10px;
134          overflow: hidden;
135          color: var(--accent);
136        }
137
138        .service-link {
139          max-width: 300px;
140          @include truncate;
141        }
142      }
143    }
144  }
145</style>