Skip to content

Commit

Permalink
feat(editLocallyAction): Handle possible no local client scenario
Browse files Browse the repository at this point in the history
Resolves: #46438

Signed-off-by: fenn-cs <fenn25.fn@gmail.com>
  • Loading branch information
Fenn-CS committed Jul 12, 2024
1 parent 76943ef commit ce7055b
Showing 1 changed file with 66 additions and 3 deletions.
69 changes: 66 additions & 3 deletions apps/files/src/actions/editLocallyAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,74 @@ 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 = (
title: string,
text: string,
confirmButtonText: string,
cancelButtonText: string,
localEditCallback: (openingLocally: boolean) => void = () => {},
) => {
let callbackCalled = false

return new DialogBuilder()
.setName(title)
.setText(text)
.setButtons([
{
label: cancelButtonText,
icon: IconCancel,
callback: () => {
callbackCalled = true
localEditCallback(false)
},
},
{
label: confirmButtonText,
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
if (!callbackCalled) {
localEditCallback(true)
}
})
}

const startOpenLocalProcess = async (path: string) => {
attemptOpenLocalClient(path)
}

const attemptOpenLocalClient = async (path: string) => {
openLocalClient(path)
confirmLocalEditDialog(
t('files', 'Edit file locally'),
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.'),
t('files', 'Retry local edit'),
t('files', 'Edit online'),
(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 +106,7 @@ export const action = new FileAction({
},

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

Expand Down

0 comments on commit ce7055b

Please sign in to comment.