refactor(web): cleanup notification components (#8150)

* refactor(web): cleanup notification components

* use counter for ID
This commit is contained in:
Michel Heusschen
2024-03-21 14:44:54 +01:00
committed by GitHub
parent 382b63954c
commit 793049388b
5 changed files with 64 additions and 109 deletions

View File

@@ -6,57 +6,43 @@ export enum NotificationType {
Warning = 'Warning',
}
export class ImmichNotification {
id = Date.now() + Math.random();
type!: NotificationType;
message!: string;
action!: NotificationAction;
timeout = 3000;
}
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;
};
type DiscardAction = { type: 'discard' };
type NoopAction = { type: 'noop' };
type LinkAction = { type: 'link'; target: string };
export type NotificationAction = DiscardAction | NoopAction | LinkAction;
export class ImmichNotificationDto {
/**
* Notification type
* @type {NotificationType} [Info, Error]
*/
type: NotificationType = NotificationType.Info;
/**
* Notification message
*/
message = '';
/**
* Timeout in miliseconds
*/
timeout?: number;
/**
* The action to take when the notification is clicked
*/
action?: NotificationAction;
}
export type NotificationOptions = Partial<Exclude<Notification, 'id'>> & { message: string };
function createNotificationList() {
const notificationList = writable<ImmichNotification[]>([]);
const notificationList = writable<Notification[]>([]);
let count = 1;
const show = (notificationInfo: ImmichNotificationDto) => {
const newNotification = new ImmichNotification();
newNotification.message = notificationInfo.message;
newNotification.type = notificationInfo.type;
newNotification.timeout = notificationInfo.timeout || 3000;
newNotification.action = notificationInfo.action || { type: 'discard' };
const show = (options: NotificationOptions) => {
notificationList.update((currentList) => {
currentList.push({
id: count++,
type: NotificationType.Info,
action: { type: 'discard' },
timeout: 3000,
...options,
});
notificationList.update((currentList) => [...currentList, newNotification]);
return currentList;
});
};
const removeNotificationById = (id: number) => {
notificationList.update((currentList) => currentList.filter((n) => n.id != id));
notificationList.update((currentList) => currentList.filter((n) => n.id !== id));
};
return {