File tree Expand file tree Collapse file tree
apps/desktop/src/lib/file-operations/mkdir Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -38,16 +38,27 @@ export async function moveCursorToNewFolder(
3838 listen : ListenFn ,
3939 findFileIndex : FindFileIndexFn ,
4040) : Promise < void > {
41+ // Try to find the folder immediately — the directory-diff event often fires
42+ // before this listener is set up (the folder is created before onCreated runs).
43+ const tryMoveCursor = async ( ) : Promise < boolean > => {
44+ const index = await findFileIndex ( paneListingId , folderName , showHiddenFiles )
45+ if ( index !== null ) {
46+ const frontendIndex = hasParent ? index + 1 : index
47+ void paneRef ?. setCursorIndex ( frontendIndex )
48+ return true
49+ }
50+ return false
51+ }
52+
53+ // First attempt: folder may already be in the listing
54+ if ( await tryMoveCursor ( ) ) return
55+
56+ // Fallback: wait for directory-diff in case the listing hasn't updated yet
4157 const unlisten = await listen ( 'directory-diff' , ( event ) => {
4258 if ( event . payload . listingId !== paneListingId ) return
43- // Small delay to ensure listing cache is fully updated before querying
4459 setTimeout ( ( ) => {
45- void findFileIndex ( paneListingId , folderName , showHiddenFiles ) . then ( ( index ) => {
46- if ( index !== null ) {
47- const frontendIndex = hasParent ? index + 1 : index
48- void paneRef ?. setCursorIndex ( frontendIndex )
49- unlisten ( )
50- }
60+ void tryMoveCursor ( ) . then ( ( found ) => {
61+ if ( found ) unlisten ( )
5162 } )
5263 } , 50 )
5364 } )
You can’t perform that action at this time.
0 commit comments