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

feat(editLocallyAction): Handle possible no local client scenario #46452

Merged
merged 3 commits into from
Jul 31, 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
43 changes: 33 additions & 10 deletions apps/files/src/actions/editLocallyAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@
import { action } from './editLocallyAction'
import { expect } from '@jest/globals'
import { File, Permission, View, FileAction } from '@nextcloud/files'
import * as ncDialogs from '@nextcloud/dialogs'
import { DialogBuilder, showError } from '@nextcloud/dialogs'
import axios from '@nextcloud/axios'

const dialogBuilder = {
setName: jest.fn().mockReturnThis(),
setText: jest.fn().mockReturnThis(),
setButtons: jest.fn().mockReturnThis(),
build: jest.fn().mockReturnValue({
show: jest.fn().mockResolvedValue(true),
}),
} as unknown as DialogBuilder

jest.mock('@nextcloud/dialogs', () => ({
DialogBuilder: jest.fn(() => dialogBuilder),
showError: jest.fn(),
}))

const view = {
id: 'files',
name: 'Files',
Expand All @@ -16,7 +30,8 @@ const view = {
// Mock webroot variable
beforeAll(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any)._oc_webroot = ''
(window as any)._oc_webroot = '';
(window as any).OCA = { Viewer: { open: jest.fn() } }
})

describe('Edit locally action conditions tests', () => {
Expand Down Expand Up @@ -44,7 +59,7 @@ describe('Edit locally action enabled tests', () => {
expect(action.enabled!([file], view)).toBe(true)
})

test('Disabled for non-dav ressources', () => {
test('Disabled for non-dav resources', () => {
const file = new File({
id: 1,
source: 'https://domain.com/data/foobar.txt',
Expand Down Expand Up @@ -104,8 +119,11 @@ describe('Edit locally action enabled tests', () => {

describe('Edit locally action execute tests', () => {
test('Edit locally opens proper URL', async () => {
jest.spyOn(axios, 'post').mockImplementation(async () => ({ data: { ocs: { data: { token: 'foobar' } } } }))
jest.spyOn(ncDialogs, 'showError')
jest.spyOn(axios, 'post').mockImplementation(async () => ({
data: { ocs: { data: { token: 'foobar' } } }
}))
const mockedShowError = jest.mocked(showError)
const spyDialogBuilder = jest.spyOn(dialogBuilder, 'build')

const file = new File({
id: 1,
Expand All @@ -117,17 +135,20 @@ describe('Edit locally action execute tests', () => {

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

expect(spyDialogBuilder).toBeCalled()

// Silent action
expect(exec).toBe(null)
expect(axios.post).toBeCalledTimes(1)
expect(axios.post).toBeCalledWith('http://localhost/ocs/v2.php/apps/files/api/v1/openlocaleditor?format=json', { path: '/foobar.txt' })
expect(ncDialogs.showError).toBeCalledTimes(0)
expect(mockedShowError).toBeCalledTimes(0)
expect(window.location.href).toBe('nc://open/test@localhost/foobar.txt?token=foobar')
})

test('Edit locally fails and show error', async () => {
test('Edit locally fails and shows error', async () => {
jest.spyOn(axios, 'post').mockImplementation(async () => ({}))
jest.spyOn(ncDialogs, 'showError')
const mockedShowError = jest.mocked(showError)
const spyDialogBuilder = jest.spyOn(dialogBuilder, 'build')

const file = new File({
id: 1,
Expand All @@ -139,12 +160,14 @@ describe('Edit locally action execute tests', () => {

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

expect(spyDialogBuilder).toBeCalled()

// Silent action
expect(exec).toBe(null)
expect(axios.post).toBeCalledTimes(1)
expect(axios.post).toBeCalledWith('http://localhost/ocs/v2.php/apps/files/api/v1/openlocaleditor?format=json', { path: '/foobar.txt' })
expect(ncDialogs.showError).toBeCalledTimes(1)
expect(ncDialogs.showError).toBeCalledWith('Failed to redirect to client')
expect(mockedShowError).toBeCalledTimes(1)
expect(mockedShowError).toBeCalledWith('Failed to redirect to client')
expect(window.location.href).toBe('http://localhost/')
})
})
57 changes: 54 additions & 3 deletions apps/files/src/actions/editLocallyAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,62 @@ import { encodePath } from '@nextcloud/paths'
import { generateOcsUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import { FileAction, Permission, type Node } from '@nextcloud/files'
import { showError } from '@nextcloud/dialogs'
import { showError, DialogBuilder } from '@nextcloud/dialogs'
import { translate as t } from '@nextcloud/l10n'
import axios from '@nextcloud/axios'

import LaptopSvg from '@mdi/svg/svg/laptop.svg?raw'
import IconCancel from '@mdi/svg/svg/cancel.svg?raw'
import IconCheck from '@mdi/svg/svg/check.svg?raw'

const confirmLocalEditDialog = (
localEditCallback: (openingLocally: boolean) => void = () => {},
) => {
let callbackCalled = false

return (new DialogBuilder())
.setName(t('files', 'Edit file locally'))
.setText(t('files', 'The file should now open locally. If you don\'t see this happening, make sure that the desktop client is installed on your system.'))
.setButtons([
{
label: t('files', 'Retry local edit'),
icon: IconCancel,
callback: () => {
callbackCalled = true
localEditCallback(false)
},
},
{
label: t('files', 'Edit online'),
icon: IconCheck,
type: 'primary',
callback: () => {
callbackCalled = true
localEditCallback(true)
},
},
])
.build()
.show()
.then(() => {
// Ensure the callback is called even if the dialog is dismissed in other ways
Fenn-CS marked this conversation as resolved.
Show resolved Hide resolved
if (!callbackCalled) {
localEditCallback(false)
}
})
}

const attemptOpenLocalClient = async (path: string) => {
openLocalClient(path)
confirmLocalEditDialog(
(openLocally: boolean) => {
if (!openLocally) {
window.OCA.Viewer.open({ path })
return
}
openLocalClient(path)
},
)
}

const openLocalClient = async function(path: string) {
const link = generateOcsUrl('apps/files/api/v1') + '/openlocaleditor?format=json'
Expand Down Expand Up @@ -43,7 +94,7 @@ export const action = new FileAction({
},

async exec(node: Node) {
openLocalClient(node.path)
attemptOpenLocalClient(node.path)
return null
},

Expand Down
4 changes: 2 additions & 2 deletions dist/core-common.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/core-common.js.map

Large diffs are not rendered by default.

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_sharing-init.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/settings-users-3239.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions dist/settings-users-3239.js.license
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ This file is generated from multiple sources. Included packages:
- @mdi/js
- version: 7.4.47
- license: Apache-2.0
- @mdi/svg
- version: 7.4.47
- license: Apache-2.0
- @nextcloud/auth
- version: 2.3.0
- license: GPL-3.0-or-later
Expand Down
2 changes: 1 addition & 1 deletion dist/settings-users-3239.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/settings-vue-settings-apps-users-management.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/settings-vue-settings-apps-users-management.js.map

Large diffs are not rendered by default.

Loading