4
4
*/
5
5
import type { FileSource , PathsStore , PathOptions , ServicesState , Service } from '../types'
6
6
import { defineStore } from 'pinia'
7
- import { FileType , Folder , Node , getNavigation } from '@nextcloud/files'
7
+ import { dirname } from '@nextcloud/paths'
8
+ import { File , FileType , Folder , Node , getNavigation } from '@nextcloud/files'
8
9
import { subscribe } from '@nextcloud/event-bus'
9
10
import Vue from 'vue'
10
11
import logger from '../logger'
@@ -50,6 +51,27 @@ export const usePathsStore = function(...args) {
50
51
Vue . delete ( this . paths [ service ] , path )
51
52
} ,
52
53
54
+ onCreatedNode ( node : Node ) {
55
+ const service = getNavigation ( ) ?. active ?. id || 'files'
56
+ if ( ! node . fileid ) {
57
+ logger . error ( 'Node has no fileid' , { node } )
58
+ return
59
+ }
60
+
61
+ // Only add path if it's a folder
62
+ if ( node . type === FileType . Folder ) {
63
+ this . addPath ( {
64
+ service,
65
+ path : node . path ,
66
+ source : node . source ,
67
+ } )
68
+ }
69
+
70
+ // Update parent folder children if exists
71
+ // If the folder is the root, get it and update it
72
+ this . addNodeToParentChildren ( node )
73
+ } ,
74
+
53
75
onDeletedNode ( node : Node ) {
54
76
const service = getNavigation ( ) ?. active ?. id || 'files'
55
77
@@ -61,95 +83,80 @@ export const usePathsStore = function(...args) {
61
83
)
62
84
}
63
85
64
- // Remove node from children
65
- if ( node . dirname === '/' ) {
66
- const root = files . getRoot ( service ) as Folder & { _children ?: string [ ] }
67
- // ensure sources are unique
68
- const children = new Set ( root . _children ?? [ ] )
69
- children . delete ( node . source )
70
- Vue . set ( root , '_children' , [ ...children . values ( ) ] )
71
- return
72
- }
73
-
74
- if ( this . paths [ service ] [ node . dirname ] ) {
75
- const parentSource = this . paths [ service ] [ node . dirname ]
76
- const parentFolder = files . getNode ( parentSource ) as Folder & { _children ?: string [ ] }
77
-
78
- if ( ! parentFolder ) {
79
- logger . error ( 'Parent folder not found' , { parentSource } )
80
- return
81
- }
82
-
83
- logger . debug ( 'Path exists, removing from children' , { parentFolder, node } )
84
-
85
- // ensure sources are unique
86
- const children = new Set ( parentFolder . _children ?? [ ] )
87
- children . delete ( node . source )
88
- Vue . set ( parentFolder , '_children' , [ ...children . values ( ) ] )
89
- return
90
- }
91
-
92
- logger . debug ( 'Parent path does not exists, skipping children update' , { node } )
86
+ this . deleteNodeFromParentChildren ( node )
93
87
} ,
94
88
95
- onCreatedNode ( node : Node ) {
89
+ onMovedNode ( { node, oldSource } : { node : Node , oldSource : string } ) {
96
90
const service = getNavigation ( ) ?. active ?. id || 'files'
97
- if ( ! node . fileid ) {
98
- logger . error ( 'Node has no fileid' , { node } )
99
- return
100
- }
101
91
102
- // Only add path if it's a folder
92
+ // Update the path of the node
103
93
if ( node . type === FileType . Folder ) {
94
+ // Delete the old path if it exists
95
+ const oldPath = Object . entries ( this . paths [ service ] ) . find ( ( [ , source ] ) => source === oldSource )
96
+ if ( oldPath ?. [ 0 ] ) {
97
+ this . deletePath ( service , oldPath [ 0 ] )
98
+ }
99
+
100
+ // Add the new path
104
101
this . addPath ( {
105
102
service,
106
103
path : node . path ,
107
104
source : node . source ,
108
105
} )
109
106
}
110
107
111
- // Update parent folder children if exists
112
- // If the folder is the root, get it and update it
113
- if ( node . dirname === '/' ) {
114
- const root = files . getRoot ( service ) as Folder & { _children ?: string [ ] }
108
+ // Dummy simple clone of the renamed node from a previous state
109
+ const oldNode = new File ( { source : oldSource , owner : node . owner , mime : node . mime } )
110
+
111
+ this . deleteNodeFromParentChildren ( oldNode )
112
+ this . addNodeToParentChildren ( node )
113
+ } ,
114
+
115
+ deleteNodeFromParentChildren ( node : Node ) {
116
+ const service = getNavigation ( ) ?. active ?. id || 'files'
117
+
118
+ // Update children of a root folder
119
+ const parentSource = dirname ( node . source )
120
+ const folder = ( node . dirname === '/' ? files . getRoot ( service ) : files . getNode ( parentSource ) ) as Folder & { _children ?: string [ ] }
121
+ if ( folder ) {
115
122
// ensure sources are unique
116
- const children = new Set ( root . _children ?? [ ] )
117
- children . add ( node . source )
118
- Vue . set ( root , '_children' , [ ...children . values ( ) ] )
123
+ const children = new Set ( folder . _children ?? [ ] )
124
+ children . delete ( node . source )
125
+ Vue . set ( folder , '_children' , [ ...children . values ( ) ] )
126
+ logger . debug ( 'Children updated' , { parent : folder , node, children : folder . _children } )
119
127
return
120
128
}
121
129
122
- // If the folder doesn't exists yet, it will be
123
- // fetched later and its children updated anyway.
124
- if ( this . paths [ service ] [ node . dirname ] ) {
125
- const parentSource = this . paths [ service ] [ node . dirname ]
126
- const parentFolder = files . getNode ( parentSource ) as Folder & { _children ?: string [ ] }
127
- logger . debug ( 'Path already exists, updating children' , { parentFolder, node } )
130
+ logger . debug ( 'Parent path does not exists, skipping children update' , { node } )
131
+ } ,
128
132
129
- if ( ! parentFolder ) {
130
- logger . error ( 'Parent folder not found' , { parentSource } )
131
- return
132
- }
133
+ addNodeToParentChildren ( node : Node ) {
134
+ const service = getNavigation ( ) ?. active ?. id || 'files'
133
135
136
+ // Update children of a root folder
137
+ const parentSource = dirname ( node . source )
138
+ const folder = ( node . dirname === '/' ? files . getRoot ( service ) : files . getNode ( parentSource ) ) as Folder & { _children ?: string [ ] }
139
+ if ( folder ) {
134
140
// ensure sources are unique
135
- const children = new Set ( parentFolder . _children ?? [ ] )
141
+ const children = new Set ( folder . _children ?? [ ] )
136
142
children . add ( node . source )
137
- Vue . set ( parentFolder , '_children' , [ ...children . values ( ) ] )
143
+ Vue . set ( folder , '_children' , [ ...children . values ( ) ] )
144
+ logger . debug ( 'Children updated' , { parent : folder , node, children : folder . _children } )
138
145
return
139
146
}
140
147
141
148
logger . debug ( 'Parent path does not exists, skipping children update' , { node } )
142
149
} ,
150
+
143
151
} ,
144
152
} )
145
153
146
154
const pathsStore = store ( ...args )
147
155
// Make sure we only register the listeners once
148
156
if ( ! pathsStore . _initialized ) {
149
- // TODO: watch folders to update paths?
150
157
subscribe ( 'files:node:created' , pathsStore . onCreatedNode )
151
158
subscribe ( 'files:node:deleted' , pathsStore . onDeletedNode )
152
- // subscribe('files:node:moved', pathsStore.onMovedNode)
159
+ subscribe ( 'files:node:moved' , pathsStore . onMovedNode )
153
160
154
161
pathsStore . _initialized = true
155
162
}
0 commit comments