aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/utils/actionUtils.ts
blob: 730a1149229b98d1097eb14347717413f34c0683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
import type { FileAction } from '@nextcloud/files'

import { NodeStatus } from '@nextcloud/files'
import { showError, showSuccess } from '@nextcloud/dialogs'
import { t } from '@nextcloud/l10n'
import Vue from 'vue'

import { getPinia } from '../store'
import { useActiveStore } from '../store/active'
import logger from '../logger'

/**
 * Execute an action on the current active node
 *
 * @param action The action to execute
 */
export const executeAction = async (action: FileAction) => {
	const activeStore = useActiveStore(getPinia())
	const currentDir = (window?.OCP?.Files?.Router?.query?.dir || '/') as string
	const currentNode = activeStore.activeNode
	const currentView = activeStore.activeView

	if (!currentNode || !currentView) {
		logger.error('No active node or view', { node: currentNode, view: currentView })
		return
	}

	if (currentNode.status === NodeStatus.LOADING) {
		logger.debug('Node is already loading', { node: currentNode })
		return
	}

	if (!action.enabled!([currentNode], currentView)) {
		logger.debug('Action is not not available for the current context', { action, node: currentNode, view: currentView })
		return
	}

	let displayName = action.id
	try {
		displayName = action.displayName([currentNode], currentView)
	} catch (error) {
		logger.error('Error while getting action display name', { action, error })
	}

	try {
		// Set the loading marker
		Vue.set(currentNode, 'status', NodeStatus.LOADING)
		activeStore.setActiveAction(action)

		const success = await action.exec(currentNode, currentView, currentDir)

		// If the action returns null, we stay silent
		if (success === null || success === undefined) {
			return
		}

		if (success) {
			showSuccess(t('files', '"{displayName}" action executed successfully', { displayName }))
			return
		}
		showError(t('files', '"{displayName}" action failed', { displayName }))
	} catch (error) {
		logger.error('Error while executing action', { action, error })
		showError(t('files', '"{displayName}" action failed', { displayName }))
	} finally {
		// Reset the loading marker
		Vue.set(currentNode, 'status', undefined)
		activeStore.clearActiveAction()
	}
}