web/src/components/things/GetSharableLink.svelte
84 lines · 2145 bytes
1<script lang="ts">
2 import { slugify } from '@utils/fetch-data';
3
4 let linkId = '';
5 let done = false;
6 let error = 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((error) => {
33 error = true;
34 console.error('Error:', error);
35 });
36 };
37</script>
38
39<div class="share-container">
40 {#if !done}
41 <button class="save-button" on:click={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: 1px solid var(--box-outline);
65 box-shadow: 3px 3px 0 var(--box-outline);
66 padding: 0.25rem 0.5rem;
67 border-radius: var(--curve-sm);
68 color: var(--foreground);
69 font-family: Lekton;
70 font-size: 1.2rem;
71 cursor: pointer;
72 }
73 .success-msg {
74 font-size: 1rem;
75 color: var(--success);
76 a {
77 color: var(--success);
78 }
79 }
80 .error-msg {
81 font-size: 1rem;
82 color: var(--danger);
83 }
84</style>