web/src/components/things/SavedServices.svelte

118 lines · 3189 bytes

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