Skip to content

Commit

Permalink
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 63564ce commit d33525c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apps/files/src/components/FileEntry/FileEntryActions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export default defineComponent({

computed: {
isActive() {
return this.activeStore.active?.source === this.source.source
return this.activeStore.activeNode?.source === this.source.source
},

isLoading() {
Expand Down
2 changes: 1 addition & 1 deletion apps/files/src/components/FileEntry/FileEntryCheckbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default defineComponent({

computed: {
isActive() {
return this.activeStore.active?.source === this.source.source
return this.activeStore.activeNode?.source === this.source.source
},

selectedFiles() {
Expand Down
27 changes: 21 additions & 6 deletions apps/files/src/store/active.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
*/

import type { ActiveStore } from '../types.ts'
import type { Node } from '@nextcloud/files'
import type { Node, View } from '@nextcloud/files'

import { defineStore } from 'pinia'
import { getNavigation } from '@nextcloud/files'
import { subscribe } from '@nextcloud/event-bus'

import logger from '../logger.ts'

export const useActiveStore = function(...args) {
const store = defineStore('active', {
state: () => ({
active: null,
_initialized: false,
activeNode: null,
activeView: null,
} as ActiveStore),

actions: {
Expand All @@ -22,32 +26,43 @@ export const useActiveStore = function(...args) {
throw new Error('Use clearActiveNode to clear the active node')
}
logger.debug('Setting active node', { node })
this.active = node
this.activeNode = node
},

/**
* Clear the active node
*/
clearActiveNode() {
this.active = null
this.activeNode = null
},

onDeletedNode(node: Node) {
if (this.active && this.active.source === node.source) {
if (this.activeNode && this.activeNode.source === node.source) {
this.clearActiveNode()
}
},

onChangedView(view: View|null = null) {
logger.debug('Setting active view', { view })
this.activeView = view
this.clearActiveNode()
},
},
})

const activeStore = store(...args)
const navigation = getNavigation()

// Make sure we only register the listeners once
if (!activeStore._initialized) {
subscribe('files:node:deleted', activeStore.onDeletedNode)
subscribe('files:navigation:changed', activeStore.clearActiveNode)

activeStore._initialized = true

// Or you can react to changes of the current active view
navigation.addEventListener('updateActive', (event) => {
activeStore.onChangedView(event.detail)
})
}

return activeStore
Expand Down
6 changes: 4 additions & 2 deletions apps/files/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { Folder, Node } from '@nextcloud/files'
import type { Folder, Node, View } from '@nextcloud/files'
import type { Upload } from '@nextcloud/upload'

// Global definitions
Expand Down Expand Up @@ -97,7 +97,9 @@ export interface DragAndDropStore {

// Active node store
export interface ActiveStore {
active: Node|null
_initialized: boolean
activeNode: Node|null
activeView: View|null
}

export interface TemplateFile {
Expand Down

0 comments on commit d33525c

Please sign in to comment.