Skip to content

Commit 04d8e83

Browse files
committed
feat(files_versions): Auto-reload versions tab on file
Listen for files:node:updated events and automatically refresh the versions list when the current file is saved, eliminating the need to manually close and reopen the sidebar to see new versions. Signed-off-by: silver <s.szmajduch@posteo.de>
1 parent 431a37e commit 04d8e83

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

apps/files_versions/src/views/FilesVersionsSidebarTab.vue

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ import type { IFolder, INode, IView } from '@nextcloud/files'
4545
import type { Version } from '../utils/versions.ts'
4646
4747
import { showError, showSuccess } from '@nextcloud/dialogs'
48-
import { emit } from '@nextcloud/event-bus'
48+
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
4949
import { t } from '@nextcloud/l10n'
5050
import { useIsMobile } from '@nextcloud/vue/composables/useIsMobile'
51-
import { computed, ref, toRef, watch } from 'vue'
51+
import { computed, onBeforeUnmount, onMounted, ref, toRef, watch } from 'vue'
5252
import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon'
5353
import VersionEntry from '../components/VersionEntry.vue'
5454
import VersionLabelDialog from '../components/VersionLabelDialog.vue'
@@ -72,19 +72,52 @@ const loading = ref(false)
7272
const showVersionLabelForm = ref(false)
7373
const editedVersion = ref<Version | null>(null)
7474
75-
watch(toRef(() => props.node), async () => {
75+
/**
76+
* Reload versions for the current file
77+
*/
78+
async function reloadVersions() {
7679
if (!props.node) {
7780
return
7881
}
7982
83+
const previousCount = versions.value.length
84+
8085
try {
8186
loading.value = true
8287
versions.value = await fetchVersions(props.node)
88+
console.debug('[FilesVersionsSidebarTab] Reloaded versions:', previousCount, '', versions.value.length)
8389
} finally {
8490
loading.value = false
8591
}
92+
}
93+
94+
/**
95+
* Handle files:node:updated event to reload versions when the current file is saved
96+
* @param node
97+
*/
98+
function handleNodeUpdated(node: INode) {
99+
// Only reload if this is the currently open file and the tab is active
100+
if (props.active && props.node && node.source === props.node.source) {
101+
console.debug('[FilesVersionsSidebarTab] File saved, reloading versions in 1s')
102+
// Delay to let the server create the new version
103+
setTimeout(() => {
104+
reloadVersions()
105+
}, 1000)
106+
}
107+
}
108+
109+
watch(toRef(() => props.node), async () => {
110+
await reloadVersions()
86111
}, { immediate: true })
87112
113+
onMounted(() => {
114+
subscribe('files:node:updated', handleNodeUpdated)
115+
})
116+
117+
onBeforeUnmount(() => {
118+
unsubscribe('files:node:updated', handleNodeUpdated)
119+
})
120+
88121
const currentVersionMtime = computed(() => props.node?.mtime?.getTime() ?? 0)
89122
90123
/**

0 commit comments

Comments
 (0)