Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(systemtags): Sub folders should be opened in files #47135

Merged
merged 1 commit into from
Aug 9, 2024
Merged
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
123 changes: 123 additions & 0 deletions apps/systemtags/src/files_actions/openInFilesAction.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { action } from './openInFilesAction'
import { expect } from '@jest/globals'
import { File, Folder, Permission, View, DefaultType, FileAction } from '@nextcloud/files'

const view = {
id: 'files',
name: 'Files',
} as View

const systemTagsView = {
id: 'tags',
name: 'tags',
} as View

const validNode = new Folder({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo',
owner: 'admin',
mime: 'httpd/unix-directory',
root: '/files/admin',
permissions: Permission.ALL,
})

const validTag = new Folder({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/systemtags/2',
displayname: 'Foo',
owner: 'admin',
mime: 'httpd/unix-directory',
root: '/systemtags',
permissions: Permission.ALL,
attributes: {
'is-tag': true,
},
})

describe('Open in files action conditions tests', () => {
test('Default values', () => {
expect(action).toBeInstanceOf(FileAction)
expect(action.id).toBe('systemtags:open-in-files')
expect(action.displayName([], systemTagsView)).toBe('Open in Files')
expect(action.iconSvgInline([], systemTagsView)).toBe('')
expect(action.default).toBe(DefaultType.HIDDEN)
expect(action.order).toBe(-1000)
expect(action.inline).toBeUndefined()
})
})

describe('Open in files action enabled tests', () => {
test('Enabled with on valid view', () => {
expect(action.enabled).toBeDefined()
expect(action.enabled!([validNode], systemTagsView)).toBe(true)
})

test('Disabled on wrong view', () => {
expect(action.enabled).toBeDefined()
expect(action.enabled!([validNode], view)).toBe(false)
})

test('Disabled without nodes', () => {
expect(action.enabled).toBeDefined()
expect(action.enabled!([], view)).toBe(false)
})

test('Disabled with too many nodes', () => {
expect(action.enabled).toBeDefined()
expect(action.enabled!([validNode, validNode], view)).toBe(false)
})

test('Disabled with when node is a tag', () => {
expect(action.enabled).toBeDefined()
expect(action.enabled!([validTag], view)).toBe(false)
})
})

describe('Open in files action execute tests', () => {
test('Open in files', async () => {
const goToRouteMock = jest.fn()
// @ts-expect-error We only mock what needed, we do not need Files.Router.goTo or Files.Navigation
window.OCP = { Files: { Router: { goToRoute: goToRouteMock } } }

const file = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/Foo/foobar.txt',
owner: 'admin',
mime: 'text/plain',
root: '/files/admin',
permissions: Permission.ALL,
})

const exec = await action.exec(file, view, '/')

// Silent action
expect(exec).toBe(null)
expect(goToRouteMock).toBeCalledTimes(1)
expect(goToRouteMock).toBeCalledWith(null, { fileid: '1', view: 'files' }, { dir: '/Foo', openfile: 'true' })
})

test('Open in files with folder', async () => {
const goToRouteMock = jest.fn()
// @ts-expect-error We only mock what needed, we do not need Files.Router.goTo or Files.Navigation
window.OCP = { Files: { Router: { goToRoute: goToRouteMock } } }

const file = new Folder({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/Foo/Bar',
owner: 'admin',
root: '/files/admin',
permissions: Permission.ALL,
})

const exec = await action.exec(file, view, '/')

// Silent action
expect(exec).toBe(null)
expect(goToRouteMock).toBeCalledTimes(1)
expect(goToRouteMock).toBeCalledWith(null, { fileid: '1', view: 'files' }, { dir: '/Foo/Bar', openfile: 'true' })
})
})
44 changes: 44 additions & 0 deletions apps/systemtags/src/files_actions/openInFilesAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { translate as t } from '@nextcloud/l10n'
import { type Node, FileType, FileAction, DefaultType } from '@nextcloud/files'

export const action = new FileAction({
id: 'systemtags:open-in-files',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bother me more than it should 😅😅

displayName: () => t('systemtags', 'Open in Files'),
iconSvgInline: () => '',

enabled(nodes, view) {
// Only for the system tags view
if (view.id !== 'tags') {
return false
}
// Only for single nodes
if (nodes.length !== 1) {
return false
}
// Do not open tags (keep the default action) and only open folders
return nodes[0].attributes['is-tag'] !== true
&& nodes[0].type === FileType.Folder
},

async exec(node: Node) {
let dir = node.dirname
if (node.type === FileType.Folder) {
dir = node.path
}

window.OCP.Files.Router.goToRoute(
null, // use default route
{ view: 'files', fileid: String(node.fileid) },
{ dir, openfile: 'true' },
)
return null
},

// Before openFolderAction
order: -1000,
default: DefaultType.HIDDEN,
})
2 changes: 2 additions & 0 deletions apps/systemtags/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
*/
import { registerDavProperty, registerFileAction } from '@nextcloud/files'
import { action as inlineSystemTagsAction } from './files_actions/inlineSystemTagsAction.js'
import { action as openInFilesAction } from './files_actions/openInFilesAction.js'
import { registerSystemTagsView } from './files_views/systemtagsView.js'

registerDavProperty('nc:system-tags')
registerFileAction(inlineSystemTagsAction)
registerFileAction(openInFilesAction)

registerSystemTagsView()
4 changes: 2 additions & 2 deletions dist/systemtags-init.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

Loading