web/src/components/things/SavedServices.svelte

98 lines · 2574 bytes

 1<script lang="ts">
 2  import { onMount } from 'svelte';
 3  import { writable } from 'svelte/store';
 4
 5  import type { Category, Service } from '../../types/Service';
 6  import { slugify } from '@utils/fetch-data';
 7  import ServiceCard from './ServiceCard.svelte';
 8
 9  export let allData: Category[];
10  export let serviceList: string[] | null = null;
11
12  interface SavedServices {
13    category: string;
14    section: string;
15    service: Service;
16  }
17
18  const savedServices = writable<SavedServices[]>([]);
19
20  onMount(async () => {
21    const results: SavedServices[] = [];
22    const saved =
23      serviceList || JSON.parse(localStorage.getItem('savedServices') || '[]');
24    saved.forEach((serviceId: string) => {
25      const parts = serviceId.split('/');
26      const categoryName = parts[0];
27      const sectionName = parts[1];
28      const serviceName = parts[2];
29
30      const category = allData.find(
31        (category) => slugify(category.name) === categoryName,
32      );
33      if (!category) return;
34      const section = category.sections.find(
35        (section) => slugify(section.name) === sectionName,
36      );
37      if (!section) return;
38      const service = section.services.find(
39        (service) => slugify(service.name) === serviceName,
40      );
41      if (!service) return;
42      results.push({ category: category.name, section: section.name, service });
43    });
44    savedServices.set(results || []);
45  });
46</script>
47
48<div>
49  {#if $savedServices.length > 0}
50    <div class="saved-services">
51      {#each $savedServices as thingy (thingy.service.name + thingy.section)}
52        <ServiceCard
53          categoryName={thingy.category}
54          sectionName={thingy.section}
55          service={thingy.service}
56        />
57      {/each}
58    </div>
59  {:else if !serviceList}
60    <div class="nothing-yet">
61      <p>
62        Here you'll find a list of all the software and services you've
63        bookmarked.
64      </p>
65      <small>
66        All data is stored on-device, in your browser's local storage, and not
67        sent anywhere unless you choose to share it
68      </small>
69      <p class="nope">Nothing saved yet!</p>
70    </div>
71  {/if}
72</div>
73
74<style lang="scss">
75  .saved-services {
76    display: grid;
77    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
78    gap: 1rem;
79    margin-top: 1rem;
80  }
81
82  .nothing-yet {
83    text-align: center;
84    p {
85      margin: 0;
86    }
87    small {
88      font-size: 0.8rem;
89      opacity: 0.6;
90    }
91    .nope {
92      font-weight: bold;
93      margin: 2rem 0;
94      opacity: 0.2;
95      font-size: 1.6rem;
96    }
97  }
98</style>