44 */
55import type { FileSource , PathOptions , ServicesState , Service } from '../types'
66import { defineStore } from 'pinia'
7- import { FileType , Folder , Node , getNavigation } from '@nextcloud/files'
7+ import { dirname } from '@nextcloud/paths'
8+ import { type Node , File , FileType , Folder , getNavigation } from '@nextcloud/files'
89import { subscribe } from '@nextcloud/event-bus'
910import Vue from 'vue'
1011import logger from '../logger'
@@ -51,6 +52,27 @@ export const usePathsStore = function(...args) {
5152 Vue . delete ( this . paths [ service ] , path )
5253 } ,
5354
55+ onCreatedNode ( node : Node ) {
56+ const service = getNavigation ( ) ?. active ?. id || 'files'
57+ if ( ! node . fileid ) {
58+ logger . error ( 'Node has no fileid' , { node } )
59+ return
60+ }
61+
62+ // Only add path if it's a folder
63+ if ( node . type === FileType . Folder ) {
64+ this . addPath ( {
65+ service,
66+ path : node . path ,
67+ source : node . source ,
68+ } )
69+ }
70+
71+ // Update parent folder children if exists
72+ // If the folder is the root, get it and update it
73+ this . addNodeToParentChildren ( node )
74+ } ,
75+
5476 onDeletedNode ( node : Node ) {
5577 const service = getNavigation ( ) ?. active ?. id || 'files'
5678
@@ -62,95 +84,80 @@ export const usePathsStore = function(...args) {
6284 )
6385 }
6486
65- // Remove node from children
66- if ( node . dirname === '/' ) {
67- const root = files . getRoot ( service ) as Folder & { _children ?: string [ ] }
68- // ensure sources are unique
69- const children = new Set ( root . _children ?? [ ] )
70- children . delete ( node . source )
71- Vue . set ( root , '_children' , [ ...children . values ( ) ] )
72- return
73- }
74-
75- if ( this . paths [ service ] [ node . dirname ] ) {
76- const parentSource = this . paths [ service ] [ node . dirname ]
77- const parentFolder = files . getNode ( parentSource ) as Folder & { _children ?: string [ ] }
78-
79- if ( ! parentFolder ) {
80- logger . error ( 'Parent folder not found' , { parentSource } )
81- return
82- }
83-
84- logger . debug ( 'Path exists, removing from children' , { parentFolder, node } )
85-
86- // ensure sources are unique
87- const children = new Set ( parentFolder . _children ?? [ ] )
88- children . delete ( node . source )
89- Vue . set ( parentFolder , '_children' , [ ...children . values ( ) ] )
90- return
91- }
92-
93- logger . debug ( 'Parent path does not exists, skipping children update' , { node } )
87+ this . deleteNodeFromParentChildren ( node )
9488 } ,
9589
96- onCreatedNode ( node : Node ) {
90+ onMovedNode ( { node, oldSource } : { node : Node , oldSource : string } ) {
9791 const service = getNavigation ( ) ?. active ?. id || 'files'
98- if ( ! node . fileid ) {
99- logger . error ( 'Node has no fileid' , { node } )
100- return
101- }
10292
103- // Only add path if it's a folder
93+ // Update the path of the node
10494 if ( node . type === FileType . Folder ) {
95+ // Delete the old path if it exists
96+ const oldPath = Object . entries ( this . paths [ service ] ) . find ( ( [ , source ] ) => source === oldSource )
97+ if ( oldPath ?. [ 0 ] ) {
98+ this . deletePath ( service , oldPath [ 0 ] )
99+ }
100+
101+ // Add the new path
105102 this . addPath ( {
106103 service,
107104 path : node . path ,
108105 source : node . source ,
109106 } )
110107 }
111108
112- // Update parent folder children if exists
113- // If the folder is the root, get it and update it
114- if ( node . dirname === '/' ) {
115- const root = files . getRoot ( service ) as Folder & { _children ?: string [ ] }
109+ // Dummy simple clone of the renamed node from a previous state
110+ const oldNode = new File ( { source : oldSource , owner : node . owner , mime : node . mime } )
111+
112+ this . deleteNodeFromParentChildren ( oldNode )
113+ this . addNodeToParentChildren ( node )
114+ } ,
115+
116+ deleteNodeFromParentChildren ( node : Node ) {
117+ const service = getNavigation ( ) ?. active ?. id || 'files'
118+
119+ // Update children of a root folder
120+ const parentSource = dirname ( node . source )
121+ const folder = ( node . dirname === '/' ? files . getRoot ( service ) : files . getNode ( parentSource ) ) as Folder & { _children ?: string [ ] }
122+ if ( folder ) {
116123 // ensure sources are unique
117- const children = new Set ( root . _children ?? [ ] )
118- children . add ( node . source )
119- Vue . set ( root , '_children' , [ ...children . values ( ) ] )
124+ const children = new Set ( folder . _children ?? [ ] )
125+ children . delete ( node . source )
126+ Vue . set ( folder , '_children' , [ ...children . values ( ) ] )
127+ logger . debug ( 'Children updated' , { parent : folder , node, children : folder . _children } )
120128 return
121129 }
122130
123- // If the folder doesn't exists yet, it will be
124- // fetched later and its children updated anyway.
125- if ( this . paths [ service ] [ node . dirname ] ) {
126- const parentSource = this . paths [ service ] [ node . dirname ]
127- const parentFolder = files . getNode ( parentSource ) as Folder & { _children ?: string [ ] }
128- logger . debug ( 'Path already exists, updating children' , { parentFolder, node } )
131+ logger . debug ( 'Parent path does not exists, skipping children update' , { node } )
132+ } ,
129133
130- if ( ! parentFolder ) {
131- logger . error ( 'Parent folder not found' , { parentSource } )
132- return
133- }
134+ addNodeToParentChildren ( node : Node ) {
135+ const service = getNavigation ( ) ?. active ?. id || 'files'
134136
137+ // Update children of a root folder
138+ const parentSource = dirname ( node . source )
139+ const folder = ( node . dirname === '/' ? files . getRoot ( service ) : files . getNode ( parentSource ) ) as Folder & { _children ?: string [ ] }
140+ if ( folder ) {
135141 // ensure sources are unique
136- const children = new Set ( parentFolder . _children ?? [ ] )
142+ const children = new Set ( folder . _children ?? [ ] )
137143 children . add ( node . source )
138- Vue . set ( parentFolder , '_children' , [ ...children . values ( ) ] )
144+ Vue . set ( folder , '_children' , [ ...children . values ( ) ] )
145+ logger . debug ( 'Children updated' , { parent : folder , node, children : folder . _children } )
139146 return
140147 }
141148
142149 logger . debug ( 'Parent path does not exists, skipping children update' , { node } )
143150 } ,
151+
144152 } ,
145153 } )
146154
147155 const pathsStore = store ( ...args )
148156 // Make sure we only register the listeners once
149157 if ( ! pathsStore . _initialized ) {
150- // TODO: watch folders to update paths?
151158 subscribe ( 'files:node:created' , pathsStore . onCreatedNode )
152159 subscribe ( 'files:node:deleted' , pathsStore . onDeletedNode )
153- // subscribe('files:node:moved', pathsStore.onMovedNode)
160+ subscribe ( 'files:node:moved' , pathsStore . onMovedNode )
154161
155162 pathsStore . _initialized = true
156163 }
0 commit comments