mirror of
https://github.com/immich-app/immich.git
synced 2025-11-24 23:35:18 -05:00
feat(web): event handler component (#23763)
This commit is contained in:
parent
493cde9d55
commit
dd393c8346
33
web/src/lib/components/OnEvents.svelte
Normal file
33
web/src/lib/components/OnEvents.svelte
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { eventManager, type Events } from '$lib/managers/event-manager.svelte';
|
||||||
|
import { onMount } from 'svelte';
|
||||||
|
|
||||||
|
type Props = Partial<{
|
||||||
|
[K in keyof Events as `on${K}`]: (...args: Events[K]) => void;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
const props: Props = $props();
|
||||||
|
const unsubscribes: Array<() => void> = [];
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
for (const name of Object.keys(props)) {
|
||||||
|
const event = name.slice(2) as keyof Events;
|
||||||
|
const listener = props[name as keyof Props];
|
||||||
|
|
||||||
|
if (!listener) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const args = [event, listener as (...args: Events[typeof event]) => void] as const;
|
||||||
|
|
||||||
|
eventManager.on(...args);
|
||||||
|
unsubscribes.push(() => eventManager.off(...args));
|
||||||
|
}
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
for (const unsubscribe of unsubscribes) {
|
||||||
|
unsubscribe();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
</script>
|
||||||
@ -30,7 +30,7 @@ class AuthManager {
|
|||||||
globalThis.location.href = redirectUri;
|
globalThis.location.href = redirectUri;
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
eventManager.emit('auth.logout');
|
eventManager.emit('AuthLogout');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,15 @@
|
|||||||
import type { ThemeSetting } from '$lib/managers/theme-manager.svelte';
|
import type { ThemeSetting } from '$lib/managers/theme-manager.svelte';
|
||||||
import type { LoginResponseDto } from '@immich/sdk';
|
import type { LoginResponseDto } from '@immich/sdk';
|
||||||
|
|
||||||
|
export type Events = {
|
||||||
|
AppInit: [];
|
||||||
|
UserLogin: [];
|
||||||
|
AuthLogin: [LoginResponseDto];
|
||||||
|
AuthLogout: [];
|
||||||
|
LanguageChange: [{ name: string; code: string; rtl?: boolean }];
|
||||||
|
ThemeChange: [ThemeSetting];
|
||||||
|
};
|
||||||
|
|
||||||
type Listener<EventMap extends Record<string, unknown[]>, K extends keyof EventMap> = (...params: EventMap[K]) => void;
|
type Listener<EventMap extends Record<string, unknown[]>, K extends keyof EventMap> = (...params: EventMap[K]) => void;
|
||||||
|
|
||||||
class EventManager<EventMap extends Record<string, unknown[]>> {
|
class EventManager<EventMap extends Record<string, unknown[]>> {
|
||||||
@ -51,11 +60,4 @@ class EventManager<EventMap extends Record<string, unknown[]>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const eventManager = new EventManager<{
|
export const eventManager = new EventManager<Events>();
|
||||||
'app.init': [];
|
|
||||||
'user.login': [];
|
|
||||||
'auth.login': [LoginResponseDto];
|
|
||||||
'auth.logout': [];
|
|
||||||
'language.change': [{ name: string; code: string; rtl?: boolean }];
|
|
||||||
'theme.change': [ThemeSetting];
|
|
||||||
}>();
|
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import { lang } from '$lib/stores/preferences.store';
|
|||||||
|
|
||||||
class LanguageManager {
|
class LanguageManager {
|
||||||
constructor() {
|
constructor() {
|
||||||
eventManager.on('app.init', () => lang.subscribe((lang) => this.setLanguage(lang)));
|
eventManager.on('AppInit', () => lang.subscribe((lang) => this.setLanguage(lang)));
|
||||||
}
|
}
|
||||||
|
|
||||||
rtl = $state(false);
|
rtl = $state(false);
|
||||||
@ -19,7 +19,7 @@ class LanguageManager {
|
|||||||
|
|
||||||
document.body.setAttribute('dir', item.rtl ? 'rtl' : 'ltr');
|
document.body.setAttribute('dir', item.rtl ? 'rtl' : 'ltr');
|
||||||
|
|
||||||
eventManager.emit('language.change', item);
|
eventManager.emit('LanguageChange', item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -36,7 +36,7 @@ class ThemeManager {
|
|||||||
isDark = $derived(this.value === Theme.DARK);
|
isDark = $derived(this.value === Theme.DARK);
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
eventManager.on('app.init', () => this.#onAppInit());
|
eventManager.on('AppInit', () => this.#onAppInit());
|
||||||
}
|
}
|
||||||
|
|
||||||
setSystem(system: boolean) {
|
setSystem(system: boolean) {
|
||||||
@ -71,7 +71,7 @@ class ThemeManager {
|
|||||||
|
|
||||||
this.#theme.current = theme;
|
this.#theme.current = theme;
|
||||||
|
|
||||||
eventManager.emit('theme.change', theme);
|
eventManager.emit('ThemeChange', theme);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@ class UploadManager {
|
|||||||
mediaTypes = $state<ServerMediaTypesResponseDto>({ image: [], sidecar: [], video: [] });
|
mediaTypes = $state<ServerMediaTypesResponseDto>({ image: [], sidecar: [], video: [] });
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
eventManager.on('app.init', () => void this.#loadExtensions()).on('auth.logout', () => void this.reset());
|
eventManager.on('AppInit', () => void this.#loadExtensions()).on('AuthLogout', () => void this.reset());
|
||||||
}
|
}
|
||||||
|
|
||||||
reset() {
|
reset() {
|
||||||
|
|||||||
@ -19,7 +19,7 @@ class FoldersStore {
|
|||||||
private assets = $state<AssetCache>({});
|
private assets = $state<AssetCache>({});
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
eventManager.on('auth.logout', () => this.clearCache());
|
eventManager.on('AuthLogout', () => this.clearCache());
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetchTree(): Promise<TreeNode> {
|
async fetchTree(): Promise<TreeNode> {
|
||||||
|
|||||||
@ -21,7 +21,7 @@ export type MemoryAsset = MemoryIndex & {
|
|||||||
|
|
||||||
class MemoryStoreSvelte {
|
class MemoryStoreSvelte {
|
||||||
constructor() {
|
constructor() {
|
||||||
eventManager.on('auth.logout', () => this.clearCache());
|
eventManager.on('AuthLogout', () => this.clearCache());
|
||||||
}
|
}
|
||||||
|
|
||||||
memories = $state<MemoryResponseDto[]>([]);
|
memories = $state<MemoryResponseDto[]>([]);
|
||||||
|
|||||||
@ -9,8 +9,8 @@ class NotificationStore {
|
|||||||
notifications = $state<NotificationDto[]>([]);
|
notifications = $state<NotificationDto[]>([]);
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
eventManager.on('auth.login', () => handlePromiseError(this.refresh()));
|
eventManager.on('AuthLogin', () => handlePromiseError(this.refresh()));
|
||||||
eventManager.on('auth.logout', () => this.clear());
|
eventManager.on('AuthLogout', () => this.clear());
|
||||||
}
|
}
|
||||||
|
|
||||||
async refresh() {
|
async refresh() {
|
||||||
|
|||||||
@ -5,7 +5,7 @@ class SearchStore {
|
|||||||
isSearchEnabled = $state(false);
|
isSearchEnabled = $state(false);
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
eventManager.on('auth.logout', () => this.clearCache());
|
eventManager.on('AuthLogout', () => this.clearCache());
|
||||||
}
|
}
|
||||||
|
|
||||||
clearCache() {
|
clearCache() {
|
||||||
|
|||||||
@ -16,4 +16,4 @@ export const resetSavedUser = () => {
|
|||||||
purchaseStore.setPurchaseStatus(false);
|
purchaseStore.setPurchaseStatus(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
eventManager.on('auth.logout', () => resetSavedUser());
|
eventManager.on('AuthLogout', () => resetSavedUser());
|
||||||
|
|||||||
@ -26,4 +26,4 @@ const reset = () => {
|
|||||||
Object.assign(userInteraction, defaultUserInteraction);
|
Object.assign(userInteraction, defaultUserInteraction);
|
||||||
};
|
};
|
||||||
|
|
||||||
eventManager.on('auth.logout', () => reset());
|
eventManager.on('AuthLogout', () => reset());
|
||||||
|
|||||||
@ -58,7 +58,7 @@
|
|||||||
// if the browser theme changes, changes the Immich theme too
|
// if the browser theme changes, changes the Immich theme too
|
||||||
});
|
});
|
||||||
|
|
||||||
eventManager.emit('app.init');
|
eventManager.emit('AppInit');
|
||||||
|
|
||||||
beforeNavigate(({ from, to }) => {
|
beforeNavigate(({ from, to }) => {
|
||||||
if (isAssetViewerRoute(from) && isAssetViewerRoute(to)) {
|
if (isAssetViewerRoute(from) && isAssetViewerRoute(to)) {
|
||||||
|
|||||||
@ -27,7 +27,7 @@
|
|||||||
|
|
||||||
const onSuccess = async (user: LoginResponseDto) => {
|
const onSuccess = async (user: LoginResponseDto) => {
|
||||||
await goto(data.continueUrl, { invalidateAll: true });
|
await goto(data.continueUrl, { invalidateAll: true });
|
||||||
eventManager.emit('auth.login', user);
|
eventManager.emit('AuthLogin', user);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onFirstLogin = () => goto(AppRoute.AUTH_CHANGE_PASSWORD);
|
const onFirstLogin = () => goto(AppRoute.AUTH_CHANGE_PASSWORD);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user