74f79cae69
* refactor folder view inline link * improved tree collapsing * handle tags * linting * formatting * simplify * .from is faster * simplify * add key
56 lines
1.2 KiB
TypeScript
56 lines
1.2 KiB
TypeScript
import { eventManager } from '$lib/managers/event-manager.svelte';
|
|
import { TreeNode } from '$lib/utils/tree-utils';
|
|
import {
|
|
getAssetsByOriginalPath,
|
|
getUniqueOriginalPaths,
|
|
/**
|
|
* TODO: Incorrect type
|
|
*/
|
|
type AssetResponseDto,
|
|
} from '@immich/sdk';
|
|
|
|
type AssetCache = {
|
|
[path: string]: AssetResponseDto[];
|
|
};
|
|
|
|
class FoldersStore {
|
|
folders = $state.raw<TreeNode | null>(null);
|
|
private initialized = false;
|
|
private assets = $state<AssetCache>({});
|
|
|
|
constructor() {
|
|
eventManager.on('auth.logout', () => this.clearCache());
|
|
}
|
|
|
|
async fetchTree(): Promise<TreeNode> {
|
|
if (this.initialized) {
|
|
return this.folders!;
|
|
}
|
|
this.initialized = true;
|
|
|
|
this.folders = TreeNode.fromPaths(await getUniqueOriginalPaths());
|
|
this.folders.collapse();
|
|
return this.folders;
|
|
}
|
|
|
|
bustAssetCache() {
|
|
this.assets = {};
|
|
}
|
|
|
|
async refreshAssetsByPath(path: string) {
|
|
return (this.assets[path] = await getAssetsByOriginalPath({ path }));
|
|
}
|
|
|
|
async fetchAssetsByPath(path: string) {
|
|
return (this.assets[path] ??= await getAssetsByOriginalPath({ path }));
|
|
}
|
|
|
|
clearCache() {
|
|
this.initialized = false;
|
|
this.assets = {};
|
|
this.folders = null;
|
|
}
|
|
}
|
|
|
|
export const foldersStore = new FoldersStore();
|