web/src/components/things/GetSharableLink.svelte

82 lines · 2031 bytes

 1
 2<script lang="ts">
 3
 4import { slugify } from "@utils/fetch-data";
 5
 6let linkId = '';
 7let done = false;
 8let error = false;
 9
10const save = async () => {
11  const savedServices =  JSON.parse(localStorage.getItem('savedServices') || '[]');
12  const inventoryTitle = localStorage.getItem('userTitle') || 'Anon\'s Inventory';
13  const uniqueId = Math.random().toString(36).substring(2);
14  const saveKey = `${uniqueId}_${slugify(inventoryTitle)}`;
15  const url = 'https://awesome-privacy-share-api.as93.net';
16  const data = { key: saveKey, services: savedServices };
17  fetch(url, {
18      method: 'PUT',
19      headers: { 'Content-Type': 'application/json' },
20      body: JSON.stringify(data),
21  })
22  .then(response => response.json())
23  .then(data => {
24    linkId = data.key;
25    done = true;
26    error = false;
27    navigator.clipboard.writeText(`https://awesome-privacy.xyz/inventory/${linkId}`);
28  })
29  .catch(error => {
30    error = true;
31    console.error('Error:', error)
32  });
33};
34
35</script>
36
37<div class="share-container">
38  {#if !done}
39    <button class="save-button" on:click={save}>Get Sharable Link</button>
40  {/if}
41  {#if done}
42    <span class="success-msg">
43      Done! Your share link has been copied to clipboard.
44      <a href={`https://awesome-privacy.xyz/inventory/${linkId}`}>
45        Visit Link
46      </a>
47    </span>
48  {/if}
49  {#if error}
50    <span class="error-msg">Something unexpected happened</span>
51  {/if}
52</div>
53
54<style lang="scss">
55  .share-container {
56    display: flex;
57    flex-direction: column;
58    max-width: 300px;
59  }
60  .save-button {
61    background: var(--accent-3);
62    border: 1px solid var(--box-outline);
63    box-shadow: 3px 3px 0 var(--box-outline);
64    padding: 0.25rem 0.5rem;
65    border-radius: var(--curve-sm);
66    color: var(--foreground);
67    font-family: Lekton;
68    font-size: 1.2rem;
69    cursor: pointer;
70  }
71  .success-msg {
72    font-size: 1rem;
73    color: var(--success);
74    a {
75      color: var(--success);
76    }
77  }
78  .error-msg {
79    font-size: 1rem;
80    color: var(--danger);
81  }
82</style>