2022-08-25 23:04:23 -07:00
|
|
|
import { writable } from 'svelte/store';
|
|
|
|
|
|
|
|
|
|
export enum NotificationType {
|
2023-07-01 00:50:47 -04:00
|
|
|
Info = 'Info',
|
|
|
|
|
Error = 'Error',
|
2023-08-09 18:11:26 -07:00
|
|
|
Warning = 'Warning',
|
2022-08-25 23:04:23 -07:00
|
|
|
}
|
|
|
|
|
|
2024-03-21 14:44:54 +01:00
|
|
|
export type Notification = {
|
|
|
|
|
id: number;
|
|
|
|
|
type: NotificationType;
|
|
|
|
|
message: string;
|
|
|
|
|
/** The action to take when the notification is clicked */
|
|
|
|
|
action: NotificationAction;
|
|
|
|
|
/** Timeout in miliseconds */
|
|
|
|
|
timeout: number;
|
|
|
|
|
};
|
2022-08-25 23:04:23 -07:00
|
|
|
|
2022-11-17 06:11:15 +01:00
|
|
|
type DiscardAction = { type: 'discard' };
|
|
|
|
|
type NoopAction = { type: 'noop' };
|
|
|
|
|
type LinkAction = { type: 'link'; target: string };
|
|
|
|
|
export type NotificationAction = DiscardAction | NoopAction | LinkAction;
|
|
|
|
|
|
2024-03-21 14:44:54 +01:00
|
|
|
export type NotificationOptions = Partial<Exclude<Notification, 'id'>> & { message: string };
|
2022-11-17 06:11:15 +01:00
|
|
|
|
2022-08-25 23:04:23 -07:00
|
|
|
function createNotificationList() {
|
2024-03-21 14:44:54 +01:00
|
|
|
const notificationList = writable<Notification[]>([]);
|
|
|
|
|
let count = 1;
|
|
|
|
|
|
|
|
|
|
const show = (options: NotificationOptions) => {
|
|
|
|
|
notificationList.update((currentList) => {
|
|
|
|
|
currentList.push({
|
|
|
|
|
id: count++,
|
|
|
|
|
type: NotificationType.Info,
|
|
|
|
|
action: { type: 'discard' },
|
|
|
|
|
timeout: 3000,
|
|
|
|
|
...options,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return currentList;
|
|
|
|
|
});
|
2023-07-01 00:50:47 -04:00
|
|
|
};
|
2022-08-25 23:04:23 -07:00
|
|
|
|
2023-07-01 00:50:47 -04:00
|
|
|
const removeNotificationById = (id: number) => {
|
2024-03-21 14:44:54 +01:00
|
|
|
notificationList.update((currentList) => currentList.filter((n) => n.id !== id));
|
2023-07-01 00:50:47 -04:00
|
|
|
};
|
2022-08-25 23:04:23 -07:00
|
|
|
|
2023-07-01 00:50:47 -04:00
|
|
|
return {
|
|
|
|
|
show,
|
|
|
|
|
removeNotificationById,
|
|
|
|
|
notificationList,
|
|
|
|
|
};
|
2022-08-25 23:04:23 -07:00
|
|
|
}
|
|
|
|
|
|
2022-08-26 09:36:54 -07:00
|
|
|
export const notificationController = createNotificationList();
|