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, 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, 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); }, addMultipleToSelection(entries: DirectoryEntry[]) { for (const entry of entries) { this.selection.add(entry); } }, removeFromSelection(entry: DirectoryEntry): boolean { return this.selection.delete(entry); }, toggleSelection(entry: DirectoryEntry) { if (this.selection.has(entry)) { this.removeFromSelection(entry); } else { this.addToSelection(entry); } }, isSelected(entry: DirectoryEntry): boolean { if (this.current == null) { return false; } return this.selection.has(entry); }, clearSelection() { this.selection.clear(); }, }, }); 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 = ''; }, }, });