Files
warren/frontend/stores/index.ts
2025-09-04 16:59:31 +02:00

185 lines
5.0 KiB
TypeScript

import { defineStore } from 'pinia';
import type { DirectoryEntry } from '#shared/types';
import type { WarrenData } from '#shared/types/warrens';
import { getParentPath } from '~/utils/files';
export const useWarrenStore = defineStore('warrens', {
state: () => ({
warrens: {} as Record<string, WarrenData>,
imageViewer: {
src: null as string | null,
},
current: null as {
warrenId: string;
path: string;
dir: {
parent: DirectoryEntry | null;
entries: DirectoryEntry[];
} | null;
} | null,
selection: new Set() as Set<DirectoryEntry>,
selectionRangeAnchor: null as DirectoryEntry | null,
loading: false,
}),
actions: {
setCurrentWarren(warrenId: string, path: string) {
this.current = {
warrenId,
path,
dir: null,
};
this.clearSelection();
},
setCurrentWarrenEntries(
entries: DirectoryEntry[],
parent: DirectoryEntry | null
) {
if (this.current == null) {
return;
}
this.current.dir = {
entries,
parent,
};
},
addToCurrentWarrenPath(path: string) {
if (this.current == null) {
return;
}
if (!this.current.path.endsWith('/')) {
path = '/' + path;
}
this.setCurrentWarrenPath(this.current.path + path);
},
backCurrentPath(): boolean {
if (this.current == null || this.current.path === '/') {
return false;
}
this.setCurrentWarrenPath(getParentPath(this.current.path));
return true;
},
setCurrentWarrenPath(path: string) {
if (this.current == null) {
return;
}
const previous = this.current.path;
if (!path.startsWith('/')) {
path = '/' + path;
}
this.current.path = path;
if (previous !== path) {
this.clearSelection();
this.current.dir = null;
}
},
clearCurrentWarren() {
this.current = null;
},
addToSelection(entry: DirectoryEntry) {
this.selection.add(entry);
},
setSelection(entries: DirectoryEntry[]) {
this.selection = new Set(entries);
if (
this.selectionRangeAnchor != null &&
!this.selection.has(this.selectionRangeAnchor)
) {
this.selectionRangeAnchor = null;
}
},
addMultipleToSelection(entries: DirectoryEntry[]) {
for (const entry of entries) {
this.selection.add(entry);
}
},
setSelectedRangeAnchor(entry: DirectoryEntry) {
this.selectionRangeAnchor = entry;
},
removeFromSelection(entry: DirectoryEntry): boolean {
return this.selection.delete(entry);
},
toggleSelection(entry: DirectoryEntry): boolean {
if (this.selection.has(entry)) {
this.removeFromSelection(entry);
return false;
} else {
this.addToSelection(entry);
return true;
}
},
isSelected(entry: DirectoryEntry): boolean {
if (this.current == null) {
return false;
}
return this.selection.has(entry);
},
clearSelection() {
this.selection.clear();
this.selectionRangeAnchor = null;
},
},
});
export const useCreateDirectoryDialog = defineStore('create_directory_dialog', {
state: () => ({
open: false,
value: '',
}),
actions: {
openDialog() {
this.open = true;
},
reset() {
this.open = false;
this.value = '';
},
},
});
export const useRenameDirectoryDialog = defineStore('rename_directory_dialog', {
state: () => ({
open: false,
entry: null as DirectoryEntry | null,
value: '',
}),
actions: {
openDialog(entry: DirectoryEntry) {
this.entry = entry;
this.value = entry.name;
this.open = true;
},
reset() {
this.open = false;
this.entry = null;
this.value = '';
},
},
});
export const useShareDialog = defineStore('share_dialog', {
state: () => ({
target: null as DirectoryEntry | null,
password: '',
}),
actions: {
openDialog(target: DirectoryEntry) {
this.target = target;
this.password = '';
},
reset() {
this.target = null;
this.password = '';
},
},
});