Files
immich/web/src/lib/stores/notification-manager.svelte.ts
Jason Rasmussen 1b5fc9c665 feat: notifications (#17701)
* feat: notifications

* UI works

* chore: pr feedback

* initial fetch and clear notification upon logging out

* fix: merge

---------

Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2025-04-28 10:36:14 -04:00

39 lines
1.1 KiB
TypeScript

import { eventManager } from '$lib/stores/event-manager.svelte';
import { getNotifications, updateNotification, updateNotifications, type NotificationDto } from '@immich/sdk';
class NotificationStore {
notifications = $state<NotificationDto[]>([]);
constructor() {
// TODO replace this with an `auth.login` event
this.refresh().catch(() => {});
eventManager.on('auth.logout', () => this.clear());
}
get hasUnread() {
return this.notifications.length > 0;
}
refresh = async () => {
this.notifications = await getNotifications({ unread: true });
};
markAsRead = async (id: string) => {
this.notifications = this.notifications.filter((notification) => notification.id !== id);
await updateNotification({ id, notificationUpdateDto: { readAt: new Date().toISOString() } });
};
markAllAsRead = async () => {
const ids = this.notifications.map(({ id }) => id);
this.notifications = [];
await updateNotifications({ notificationUpdateAllDto: { ids, readAt: new Date().toISOString() } });
};
clear = () => {
this.notifications = [];
};
}
export const notificationManager = new NotificationStore();