44 */
55import type { FileSource , PathsStore , PathOptions , ServicesState , Service } from '../types'
66import { defineStore } from 'pinia'
7- import { FileType , Folder , Node , getNavigation } from '@nextcloud/files'
7+ import { File , FileType , Folder , Node , getNavigation } from '@nextcloud/files'
88import { subscribe } from '@nextcloud/event-bus'
99import Vue from 'vue'
1010import logger from '../logger'
1111
1212import { useFilesStore } from './files'
13+ import { basename , dirname } from '@nextcloud/paths'
14+ import { F } from 'lodash/fp'
1315
1416export const usePathsStore = function ( ...args ) {
1517 const files = useFilesStore ( ...args )
@@ -50,6 +52,27 @@ export const usePathsStore = function(...args) {
5052 Vue . delete ( this . paths [ service ] , path )
5153 } ,
5254
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+
5376 onDeletedNode ( node : Node ) {
5477 const service = getNavigation ( ) ?. active ?. id || 'files'
5578
@@ -61,95 +84,80 @@ export const usePathsStore = function(...args) {
6184 )
6285 }
6386
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 } )
87+ this . deleteNodeFromParentChildren ( node )
9388 } ,
9489
95- onCreatedNode ( node : Node ) {
90+ onMovedNode ( { node, oldSource } : { node : Node , oldSource : string } ) {
9691 const service = getNavigation ( ) ?. active ?. id || 'files'
97- if ( ! node . fileid ) {
98- logger . error ( 'Node has no fileid' , { node } )
99- return
100- }
10192
102- // Only add path if it's a folder
93+ // Update the path of the node
10394 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
104102 this . addPath ( {
105103 service,
106104 path : node . path ,
107105 source : node . source ,
108106 } )
109107 }
110108
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 [ ] }
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 ) {
115123 // ensure sources are unique
116- const children = new Set ( root . _children ?? [ ] )
117- children . add ( node . source )
118- 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 } )
119128 return
120129 }
121130
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 } )
131+ logger . debug ( 'Parent path does not exists, skipping children update' , { node } )
132+ } ,
128133
129- if ( ! parentFolder ) {
130- logger . error ( 'Parent folder not found' , { parentSource } )
131- return
132- }
134+ addNodeToParentChildren ( node : Node ) {
135+ const service = getNavigation ( ) ?. active ?. id || 'files'
133136
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 ) {
134141 // ensure sources are unique
135- const children = new Set ( parentFolder . _children ?? [ ] )
142+ const children = new Set ( folder . _children ?? [ ] )
136143 children . add ( node . source )
137- Vue . set ( parentFolder , '_children' , [ ...children . values ( ) ] )
144+ Vue . set ( folder , '_children' , [ ...children . values ( ) ] )
145+ logger . debug ( 'Children updated' , { parent : folder , node, children : folder . _children } )
138146 return
139147 }
140148
141149 logger . debug ( 'Parent path does not exists, skipping children update' , { node } )
142150 } ,
151+
143152 } ,
144153 } )
145154
146155 const pathsStore = store ( ...args )
147156 // Make sure we only register the listeners once
148157 if ( ! pathsStore . _initialized ) {
149- // TODO: watch folders to update paths?
150158 subscribe ( 'files:node:created' , pathsStore . onCreatedNode )
151159 subscribe ( 'files:node:deleted' , pathsStore . onDeletedNode )
152- // subscribe('files:node:moved', pathsStore.onMovedNode)
160+ subscribe ( 'files:node:moved' , pathsStore . onMovedNode )
153161
154162 pathsStore . _initialized = true
155163 }
0 commit comments