Skip to content

Commit 8ff2379

Browse files
committed
Fix cancel-loading to return to previous folder
- Instead of always navigating to ~ when cancelling a large directory load, return to the folder the user was in before - Compares cancelled path with history to handle two cases: listing completed (go back in history) vs listing still loading (reload current history entry via navigateToPath) - Falls back to resolveValidPath on the parent when a tab was opened directly at the cancelled path (no history)
1 parent a87c45a commit 8ff2379

2 files changed

Lines changed: 39 additions & 15 deletions

File tree

apps/desktop/src/lib/file-explorer/pane/DualPaneExplorer.svelte

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
} from '../types'
3939
import { defaultSortOrders } from '../types'
4040
import { ensureFontMetricsLoaded } from '$lib/font-metrics'
41-
import { determineNavigationPath } from '../navigation/path-navigation'
41+
import { determineNavigationPath, resolveValidPath } from '../navigation/path-navigation'
4242
import {
4343
createHistory,
4444
push,
@@ -551,8 +551,9 @@
551551
containerElement?.focus()
552552
}
553553
554-
function handleCancelLoading(pane: 'left' | 'right') {
555-
const entry = getCurrentEntry(getPaneHistory(pane))
554+
function handleCancelLoading(pane: 'left' | 'right', cancelledPath: string, selectName?: string) {
555+
const history = getPaneHistory(pane)
556+
const entry = getCurrentEntry(history)
556557
const paneRef = getPaneRef(pane)
557558
558559
if (entry.volumeId === 'network') {
@@ -561,13 +562,36 @@
561562
saveAppStatus({ [paneKey(pane, 'volumeId')]: 'network', [paneKey(pane, 'path')]: entry.path })
562563
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
563564
paneRef?.setNetworkHost?.(entry.networkHost ?? null)
564-
} else {
565-
// Immediately navigate to a known-safe local path — the user pressed ESC, they want out
566-
setPanePath(pane, '~')
567-
setPaneVolumeId(pane, DEFAULT_VOLUME_ID)
568-
saveAppStatus({ [paneKey(pane, 'path')]: '~', [paneKey(pane, 'volumeId')]: DEFAULT_VOLUME_ID })
565+
saveTabsForPaneSide(pane)
566+
containerElement?.focus()
567+
return
569568
}
570-
saveTabsForPaneSide(pane)
569+
570+
if (entry.path === cancelledPath) {
571+
// Listing completed before cancel — history has the cancelled path pushed. Go back.
572+
if (canGoBack(history)) {
573+
const newHistory = back(history)
574+
const target = getCurrentEntry(newHistory)
575+
updatePaneAfterHistoryNavigation(pane, newHistory, target.path)
576+
return
577+
}
578+
579+
// Edge case: tab opened directly at this path, no history. Walk up to nearest valid parent.
580+
const parentPath = entry.path.substring(0, Math.max(1, entry.path.lastIndexOf('/')))
581+
void resolveValidPath(parentPath).then((validPath) => {
582+
const target = validPath ?? '~'
583+
setPanePath(pane, target)
584+
saveAppStatus({ [paneKey(pane, 'path')]: target })
585+
saveTabsForPaneSide(pane)
586+
containerElement?.focus()
587+
})
588+
return
589+
}
590+
591+
// Listing didn't complete — history still points at the previous folder (correct destination).
592+
// setPanePath won't trigger FilePane's $effect (path unchanged), so call navigateToPath directly.
593+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
594+
paneRef?.navigateToPath?.(entry.path, selectName)
571595
containerElement?.focus()
572596
}
573597
@@ -1977,8 +2001,8 @@
19772001
onNetworkHostChange={(host: NetworkHost | null) => {
19782002
handleNetworkHostChange(paneId, host)
19792003
}}
1980-
onCancelLoading={() => {
1981-
handleCancelLoading(paneId)
2004+
onCancelLoading={(cancelledPath: string, selectName?: string) => {
2005+
handleCancelLoading(paneId, cancelledPath, selectName)
19822006
}}
19832007
onMtpFatalError={(msg: string) => handleMtpFatalError(paneId, msg)}
19842008
/>

apps/desktop/src/lib/file-explorer/pane/FilePane.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@
9898
onRequestFocus?: () => void
9999
/** Called when active network host changes (for history tracking) */
100100
onNetworkHostChange?: (host: NetworkHost | null) => void
101-
/** Called when user cancels loading (ESC key) - parent should reload previous folder, optionally selecting the folder we tried to enter */
102-
onCancelLoading?: (selectName?: string) => void
101+
/** Called when user cancels loading (ESC key) - parent navigates back to previous folder */
102+
onCancelLoading?: (cancelledPath: string, selectName?: string) => void
103103
/** Called when MTP connection fails fatally (device disconnected, timeout) - parent should fall back to previous volume */
104104
onMtpFatalError?: (error: string) => void
105105
}
@@ -852,8 +852,8 @@
852852
// Extract the folder name we were trying to enter, so parent can select it when reloading
853853
const folderName = currentPath.split('/').pop()
854854
855-
// Reload previous folder via callback (parent will set the path, triggering our effect)
856-
onCancelLoading?.(folderName)
855+
// Tell parent to navigate back — passes the path we were loading so parent can decide where to go
856+
onCancelLoading?.(currentPath, folderName)
857857
}
858858
859859
// Navigate to a specific path with optional item selection (used when cancelling navigation)

0 commit comments

Comments
 (0)