master
ts 126 lines 3.27 KB
Raw
1 import { writable } from "svelte/store";
2
3 export enum NotifType {
4 Message,
5 Warning,
6 Error
7 }
8 class Notification {
9 id;
10 type: string;
11 title: string;
12 message: string;
13 read: boolean;
14 actions;
15 constructor(title, type, read = false, message = "", actions = []) {
16 this.title = title;
17 this.message = message;
18 this.type = getType(type);
19 this.read = read;
20 this.actions = actions;
21 }
22 }
23 class ToastNotification extends Notification {
24 constructor(title, type, message = "", actions = []) {
25 super(title, type, false, message, actions);
26 }
27 }
28 export class Action {
29 label: string;
30 action: () => void | null;
31 constructor(label, action: () => void | null = null) {
32 this.label = label;
33 this.action = action;
34 }
35 }
36
37 const actions = [
38 new Action(" Test Message", () => {
39 addNotification(NotifType.Message, "Message notification success");
40 }),
41 new Action("Test Warning", () => {
42 addNotification(NotifType.Warning, "Warning notification success");
43 }),
44 new Action("Test Error", () => {
45 addNotification(NotifType.Error, "Failure or something idk");
46 })
47 ]
48
49 let id = 0;
50 export const unreadnotifications = writable(false);
51 export const notifications = writable([]);
52 export const toasts = writable([]);
53 let notificationlist: Notification[] = [];
54 let toastlist: ToastNotification[] = [];
55
56 export function addNotification(type: NotifType, title: string, actions: Action[] = [], message = "") {
57 let notification = new Notification(title, type, false, message, actions);
58 let toast = new ToastNotification(title, type, message, actions);
59 notification.id = id;
60 toast.id = id;
61 notificationlist = [...notificationlist, notification];
62 toastlist = [...toastlist, toast];
63 notifications.set(notificationlist);
64 toasts.set(toastlist);
65 updateNotificationStatus();
66 id++;
67 }
68
69 export function markAllRead() {
70 for (const notification of notificationlist) {
71 updateReadStatus(notification.id, true);
72 }
73 }
74
75 export function updateReadStatus(id, read) {
76 for (const notification of notificationlist) {
77 if (notification.id === id) {
78 notification.read = read;
79 break;
80 }
81 }
82 updateNotificationStatus();
83 }
84
85 export function clearNotifications() {
86 notificationlist = [];
87 toastlist = [];
88 notifications.set(notificationlist);
89 toasts.set(toastlist);
90 updateNotificationStatus();
91 }
92
93 export function closeNotification(id) {
94 notificationlist = notificationlist.filter(n => n.id !== id);
95 notifications.set(notificationlist);
96 updateNotificationStatus();
97 }
98
99 export function closeToast(id) {
100 toastlist = toastlist.filter(n => n.id !== id);
101 toasts.set(toastlist);
102 }
103
104 function getType(type: NotifType) {
105 switch (type) {
106 case NotifType.Message:
107 return "Message";
108 case NotifType.Warning:
109 return "Warning";
110 case NotifType.Error:
111 return "Error";
112 default:
113 return "Message";
114 }
115 }
116
117 function updateNotificationStatus() {
118 let unread = false;
119 for (const notification of notificationlist) {
120 if (!notification.read) {
121 unread = true;
122 break;
123 }
124 }
125 unreadnotifications.set(unread);
126 }