2022-08-25 23:04:23 -07:00
|
|
|
import { writable } from 'svelte/store';
|
|
|
|
|
|
|
|
|
|
export enum NotificationType {
|
|
|
|
|
Info = 'Info',
|
|
|
|
|
Error = 'Error'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ImmichNotification {
|
|
|
|
|
id = new Date().getTime();
|
|
|
|
|
type!: NotificationType;
|
|
|
|
|
message!: string;
|
2022-11-17 06:11:15 +01:00
|
|
|
action!: NotificationAction;
|
2022-08-26 10:01:47 -07:00
|
|
|
timeout = 3000;
|
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;
|
|
|
|
|
|
2022-08-26 10:01:47 -07:00
|
|
|
export class ImmichNotificationDto {
|
|
|
|
|
/**
|
|
|
|
|
* Notification type
|
|
|
|
|
* @type {NotificationType} [Info, Error]
|
|
|
|
|
*/
|
|
|
|
|
type: NotificationType = NotificationType.Info;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Notification message
|
|
|
|
|
*/
|
|
|
|
|
message = '';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Timeout in miliseconds
|
|
|
|
|
*/
|
2022-08-26 10:36:41 -07:00
|
|
|
timeout?: number;
|
2022-11-17 06:11:15 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The action to take when the notification is clicked
|
|
|
|
|
*/
|
|
|
|
|
action?: NotificationAction;
|
2022-08-26 10:01:47 -07:00
|
|
|
}
|
2022-11-17 06:11:15 +01:00
|
|
|
|
2022-08-25 23:04:23 -07:00
|
|
|
function createNotificationList() {
|
2022-08-26 09:36:54 -07:00
|
|
|
const notificationList = writable<ImmichNotification[]>([]);
|
2022-08-25 23:04:23 -07:00
|
|
|
|
2022-08-26 10:01:47 -07:00
|
|
|
const show = (notificationInfo: ImmichNotificationDto) => {
|
|
|
|
|
const newNotification = new ImmichNotification();
|
|
|
|
|
newNotification.message = notificationInfo.message;
|
|
|
|
|
newNotification.type = notificationInfo.type;
|
2022-08-26 10:36:41 -07:00
|
|
|
newNotification.timeout = notificationInfo.timeout || 3000;
|
2022-11-17 06:11:15 +01:00
|
|
|
newNotification.action = notificationInfo.action || { type: 'discard' };
|
2022-08-25 23:04:23 -07:00
|
|
|
|
2022-08-26 10:01:47 -07:00
|
|
|
notificationList.update((currentList) => [...currentList, newNotification]);
|
2022-08-25 23:04:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const removeNotificationById = (id: number) => {
|
2022-08-26 09:36:54 -07:00
|
|
|
notificationList.update((currentList) => currentList.filter((n) => n.id != id));
|
2022-08-25 23:04:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
show,
|
|
|
|
|
removeNotificationById,
|
2022-08-26 09:36:54 -07:00
|
|
|
notificationList
|
2022-08-25 23:04:23 -07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 09:36:54 -07:00
|
|
|
export const notificationController = createNotificationList();
|