From 3c74559e4ce81ba5b0427125c67c5d31c33c0376 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Wed, 17 Apr 2024 17:03:21 +0200 Subject: [PATCH 1/4] fix(files): Close sidebar if shown node is deleted Signed-off-by: Ferdinand Thiessen --- apps/files/src/views/Sidebar.vue | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/apps/files/src/views/Sidebar.vue b/apps/files/src/views/Sidebar.vue index e174dee7756fd..3b79cda260c50 100644 --- a/apps/files/src/views/Sidebar.vue +++ b/apps/files/src/views/Sidebar.vue @@ -105,7 +105,7 @@ import { getCurrentUser } from '@nextcloud/auth' import { getCapabilities } from '@nextcloud/capabilities' import { showError } from '@nextcloud/dialogs' -import { emit } from '@nextcloud/event-bus' +import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus' import { File, Folder, formatFileSize } from '@nextcloud/files' import { encodePath } from '@nextcloud/paths' import { generateRemoteUrl, generateUrl } from '@nextcloud/router' @@ -304,10 +304,13 @@ export default { }, }, created() { + subscribe('files:node:deleted', this.onNodeDeleted) + window.addEventListener('resize', this.handleWindowResize) this.handleWindowResize() }, beforeDestroy() { + unsubscribe('file:node:deleted', this.onNodeDeleted) window.removeEventListener('resize', this.handleWindowResize) }, @@ -507,6 +510,16 @@ export default { this.resetData() }, + /** + * Handle if the current node was deleted + * @param {import('@nextcloud/files').Node} node The deleted node + */ + onNodeDeleted(node) { + if (this.fileInfo && node && this.fileInfo.id === node.fileid) { + this.close() + } + }, + /** * Allow to set the Sidebar as fullscreen from OCA.Files.Sidebar * From 3808a5b0565847284f8a8398d8f10ac7da51e306 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Wed, 17 Apr 2024 17:03:44 +0200 Subject: [PATCH 2/4] fix(files): Update current fileid in route if that node was deleted We do not change the view to the trash bin but stay in the current view, so we need to update the current fileid on the route if that was deleted. Signed-off-by: Ferdinand Thiessen --- apps/files/src/actions/deleteAction.ts | 5 ++-- apps/files/src/store/paths.ts | 2 +- apps/files/src/views/FilesList.vue | 36 ++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/apps/files/src/actions/deleteAction.ts b/apps/files/src/actions/deleteAction.ts index 8148dd595157c..a9c2aadc8ec45 100644 --- a/apps/files/src/actions/deleteAction.ts +++ b/apps/files/src/actions/deleteAction.ts @@ -140,14 +140,15 @@ export const action = new FileAction({ .every(permission => (permission & Permission.DELETE) !== 0) }, - async exec(node: Node) { + async exec(node: Node, view: View, dir: string) { try { await axios.delete(node.encodedSource) // Let's delete even if it's moved to the trashbin // since it has been removed from the current view - // and changing the view will trigger a reload anyway. + // and changing the view will trigger a reload anyway. emit('files:node:deleted', node) + return true } catch (error) { logger.error('Error while deleting a file', { error, source: node.source, node }) diff --git a/apps/files/src/store/paths.ts b/apps/files/src/store/paths.ts index 49b0ec9495b63..15d16b1760b52 100644 --- a/apps/files/src/store/paths.ts +++ b/apps/files/src/store/paths.ts @@ -29,7 +29,7 @@ import logger from '../logger' import { useFilesStore } from './files' export const usePathsStore = function(...args) { - const files = useFilesStore() + const files = useFilesStore(...args) const store = defineStore('paths', { state: () => ({ diff --git a/apps/files/src/views/FilesList.vue b/apps/files/src/views/FilesList.vue index bd42e80c3a70e..9c73947679324 100644 --- a/apps/files/src/views/FilesList.vue +++ b/apps/files/src/views/FilesList.vue @@ -240,6 +240,14 @@ export default defineComponent({ return (this.$route?.query?.dir?.toString() || '/').replace(/^(.+)\/$/, '$1') }, + /** + * The current file id + */ + fileId(): number | null { + const number = Number.parseInt(this.$route?.params.fileid ?? '') + return Number.isNaN(number) ? null : number + }, + /** * The current folder. */ @@ -453,6 +461,8 @@ export default defineComponent({ mounted() { this.fetchContent() + + subscribe('files:node:deleted', this.onNodeDeleted) subscribe('files:node:updated', this.onUpdatedNode) subscribe('nextcloud:unified-search.search', this.onSearch) subscribe('nextcloud:unified-search.reset', this.onSearch) @@ -462,6 +472,7 @@ export default defineComponent({ }, unmounted() { + unsubscribe('files:node:deleted', this.onNodeDeleted) unsubscribe('files:node:updated', this.onUpdatedNode) unsubscribe('nextcloud:unified-search.search', this.onSearch) unsubscribe('nextcloud:unified-search.reset', this.onSearch) @@ -535,6 +546,31 @@ export default defineComponent({ return this.filesStore.getNode(fileId) }, + /** + * Handle the node deleted event to reset open file + * @param node The deleted node + */ + onNodeDeleted(node: Node) { + if (node.fileid && node.fileid === this.fileId) { + if (node.fileid === this.currentFolder?.fileid) { + // Handle the edge case that the current directory is deleted + // in this case we neeed to keept the current view but move to the parent directory + window.OCP.Files.Router.goToRoute( + null, + { view: this.$route.params.view }, + { dir: this.currentFolder?.dirname ?? '/' }, + ) + } else { + // If the currently active file is deleted we need to remove the fileid and possible the `openfile` query + window.OCP.Files.Router.goToRoute( + null, + { ...this.$route.params, fileid: undefined }, + { ...this.$route.query, openfile: undefined }, + ) + } + } + }, + /** * The upload manager have finished handling the queue * @param {Upload} upload the uploaded data From 892f17a7a8b32450fe392840875a18cea9dd6078 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Wed, 17 Apr 2024 17:38:41 +0200 Subject: [PATCH 3/4] feat(cypress): Add tests for files sidebar Signed-off-by: Ferdinand Thiessen --- apps/files/src/views/Sidebar.vue | 1 + cypress/e2e/files/FilesUtils.ts | 4 +- cypress/e2e/files/files-sidebar.cy.ts | 98 +++++++++++++++++++++++++++ cypress/support/commands.ts | 9 ++- 4 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 cypress/e2e/files/files-sidebar.cy.ts diff --git a/apps/files/src/views/Sidebar.vue b/apps/files/src/views/Sidebar.vue index 3b79cda260c50..16c7fed24f504 100644 --- a/apps/files/src/views/Sidebar.vue +++ b/apps/files/src/views/Sidebar.vue @@ -23,6 +23,7 @@