Skip to content

Commit 1b2a0f4

Browse files
Demo add custom links
1 parent 1f90845 commit 1b2a0f4

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

src/routes/+page.svelte

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
isMachineTimeSameAsPreferred
1414
} from "@davidnet/svelte-ui";
1515
import { onMount, onDestroy } from "svelte";
16+
import { writable } from "svelte/store";
1617
1718
let correlationID = crypto.randomUUID();
1819
let sessionInfo: SessionInfo | null = $state(null);
@@ -112,6 +113,53 @@
112113
onDestroy(() => {
113114
window.removeEventListener("resize", handleResize);
114115
});
116+
117+
export type CustomLink = {
118+
id: string;
119+
name: string;
120+
url: string;
121+
};
122+
123+
const storedLinks = localStorage.getItem("customLinks");
124+
const initialLinks: CustomLink[] = storedLinks ? JSON.parse(storedLinks) : [];
125+
126+
export const customLinks = writable<CustomLink[]>(initialLinks);
127+
128+
customLinks.subscribe((links) => {
129+
localStorage.setItem("customLinks", JSON.stringify(links));
130+
});
131+
132+
// Functions to manage links
133+
export function create_custom_link(id: string, name: string, url: string) {
134+
customLinks.update((links) => [...links, { id, name, url }]);
135+
}
136+
137+
export function edit_custom_link(id: string, name: string, url: string) {
138+
customLinks.update((links) =>
139+
links.map((link) => (link.id === id ? { ...link, name, url } : link))
140+
);
141+
}
142+
143+
export function remove_custom_link(id: string) {
144+
customLinks.update((links) => links.filter((link) => link.id !== id));
145+
}
146+
147+
function addLinkPrompt() {
148+
const name = prompt("Enter link name:");
149+
if (!name) return;
150+
const url = prompt("Enter link URL:");
151+
if (!url) return;
152+
const id = crypto.randomUUID();
153+
create_custom_link(id, name, url);
154+
}
155+
156+
function editLinkPrompt(link: { id: string; name: string; url: string }) {
157+
const name = prompt("Edit link name:", link.name);
158+
if (!name) return;
159+
const url = prompt("Edit link URL:", link.url);
160+
if (!url) return;
161+
edit_custom_link(link.id, name, url);
162+
}
115163
</script>
116164

117165
<FlexWrapper direction="column" width="100%">
@@ -363,6 +411,34 @@
363411
<span style="color: var(--token-color-text-secondary); margin-left: var(--token-space-3);">No recent activity.</span>
364412
</FlexWrapper>
365413

414+
<Space height="var(--token-space-4)" />
415+
416+
<FlexWrapper alignitems="flex-start" width="80%">
417+
<h2>Your links:</h2>
418+
<FlexWrapper gap="var(--token-space-3)" justifycontent={width > 600 ? "flex-start" : "space-evenly"} direction="row" wrap="wrap">
419+
{#each $customLinks as link (link.id)}
420+
<div class="option">
421+
<FlexWrapper width="100%" height="100%" gap="var(--token-space-2)" direction="column" alignitems="center">
422+
<a href={link.url} target="_blank">
423+
<p class="option-text">{link.name}</p>
424+
</a>
425+
<FlexWrapper gap="0.25rem">
426+
<button onclick={() => editLinkPrompt(link)}>✏️</button>
427+
<button onclick={() => remove_custom_link(link.id)}>🗑️</button>
428+
</FlexWrapper>
429+
</FlexWrapper>
430+
</div>
431+
{/each}
432+
<!-- Button to add a new link -->
433+
<div class="option" on:click={addLinkPrompt}>
434+
<FlexWrapper width="100%" height="100%" justifycontent="center" alignitems="center">
435+
<p class="option-text">➕ Add</p>
436+
</FlexWrapper>
437+
</div>
438+
</FlexWrapper>
439+
</FlexWrapper>
440+
441+
366442
<style>
367443
.welcomebox {
368444
height: 4rem;

0 commit comments

Comments
 (0)