mirror of
https://github.com/immich-app/immich.git
synced 2026-02-04 08:49:01 +03:00
* 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>
39 lines
1.1 KiB
TypeScript
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();
|