web/src/components/things/SavedServices.svelte

116 lines · 3140 bytes

  1<script lang="ts">
  2  import { onMount } from 'svelte';
  3
  4  import type { Category, Service } from '../../types/Service';
  5  import { slugify } from '@utils/fetch-data';
  6  import ServiceCard from './ServiceCard.svelte';
  7
  8  interface Props {
  9    allData: Category[];
 10    serviceList?: string[] | null;
 11  }
 12  const { allData, serviceList = null }: Props = $props();
 13
 14  interface SavedServices {
 15    category: string;
 16    section: string;
 17    service: Service;
 18  }
 19
 20  let savedServices: SavedServices[] = $state([]);
 21
 22  onMount(async () => {
 23    const results: SavedServices[] = [];
 24    const saved =
 25      serviceList || JSON.parse(localStorage.getItem('savedServices') || '[]');
 26    saved.forEach((serviceId: string) => {
 27      const parts = serviceId.split('/');
 28      const categoryName = parts[0];
 29      const sectionName = parts[1];
 30      const serviceName = parts[2];
 31
 32      const category = allData.find(
 33        (category) => slugify(category.name) === categoryName,
 34      );
 35      if (!category) return;
 36      const section = category.sections.find(
 37        (section) => slugify(section.name) === sectionName,
 38      );
 39      if (!section) return;
 40      const service = section.services.find(
 41        (service) => slugify(service.name) === serviceName,
 42      );
 43      if (!service) return;
 44      results.push({ category: category.name, section: section.name, service });
 45    });
 46    savedServices = results;
 47  });
 48</script>
 49
 50<div>
 51  {#if savedServices.length > 0}
 52    <div class="saved-services">
 53      {#each savedServices as thingy (thingy.service.name + thingy.section)}
 54        <ServiceCard
 55          categoryName={thingy.category}
 56          sectionName={thingy.section}
 57          service={thingy.service}
 58        />
 59      {/each}
 60    </div>
 61  {:else if !serviceList}
 62    <div class="nothing-yet">
 63      <p>
 64        Save software and services, to curate your own privacy-respecting
 65        software collection.
 66      </p>
 67      <small>
 68        All data is stored on-device, in your browser's local storage, and not
 69        sent anywhere unless you choose to share it
 70      </small>
 71      <p class="nope">Nothing saved yet!</p>
 72      <div class="footer">
 73        <a class="small-button" href="/all">Browse all entries &rarr;</a>
 74      </div>
 75    </div>
 76  {/if}
 77</div>
 78
 79<style lang="scss">
 80  .saved-services {
 81    display: grid;
 82    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
 83    gap: var(--space-md);
 84    margin-top: var(--space-md);
 85  }
 86
 87  .nothing-yet {
 88    background: var(--accent-fg);
 89    border: var(--border-heavy);
 90    border-radius: var(--curve-sm);
 91    box-shadow: var(--shadow-md);
 92    padding: var(--space-md);
 93    width: 100%;
 94    box-sizing: border-box;
 95    margin: 0 auto;
 96    text-align: center;
 97    p {
 98      margin: 0;
 99    }
100    small {
101      font-size: var(--text-sm);
102      opacity: var(--opacity-muted);
103    }
104    .nope {
105      font-weight: bold;
106      margin: var(--space-lg) 0 var(--space-sm);
107      font-size: var(--text-xl);
108      opacity: var(--opacity-muted);
109    }
110    .footer {
111      display: flex;
112      justify-content: flex-end;
113      margin-top: var(--space-md);
114    }
115  }
116</style>