web/src/components/things/SmartSuggestions.svelte

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