refactor: auth login event (#17934)

This commit is contained in:
Jason Rasmussen
2025-04-28 14:13:14 -04:00
committed by GitHub
parent 64e738f79d
commit f64e6f5dc3
4 changed files with 24 additions and 16 deletions

View File

@@ -1,3 +1,5 @@
import type { LoginResponseDto } from '@immich/sdk';
type Listener<EventMap extends Record<string, unknown[]>, K extends keyof EventMap> = (...params: EventMap[K]) => void;
class EventManager<EventMap extends Record<string, unknown[]>> {
@@ -50,6 +52,7 @@ class EventManager<EventMap extends Record<string, unknown[]>> {
export const eventManager = new EventManager<{
'user.login': [];
'auth.login': [LoginResponseDto];
'auth.logout': [];
'language.change': [{ name: string; code: string; rtl?: boolean }];
}>();

View File

@@ -1,24 +1,27 @@
import { eventManager } from '$lib/managers/event-manager.svelte';
import { handlePromiseError } from '$lib/utils';
import { handleError } from '$lib/utils/handle-error';
import { getNotifications, updateNotification, updateNotifications, type NotificationDto } from '@immich/sdk';
import { t } from 'svelte-i18n';
import { get } from 'svelte/store';
class NotificationStore {
notifications = $state<NotificationDto[]>([]);
constructor() {
// TODO replace this with an `auth.login` event
this.refresh().catch(() => {});
eventManager.on('auth.login', () => handlePromiseError(this.refresh()));
eventManager.on('auth.logout', () => this.clear());
}
get hasUnread() {
return this.notifications.length > 0;
async refresh() {
try {
this.notifications = await getNotifications({ unread: true });
} catch (error) {
const translate = get(t);
handleError(error, translate('errors.failed_to_load_notifications'));
}
}
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() } });