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