mirror of
https://github.com/immich-app/immich.git
synced 2025-11-30 21:29:44 +09:00
refactor(web): cleanup notification components (#8150)
* refactor(web): cleanup notification components * use counter for ID
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user