Files
immich/web/src/lib/stores/memory.store.svelte.ts
Jason Rasmussen d0014bdf94
Some checks are pending
CodeQL / Analyze (javascript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
Docker / pre-job (push) Waiting to run
Docker / Re-Tag ML () (push) Blocked by required conditions
Docker / Re-Tag ML (-armnn) (push) Blocked by required conditions
Docker / Re-Tag ML (-cuda) (push) Blocked by required conditions
Docker / Re-Tag ML (-openvino) (push) Blocked by required conditions
Docker / Re-Tag ML (-rknn) (push) Blocked by required conditions
Docker / Re-Tag ML (-rocm) (push) Blocked by required conditions
Docker / Re-Tag Server () (push) Blocked by required conditions
Docker / Build and Push ML (armnn, linux/arm64, ubuntu-24.04-arm, -armnn) (push) Blocked by required conditions
Docker / Build and Push ML (cpu, linux/amd64, ubuntu-latest) (push) Blocked by required conditions
Docker / Build and Push ML (cpu, linux/arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
Docker / Build and Push ML (cuda, linux/amd64, ubuntu-latest, -cuda) (push) Blocked by required conditions
Docker / Build and Push ML (openvino, linux/amd64, ubuntu-latest, -openvino) (push) Blocked by required conditions
Docker / Build and Push ML (rknn, linux/arm64, ubuntu-24.04-arm, -rknn) (push) Blocked by required conditions
Docker / Build and Push ML (rocm, linux/amd64, mich, -rocm) (push) Blocked by required conditions
Docker / Merge & Push ML (armnn, -armnn) (push) Blocked by required conditions
Docker / Merge & Push ML (cpu) (push) Blocked by required conditions
Docker / Merge & Push ML (cuda, -cuda) (push) Blocked by required conditions
Docker / Merge & Push ML (openvino, -openvino) (push) Blocked by required conditions
Docker / Merge & Push ML (rknn, -rknn) (push) Blocked by required conditions
Docker / Merge & Push ML (rocm, -rocm) (push) Blocked by required conditions
Docker / Build and Push Server (linux/amd64, ubuntu-latest) (push) Blocked by required conditions
Docker / Build and Push Server (linux/arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
Docker / Merge & Push Server (push) Blocked by required conditions
Docker / Docker Build & Push Server Success (push) Blocked by required conditions
Docker / Docker Build & Push ML Success (push) Blocked by required conditions
Docs build / pre-job (push) Waiting to run
Docs build / Docs Build (push) Blocked by required conditions
Static Code Analysis / pre-job (push) Waiting to run
Static Code Analysis / Run Dart Code Analysis (push) Blocked by required conditions
Static Code Analysis / zizmor (push) Waiting to run
Test / pre-job (push) Waiting to run
Test / Test & Lint Server (push) Blocked by required conditions
Test / Unit Test CLI (push) Blocked by required conditions
Test / Unit Test CLI (Windows) (push) Blocked by required conditions
Test / Lint Web (push) Blocked by required conditions
Test / Test Web (push) Blocked by required conditions
Test / End-to-End Lint (push) Blocked by required conditions
Test / Medium Tests (Server) (push) Blocked by required conditions
Test / End-to-End Tests (Server & CLI) (push) Blocked by required conditions
Test / End-to-End Tests (Web) (push) Blocked by required conditions
Test / Unit Test Mobile (push) Blocked by required conditions
Test / Unit Test ML (push) Blocked by required conditions
Test / .github Files Formatting (push) Blocked by required conditions
Test / ShellCheck (push) Waiting to run
Test / OpenAPI Clients (push) Waiting to run
Test / TypeORM Checks (push) Waiting to run
refactor: event manager (#17862)
* refactor: event manager

* refactor: event manager
2025-04-25 08:36:31 -04:00

130 lines
3.5 KiB
TypeScript

import { eventManager } from '$lib/stores/event-manager.svelte';
import { asLocalTimeISO } from '$lib/utils/date-time';
import {
type AssetResponseDto,
deleteMemory,
type MemoryResponseDto,
removeMemoryAssets,
searchMemories,
updateMemory,
} from '@immich/sdk';
import { DateTime } from 'luxon';
type MemoryIndex = {
memoryIndex: number;
assetIndex: number;
};
export type MemoryAsset = MemoryIndex & {
memory: MemoryResponseDto;
asset: AssetResponseDto;
previousMemory?: MemoryResponseDto;
previous?: MemoryAsset;
next?: MemoryAsset;
nextMemory?: MemoryResponseDto;
};
class MemoryStoreSvelte {
constructor() {
eventManager.on('auth.logout', () => this.clearCache());
}
memories = $state<MemoryResponseDto[]>([]);
private initialized = false;
private memoryAssets = $derived.by(() => {
const memoryAssets: MemoryAsset[] = [];
let previous: MemoryAsset | undefined;
for (const [memoryIndex, memory] of this.memories.entries()) {
for (const [assetIndex, asset] of memory.assets.entries()) {
const current = {
memory,
memoryIndex,
previousMemory: this.memories[memoryIndex - 1],
nextMemory: this.memories[memoryIndex + 1],
asset,
assetIndex,
previous,
};
memoryAssets.push(current);
if (previous) {
previous.next = current;
}
previous = current;
}
}
return memoryAssets;
});
getMemoryAsset(assetId: string | undefined) {
return this.memoryAssets.find((memoryAsset) => memoryAsset.asset.id === assetId) ?? this.memoryAssets[0];
}
hideAssetsFromMemory(ids: string[]) {
const idSet = new Set<string>(ids);
for (const memory of this.memories) {
memory.assets = memory.assets.filter((asset) => !idSet.has(asset.id));
}
// if we removed all assets from a memory, then lets remove those memories (we don't show memories with 0 assets)
this.memories = this.memories.filter((memory) => memory.assets.length > 0);
}
async deleteMemory(id: string) {
const memory = this.memories.find((memory) => memory.id === id);
if (memory) {
await deleteMemory({ id: memory.id });
this.memories = this.memories.filter((memory) => memory.id !== id);
}
}
async deleteAssetFromMemory(assetId: string) {
const memoryWithAsset = this.memories.find((memory) => memory.assets.some((asset) => asset.id === assetId));
if (memoryWithAsset) {
if (memoryWithAsset.assets.length === 1) {
await this.deleteMemory(memoryWithAsset.id);
} else {
await removeMemoryAssets({ id: memoryWithAsset.id, bulkIdsDto: { ids: [assetId] } });
memoryWithAsset.assets = memoryWithAsset.assets.filter((asset) => asset.id !== assetId);
}
}
}
async updateMemorySaved(id: string, isSaved: boolean) {
const memory = this.memories.find((memory) => memory.id === id);
if (memory) {
await updateMemory({
id,
memoryUpdateDto: {
isSaved,
},
});
memory.isSaved = isSaved;
}
}
async initialize() {
if (this.initialized) {
return;
}
this.initialized = true;
await this.loadAllMemories();
}
clearCache() {
this.initialized = false;
this.memories = [];
}
private async loadAllMemories() {
const memories = await searchMemories({ $for: asLocalTimeISO(DateTime.now()) });
this.memories = memories.filter((memory) => memory.assets.length > 0);
}
}
export const memoryStore = new MemoryStoreSvelte();