Skip to content

Commit cd5741b

Browse files
committed
chore: proxy globally usable
1 parent e73497d commit cd5741b

File tree

6 files changed

+101
-0
lines changed

6 files changed

+101
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<script lang="ts">
2+
import {page} from '$app/stores';
3+
import {parse} from 'cookie';
4+
import {afterNavigate} from '$app/navigation';
5+
import type {FlashMessage, Message, MessageFlux,} from "$lib/types.js";
6+
import {add_toast, dismiss_toast_after} from "$lib/notifier.svelte.js";
7+
import {untrack} from "svelte";
8+
9+
function flux_message_to_proper(msg_source: MessageFlux) {
10+
if (!msg_source || !msg_source.message) return;
11+
return {
12+
message_type: msg_source?.message_type || 'warning',
13+
alias: msg_source?.alias || 'system',
14+
message: msg_source.message,
15+
action: msg_source.action,
16+
} as Message;
17+
}
18+
19+
function trigger_message(msg_source: MessageFlux) {
20+
const proper_msg = flux_message_to_proper(msg_source);
21+
if (proper_msg) untrack(() => dismiss_toast_after(add_toast(proper_msg)));
22+
}
23+
24+
$effect(() => {
25+
trigger_message($page.form);
26+
trigger_message($page.data);
27+
trigger_message($page.error);
28+
});
29+
30+
function assign_flash_message() {
31+
try {
32+
const raw_flash_message = parse(document.cookie)[`flash_message`];
33+
if (raw_flash_message) {
34+
const flash_message = JSON.parse(atob(raw_flash_message) || '{}') as FlashMessage;
35+
if (flash_message.path !== $page.url.pathname) return;
36+
trigger_message(flash_message);
37+
document.cookie = `flash_message=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
38+
}
39+
} catch (e) {
40+
console.error(e);
41+
}
42+
}
43+
44+
afterNavigate(() => {
45+
assign_flash_message();
46+
});
47+
</script>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script lang="ts">
2+
const {...restParams}: Record<string, string> = $props();
3+
</script>
4+
5+
{#each Object.entries(restParams) as [key, value]}
6+
<input name="{key}" type="hidden" value="{value}"/>
7+
{/each}
File renamed without changes.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type {BaseMessage, Toast} from "$lib/types.js";
2+
3+
4+
const AUTO_DISMISS_DURATION = 7777;
5+
6+
/*
7+
Notifier is a global store that manages toast notifications
8+
- toasts: a list of toast notifications
9+
- error_: a flag to indicate if any error has occurred; implementation is up to the developer
10+
*/
11+
export let notifier = $state({
12+
toasts: [] as Toast[],
13+
error_: false,
14+
})
15+
16+
/*
17+
Add a toast to the notifier
18+
*/
19+
export function add_toast(message: BaseMessage, auto_dismiss_duration = AUTO_DISMISS_DURATION) {
20+
const toast: Toast = {...message, auto_dismiss_duration, id: crypto.randomUUID(),};
21+
notifier.toasts.push(toast);
22+
return toast;
23+
}
24+
25+
/*
26+
Dismiss a toast, given its id
27+
*/
28+
export function dismiss_toast(toastId: string) {
29+
const index = notifier.toasts.findIndex((toast) => toast && toast.id === toastId);
30+
notifier.toasts.splice(index, 1);
31+
}
32+
33+
/*
34+
Dismiss a toast after a given duration
35+
*/
36+
export function dismiss_toast_after(toast: Toast) {
37+
setTimeout(() => {
38+
dismiss_toast(toast.id);
39+
}, toast.auto_dismiss_duration);
40+
}
41+
42+
/*
43+
Dismiss all toasts
44+
*/
45+
export function dismiss_all_toasts() {
46+
notifier.toasts.splice(0, notifier.toasts.length);
47+
}

0 commit comments

Comments
 (0)