Skip to content

Commit

Permalink
fixup! fix(files): simplify active store handling
Browse files Browse the repository at this point in the history
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Dec 11, 2024
1 parent d33525c commit 54caf2d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 11 deletions.
11 changes: 11 additions & 0 deletions apps/files/src/actions/deleteAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { Permission, Node, View, FileAction } from '@nextcloud/files'
import { showInfo } from '@nextcloud/dialogs'
import { translate as t } from '@nextcloud/l10n'
import { useHotKey } from '@nextcloud/vue/dist/Composables/useHotKey.js'
import PQueue from 'p-queue'

import CloseSvg from '@mdi/svg/svg/close.svg?raw'
Expand All @@ -13,6 +14,7 @@ import TrashCanSvg from '@mdi/svg/svg/trash-can.svg?raw'

import logger from '../logger.ts'
import { askConfirmation, canDisconnectOnly, canUnshareOnly, deleteNode, displayName, isTrashbinEnabled } from './deleteUtils'
import { executeAction } from '../utils/actionUtils.ts'

const queue = new PQueue({ concurrency: 5 })

Expand Down Expand Up @@ -107,3 +109,12 @@ export const action = new FileAction({

order: 100,
})

const executeDeleteAction = function() {
executeAction(action)
}

useHotKey('Delete', executeDeleteAction, {
stop: true,
prevent: true,
})
5 changes: 0 additions & 5 deletions apps/files/src/components/FileEntry/FileEntryActions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,6 @@ export default defineComponent({
prevent: true,
})

useHotKey('Delete', this.onKeyDown, {
stop: true,
prevent: true,
})

useHotKey('s', this.onKeyDown, {
stop: true,
prevent: true,
Expand Down
1 change: 1 addition & 0 deletions apps/files/src/store/active.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const useActiveStore = function(...args) {
subscribe('files:node:deleted', activeStore.onDeletedNode)

activeStore._initialized = true
activeStore.onChangedView(navigation.active)

// Or you can react to changes of the current active view
navigation.addEventListener('updateActive', (event) => {
Expand Down
61 changes: 61 additions & 0 deletions apps/files/src/utils/actionUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* 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 { useActiveStore } from '../store/active'
import logger from '../logger'

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

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)

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)
}
}
13 changes: 7 additions & 6 deletions apps/files/src/views/FilesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,17 @@ import type { Route } from 'vue-router'
import type { Upload } from '@nextcloud/upload'
import type { UserConfig } from '../types.ts'

import { getCapabilities } from '@nextcloud/capabilities'
import { defineComponent } from 'vue'
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
import { Node, Permission, sortNodes, getFileListActions } from '@nextcloud/files'
import { translate as t } from '@nextcloud/l10n'
import { getCapabilities } from '@nextcloud/capabilities'
import { join, dirname, normalize } from 'path'
import { showError, showWarning } from '@nextcloud/dialogs'
import { loadState } from '@nextcloud/initial-state'
import { Node, Permission, sortNodes, getFileListActions } from '@nextcloud/files'
import { ShareType } from '@nextcloud/sharing'
import { showError, showWarning } from '@nextcloud/dialogs'
import { translate as t } from '@nextcloud/l10n'
import { UploadPicker, UploadStatus } from '@nextcloud/upload'
import { loadState } from '@nextcloud/initial-state'
import { defineComponent } from 'vue'
import { useHotKey } from '@nextcloud/vue/dist/Composables/useHotKey.js'

import AccountPlusIcon from 'vue-material-design-icons/AccountPlus.vue'
import IconAlertCircleOutline from 'vue-material-design-icons/AlertCircleOutline.vue'
Expand Down

0 comments on commit 54caf2d

Please sign in to comment.