aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Notification/store.ts
blob: 4eb163f93063999e56405df010e3e72eac3ec621 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { writable } from "svelte/store";

export interface Notification {
	id: string;
	heading: string;
	description?: string;
	duration?: number;
}

function createNotificationStore() {
	const { subscribe, update } = writable<Notification[]>([]);

	return {
		subscribe,
		add: (notification: Omit<Notification, "id">) => {
			const id = crypto.randomUUID();

			update((notifications) => [...notifications, { ...notification, id }]);

			return id;
		},
		remove: (id: string) => {
			update((notifications) => notifications.filter((n) => n.id !== id));
		},
	};
}

export const notifications = createNotificationStore();

export function addNotification(notification: Omit<Notification, "id">) {
	return notifications.add(notification);
}