Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions apps/files/src/store/active.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*!
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { Folder } from '@nextcloud/files'
import { createPinia, setActivePinia } from 'pinia'
import { beforeEach, describe, expect, test, vi } from 'vitest'
import { nextTick } from 'vue'
import { useActiveStore } from './active.ts'

function buildFolder(id: number, name: string) {
return new Folder({
id,
owner: 'test',
source: `http://example.com/remote.php/dav/files/test/${name}`,
root: '/files/test',
})
}

describe('Active store syncs the route fileid with the active node', () => {
let goToRoute: ReturnType<typeof vi.fn>

beforeEach(() => {
setActivePinia(createPinia())
goToRoute = vi.fn()
window.OCP = { Files: { Router: { goToRoute, params: {}, query: {} } } } as unknown as typeof window.OCP
})

test('rewrites a stale child fileid when the current folder becomes active', async () => {
// The route still deep-links a child (79) left over from a previous
// location, while the sidebar is being opened for the current folder (78).
window.OCP.Files.Router.params = { fileid: '79' }

const store = useActiveStore()
const folder = buildFolder(78, 'parent')
store.activeFolder = folder
store.activeNode = folder
await nextTick()

expect(goToRoute).toHaveBeenCalledTimes(1)
expect(goToRoute.mock.calls[0][1]).toMatchObject({ fileid: '78' })
})

test('does not touch the route when it already points at the active node', async () => {
window.OCP.Files.Router.params = { fileid: '78' }

const store = useActiveStore()
const folder = buildFolder(78, 'parent')
store.activeFolder = folder
store.activeNode = folder
await nextTick()

expect(goToRoute).not.toHaveBeenCalled()
})
})
13 changes: 10 additions & 3 deletions apps/files/src/store/active.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,21 @@ export const useActiveStore = defineStore('active', () => {

// Set the active node on the router params
watch(activeNode, () => {
if (typeof activeNode.value?.fileid !== 'number' || activeNode.value.fileid === activeFolder.value?.fileid) {
if (!activeNode.value?.id) {
return
}

logger.debug('Updating active fileid in URL query', { fileid: activeNode.value.fileid })
// Sync even when the active node is the current folder: skipping that case
// leaves a stale child fileid in the route, which then gets restored into
// the sidebar when it reopens.
if (activeNode.value.id === String(window.OCP.Files.Router.params?.fileid ?? '')) {
return
}

logger.debug('Updating active fileid in URL query', { fileid: activeNode.value.id })
window.OCP.Files.Router.goToRoute(
null,
{ ...window.OCP.Files.Router.params, fileid: String(activeNode.value.fileid) },
{ ...window.OCP.Files.Router.params, fileid: activeNode.value.id },
{ ...window.OCP.Files.Router.query },
true,
)
Expand Down
4 changes: 2 additions & 2 deletions dist/files-init.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/files-init.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/files-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/files-main.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/files-sidebar.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/files-sidebar.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/files_sharing-init-public.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/files_sharing-init-public.js.map

Large diffs are not rendered by default.

Loading