chore: componetize toasts

makes creating a toast return an id, stop settings saving every time you visit page
This commit is contained in:
JovannMC 2025-02-16 19:47:04 +03:00
parent 3a839124d2
commit 775eebbc28
No known key found for this signature in database
6 changed files with 91 additions and 85 deletions

View File

@ -0,0 +1,15 @@
<script lang="ts">
import Toast from "$lib/components/visual/Toast.svelte";
import { type Toast as ToastType, toasts } from "$lib/store/ToastProvider";
let toastList = $state<ToastType[]>([]);
toasts.subscribe((value) => {
toastList = value as ToastType[];
});
</script>
<div class="fixed bottom-28 md:bottom-0 right-0 p-4 space-y-4 z-50">
{#each toastList as { id, type, message, durations }}
<Toast {id} {type} {message} {durations} />
{/each}
</div>

View File

@ -1,5 +1,6 @@
export { default as UploadRegion } from './UploadRegion.svelte'; export { default as UploadRegion } from './UploadRegion.svelte';
export { default as Gradients } from './Gradients.svelte'; export { default as Gradients } from './Gradients.svelte';
export { default as Toasts } from './Toasts.svelte';
export { default as Dialogs } from './Dialogs.svelte'; export { default as Dialogs } from './Dialogs.svelte';
export { default as PageContent } from './PageContent.svelte'; export { default as PageContent } from './PageContent.svelte';
export { default as MobileLogo } from './MobileLogo.svelte'; export { default as MobileLogo } from './MobileLogo.svelte';

View File

@ -47,6 +47,8 @@ function addToast(
}, },
durations.enter + durations.stay + durations.exit, durations.enter + durations.stay + durations.exit,
); );
return id;
} }
function removeToast(id: number) { function removeToast(id: number) {

View File

@ -4,11 +4,9 @@
import { PUB_HOSTNAME, PUB_PLAUSIBLE_URL } from "$env/static/public"; import { PUB_HOSTNAME, PUB_PLAUSIBLE_URL } from "$env/static/public";
import { VERT_NAME } from "$lib/consts"; import { VERT_NAME } from "$lib/consts";
import Toast from "$lib/components/visual/Toast.svelte";
import * as Layout from "$lib/components/layout"; import * as Layout from "$lib/components/layout";
import * as Navbar from "$lib/components/layout/Navbar"; import * as Navbar from "$lib/components/layout/Navbar";
import featuredImage from "$lib/assets/VERT_Feature.webp"; import featuredImage from "$lib/assets/VERT_Feature.webp";
import { type Toast as ToastType, toasts } from "$lib/store/ToastProvider";
import { Settings } from "$lib/sections/settings/index.svelte"; import { Settings } from "$lib/sections/settings/index.svelte";
import { import {
files, files,
@ -21,12 +19,6 @@
let { children } = $props(); let { children } = $props();
let toastList = $state<ToastType[]>([]);
toasts.subscribe((value) => {
toastList = value as ToastType[];
});
const dropFiles = (e: DragEvent) => { const dropFiles = (e: DragEvent) => {
e.preventDefault(); e.preventDefault();
dropping.set(false); dropping.set(false);
@ -116,12 +108,7 @@
--> -->
<Layout.PageContent {children} /> <Layout.PageContent {children} />
<div class="fixed bottom-28 md:bottom-0 right-0 p-4 space-y-4 z-50"> <Layout.Toasts />
{#each toastList as { id, type, message, durations }}
<Toast {id} {type} {message} {durations} />
{/each}
</div>
<Layout.Dialogs /> <Layout.Dialogs />
<div> <div>

View File

@ -16,17 +16,18 @@
isInitial = false; isInitial = false;
return; return;
} }
settings;
const savedSettings = localStorage.getItem("settings"); const savedSettings = localStorage.getItem("settings");
if (savedSettings) { if (savedSettings) {
const parsedSettings = JSON.parse(savedSettings); const parsedSettings = JSON.parse(savedSettings);
if (parsedSettings === settings) return; if (JSON.stringify(parsedSettings) === JSON.stringify(settings))
return;
} }
log(["settings"], "saving settings");
try { try {
Settings.Settings.instance.settings = settings; Settings.Settings.instance.settings = settings;
Settings.Settings.instance.save(); Settings.Settings.instance.save();
log(["settings"], "saving settings");
} catch (error) { } catch (error) {
log(["settings", "error"], `failed to save settings: ${error}`); log(["settings", "error"], `failed to save settings: ${error}`);
addToast("error", "Failed to save settings!"); addToast("error", "Failed to save settings!");