web/src/components/things/SmartSuggestions.svelte

147 lines · 3901 bytes

  1<script lang="ts">
  2  import { onMount } from 'svelte';
  3  import { writable } from 'svelte/store';
  4  import type { Category, Service } from '../../types/Service';
  5  import { formatLink } from '@utils/parse-markdown';
  6  import { slugify } from '@utils/fetch-data';
  7
  8  interface ServiceResult extends Service {
  9    path: string;
 10  }
 11
 12  export let categories: Category[];
 13  export let searchTerm: string;
 14
 15  let results = writable<ServiceResult[]>([]);
 16
 17  const normalize = (str: string) =>
 18    str.toLowerCase().replace(/[^a-z0-9]/g, '');
 19
 20  onMount(async () => {
 21    const apiEndpoint = `https://awesome-privacy.as93.workers.dev/${searchTerm}`;
 22    const fetchedServices = await fetch(apiEndpoint)
 23      .then((response) => response.json())
 24      .then((data) =>
 25        (JSON.parse(data) || []).map((servName: string) => normalize(servName)),
 26      );
 27
 28    const tmpResults: ServiceResult[] = [];
 29    categories.forEach((category) => {
 30      (category.sections || []).forEach((section) => {
 31        (section.services || []).forEach((service) => {
 32          if (fetchedServices.includes(normalize(service.name))) {
 33            const path = `/${slugify(category.name)}/${slugify(section.name)}/${slugify(service.name)}`;
 34            tmpResults.push({ ...service, path });
 35            return;
 36          }
 37        });
 38      });
 39    });
 40    results.set(tmpResults);
 41  });
 42</script>
 43
 44{#if $results.length === 1}
 45  <h3>Top Result</h3>
 46{/if}
 47{#if $results.length > 1}
 48  <h3>Top Results</h3>
 49{/if}
 50<section>
 51  {#each $results as service (service)}
 52    <a class="service-result" href={service.path}>
 53      <div class="service-head">
 54        <img
 55          width="40"
 56          height="40"
 57          loading="lazy"
 58          decoding="async"
 59          class="service-icon"
 60          alt={`${service.name} Icon`}
 61          data-service-url={formatLink(service.url)}
 62          src={service.icon ||
 63            `https://icon.horse/icon/${formatLink(service.url)}`}
 64        />
 65        <div>
 66          <h4>
 67            {service.name}
 68            {#if service.followWith}
 69              <p class="follow-with">({service.followWith})</p>
 70            {/if}
 71          </h4>
 72          <a class="service-link" href={service.url}
 73            >{formatLink(service.url)}</a
 74          >
 75        </div>
 76      </div>
 77    </a>
 78  {/each}
 79</section>
 80
 81<style lang="scss">
 82  h3 {
 83    width: 80vw;
 84    max-width: 900px;
 85    margin: 1rem auto;
 86    color: var(--accent-3);
 87    font-size: 1.6rem;
 88  }
 89  section {
 90    display: grid;
 91    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
 92    gap: 1rem;
 93    width: 80vw;
 94    max-width: 900px;
 95    margin: 0 auto;
 96    .service-result {
 97      background: var(--accent-fg);
 98      padding: 1rem;
 99      border-radius: var(--curve-sm);
100      border: 1px solid var(--box-outline);
101      box-shadow: 3px 3px 0 var(--box-outline);
102      color: var(--foreground);
103      text-decoration: none;
104
105      .service-head {
106        display: flex;
107        align-items: center;
108        gap: 0.5rem;
109        h4 {
110          margin: 0;
111          font-size: 1.6rem;
112          display: flex;
113          align-items: center;
114          gap: 0.5rem;
115          .follow-with {
116            opacity: 0.7;
117            font-style: italic;
118            margin: 0;
119            font-size: 0.85rem;
120            font-weight: 400;
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          text-overflow: ellipsis;
141          overflow: hidden;
142          white-space: nowrap;
143        }
144      }
145    }
146  }
147</style>