mirror of
https://github.com/immich-app/immich.git
synced 2026-05-27 01:52:33 -04:00
3d075f2bf8
feat: plugins chore: better types feat: plugins
85 lines
1.9 KiB
TypeScript
85 lines
1.9 KiB
TypeScript
import {
|
|
getWorkflowTriggers,
|
|
searchPluginMethods,
|
|
WorkflowTrigger,
|
|
type PluginMethodResponseDto,
|
|
type WorkflowTriggerResponseDto,
|
|
} from '@immich/sdk';
|
|
import { t } from 'svelte-i18n';
|
|
import { SvelteMap } from 'svelte/reactivity';
|
|
import { get } from 'svelte/store';
|
|
import { authManager } from '$lib/managers/auth-manager.svelte';
|
|
import { eventManager } from '$lib/managers/event-manager.svelte';
|
|
|
|
class PluginManager {
|
|
#loading: Promise<void> | undefined;
|
|
#methodMap = new SvelteMap<string, PluginMethodResponseDto>();
|
|
#methods = $state<PluginMethodResponseDto[]>([]);
|
|
#triggers = $state<WorkflowTriggerResponseDto[]>([]);
|
|
|
|
constructor() {
|
|
eventManager.on({
|
|
AuthLogout: () => this.clearCache(),
|
|
AuthUserLoaded: () => this.initialize(),
|
|
});
|
|
|
|
// loaded event might have already happened
|
|
if (authManager.authenticated) {
|
|
void this.initialize();
|
|
}
|
|
}
|
|
|
|
get triggers() {
|
|
return this.#triggers;
|
|
}
|
|
|
|
ready() {
|
|
return this.initialize();
|
|
}
|
|
|
|
getMethod(key: string) {
|
|
return this.#methodMap.get(key);
|
|
}
|
|
|
|
getMethodLabel(key: string) {
|
|
const method = this.getMethod(key);
|
|
return method?.title ?? get(t)('unknown');
|
|
}
|
|
|
|
getTrigger(trigger: WorkflowTrigger) {
|
|
const result = this.#triggers.find((t) => t.trigger === trigger);
|
|
|
|
if (!result) {
|
|
throw new Error(`Unknown trigger type: ${trigger}`);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private clearCache() {
|
|
this.#loading = undefined;
|
|
this.#methodMap = new SvelteMap();
|
|
}
|
|
|
|
private initialize() {
|
|
if (!this.#loading) {
|
|
this.#loading = this.load();
|
|
}
|
|
|
|
return this.#loading;
|
|
}
|
|
|
|
private async load() {
|
|
const [methods, triggers] = await Promise.all([searchPluginMethods({}), getWorkflowTriggers()]);
|
|
|
|
this.#methods = methods;
|
|
for (const method of this.#methods) {
|
|
this.#methodMap.set(method.key, method);
|
|
}
|
|
|
|
this.#triggers = triggers;
|
|
}
|
|
}
|
|
|
|
export const pluginManager = new PluginManager();
|