@@ -260,21 +260,21 @@ export function DirectoryItemSelector({
260260 } , [ convertRoots , types , snackError ] ) ;
261261
262262 const fetchDirectoryChildren = useCallback (
263- ( nodeId : UUID ) : void => {
263+ ( nodeId : UUID ) : Promise < void > => {
264264 const typeList = types . includes ( ElementType . DIRECTORY ) ? [ ] : types ;
265- fetchDirectoryContent ( nodeId , typeList )
265+ return fetchDirectoryContent ( nodeId , typeList )
266266 . then ( ( children ) => {
267267 const childrenMatchedTypes = children . filter ( ( item : ElementAttributes ) =>
268268 contentFilter ( ) . has ( item . type )
269269 ) ;
270270
271271 if ( childrenMatchedTypes . length > 0 && equipmentTypes && equipmentTypes . length > 0 ) {
272- fetchElementsInfos (
272+ return fetchElementsInfos (
273273 childrenMatchedTypes . map ( ( e : ElementAttributes ) => e . elementUuid ) ,
274274 types ,
275275 equipmentTypes
276276 ) . then ( ( childrenWithMetadata : ElementAttributes [ ] ) => {
277- const filtredChildren = itemFilter
277+ const filteredChildren = itemFilter
278278 ? childrenWithMetadata . filter ( ( val : ElementAttributes ) => {
279279 // Accept every directory
280280 if ( val . type === ElementType . DIRECTORY ) {
@@ -285,12 +285,12 @@ export function DirectoryItemSelector({
285285 } )
286286 : childrenWithMetadata ;
287287 // update directory content
288- addToDirectory ( nodeId , filtredChildren ) ;
288+ addToDirectory ( nodeId , filteredChildren ) ;
289289 } ) ;
290- } else {
291- // update directory content
292- addToDirectory ( nodeId , childrenMatchedTypes ) ;
293290 }
291+ // update directory content
292+ addToDirectory ( nodeId , childrenMatchedTypes ) ;
293+ return Promise . resolve ( ) ;
294294 } )
295295 . catch ( ( error ) => {
296296 console . warn ( `Could not update subs (and content) of '${ nodeId } ' : ${ error . message } ` ) ;
@@ -301,13 +301,11 @@ export function DirectoryItemSelector({
301301
302302 // Helper function to fetch children for a node if not already loaded
303303 const fetchNodeChildrenIfNeeded = useCallback (
304- ( nodeId : UUID , delay : number = 0 ) => {
305- setTimeout ( ( ) => {
306- const node = nodeMap . current [ nodeId ] ;
307- if ( node && ( ! node . children || node . children . length === 0 ) && node . type === ElementType . DIRECTORY ) {
308- fetchDirectoryChildren ( nodeId ) ;
309- }
310- } , delay ) ;
304+ async ( nodeId : UUID ) : Promise < void > => {
305+ const node = nodeMap . current [ nodeId ] ;
306+ if ( node && ( ! node . children || node . children . length === 0 ) && node . type === ElementType . DIRECTORY ) {
307+ await fetchDirectoryChildren ( nodeId ) ;
308+ }
311309 } ,
312310 [ fetchDirectoryChildren ]
313311 ) ;
@@ -320,18 +318,18 @@ export function DirectoryItemSelector({
320318
321319 const expandedArray = await getExpansionPathsForSelected ( selected , expanded ) ;
322320 setAutoExpandedNodes ( expandedArray ) ;
323- fetchChildrenForExpandedNodes ( expandedArray , fetchNodeChildrenIfNeeded ) ;
321+ await fetchChildrenForExpandedNodes ( expandedArray , fetchNodeChildrenIfNeeded ) ;
324322 return true ;
325323 } , [ selected , expanded , fetchNodeChildrenIfNeeded ] ) ;
326324
327325 // Handle expansion from provided expanded prop
328- const handleProvidedExpansion = useCallback ( ( ) : boolean => {
326+ const handleProvidedExpansion = useCallback ( async ( ) : Promise < boolean > => {
329327 if ( ! expanded || expanded . length === 0 ) {
330328 return false ;
331329 }
332330
333331 setAutoExpandedNodes ( expanded ) ;
334- fetchChildrenForExpandedNodes ( expanded , fetchNodeChildrenIfNeeded ) ;
332+ await fetchChildrenForExpandedNodes ( expanded , fetchNodeChildrenIfNeeded ) ;
335333
336334 return true ;
337335 } , [ expanded , fetchNodeChildrenIfNeeded ] ) ;
@@ -345,7 +343,7 @@ export function DirectoryItemSelector({
345343 }
346344
347345 setAutoExpandedNodes ( expandPath ) ;
348- fetchChildrenForExpandedNodes ( expandPath , fetchNodeChildrenIfNeeded ) ;
346+ await fetchChildrenForExpandedNodes ( expandPath , fetchNodeChildrenIfNeeded ) ;
349347
350348 return true ;
351349 } , [ fetchNodeChildrenIfNeeded ] ) ;
@@ -354,11 +352,15 @@ export function DirectoryItemSelector({
354352 const initializeExpansion = useCallback ( async ( ) => {
355353 // Priority 1: Handle selected items
356354 const selectedSuccess = await handleSelectedExpansion ( ) ;
357- if ( selectedSuccess ) return ;
355+ if ( selectedSuccess ) {
356+ return ;
357+ }
358358
359359 // Priority 2: Handle provided expanded items
360- const expandedSuccess = handleProvidedExpansion ( ) ;
361- if ( expandedSuccess ) return ;
360+ const expandedSuccess = await handleProvidedExpansion ( ) ;
361+ if ( expandedSuccess ) {
362+ return ;
363+ }
362364
363365 // Priority 3: Fall back to last selected directory
364366 await handleLastSelectedExpansion ( ) ;
0 commit comments