diff options
Diffstat (limited to 'apps/files/src/store/files.ts')
-rw-r--r-- | apps/files/src/store/files.ts | 115 |
1 files changed, 78 insertions, 37 deletions
diff --git a/apps/files/src/store/files.ts b/apps/files/src/store/files.ts index 96752653735..f90f3bff7bb 100644 --- a/apps/files/src/store/files.ts +++ b/apps/files/src/store/files.ts @@ -24,51 +24,92 @@ import type { Folder, Node } from '@nextcloud/files' import type { FilesStore, RootsStore, RootOptions, Service, FilesState } from '../types' import { defineStore } from 'pinia' +import { subscribe } from '@nextcloud/event-bus' import Vue from 'vue' import logger from '../logger' -export const useFilesStore = defineStore('files', { - state: (): FilesState => ({ - files: {} as FilesStore, - roots: {} as RootsStore, - }), +export const useFilesStore = () => { + const store = defineStore('files', { + state: (): FilesState => ({ + files: {} as FilesStore, + roots: {} as RootsStore, + }), - getters: { - /** - * Get a file or folder by id - */ - getNode: (state) => (id: number): Node|undefined => state.files[id], + getters: { + /** + * Get a file or folder by id + */ + getNode: (state) => (id: number): Node|undefined => state.files[id], - /** - * Get a list of files or folders by their IDs - * Does not return undefined values - */ - getNodes: (state) => (ids: number[]): Node[] => ids - .map(id => state.files[id]) - .filter(Boolean), - /** - * Get a file or folder by id - */ - getRoot: (state) => (service: Service): Folder|undefined => state.roots[service], - }, + /** + * Get a list of files or folders by their IDs + * Does not return undefined values + */ + getNodes: (state) => (ids: number[]): Node[] => ids + .map(id => state.files[id]) + .filter(Boolean), + /** + * Get a file or folder by id + */ + getRoot: (state) => (service: Service): Folder|undefined => state.roots[service], + }, - actions: { - updateNodes(nodes: Node[]) { - // Update the store all at once - const files = nodes.reduce((acc, node) => { - if (!node.attributes.fileid) { - logger.warn('Trying to update/set a node without fileid', node) + actions: { + updateNodes(nodes: Node[]) { + // Update the store all at once + const files = nodes.reduce((acc, node) => { + if (!node.attributes.fileid) { + logger.warn('Trying to update/set a node without fileid', node) + return acc + } + acc[node.attributes.fileid] = node return acc - } - acc[node.attributes.fileid] = node - return acc - }, {} as FilesStore) + }, {} as FilesStore) - Vue.set(this, 'files', {...this.files, ...files}) - }, + Vue.set(this, 'files', {...this.files, ...files}) + }, + + deleteNodes(nodes: Node[]) { + nodes.forEach(node => { + if (node.fileid) { + Vue.delete(this.files, node.fileid) + } + }) + }, + + setRoot({ service, root }: RootOptions) { + Vue.set(this.roots, service, root) + }, + + onCreatedNode() { + // TODO: do something + }, - setRoot({ service, root }: RootOptions) { - Vue.set(this.roots, service, root) + onDeletedNode(node: Node) { + this.deleteNodes([node]) + }, + + onMovedNode() { + // TODO: do something + }, } + }) + + const fileStore = store() + // Make sure we only register the listeners once + if (!fileStore.initialized) { + subscribe('files:file:created', fileStore.onCreatedNode) + subscribe('files:file:deleted', fileStore.onDeletedNode) + subscribe('files:file:moved', fileStore.onMovedNode) + // subscribe('files:file:updated', fileStore.onUpdatedNode) + + subscribe('files:folder:created', fileStore.onCreatedNode) + subscribe('files:folder:deleted', fileStore.onDeletedNode) + subscribe('files:folder:moved', fileStore.onMovedNode) + // subscribe('files:folder:updated', fileStore.onUpdatedNode) + + fileStore.initialized = true } -}) + + return fileStore +} |