Skip to content

Commit ca0d953

Browse files
committed
Bug fix: viewing may be folder or file
1 parent c822a86 commit ca0d953

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

smoosense-gui/src/components/providers/FolderUrlParamsProvider.tsx

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { useEffect, Suspense } from 'react'
44
import { useSearchParams } from 'next/navigation'
55
import { useAppDispatch } from '@/lib/hooks'
66
import { setRootFolder, setBaseUrl } from '@/lib/features/ui/uiSlice'
7-
import { setViewingId, loadFolderContents, expandNode } from '@/lib/features/folderTree/folderTreeSlice'
8-
import { pathJoin, pathParent } from '@/lib/utils/pathUtils'
7+
import { setViewingId, loadFolderContents, expandNode, type FSItem } from '@/lib/features/folderTree/folderTreeSlice'
8+
import { pathJoin, pathParent, pathBasename } from '@/lib/utils/pathUtils'
99
import type { AppDispatch } from '@/lib/store'
1010

1111
// Recursively ensure a path and all its ancestors are expanded
@@ -19,34 +19,43 @@ async function expandTreeToViewing(
1919
const fullPath = pathJoin(urlRootFolder, viewing)
2020
dispatch(setViewingId(fullPath))
2121

22-
const ensurePathExpanded = async (path: string): Promise<void> => {
23-
22+
// Returns the loaded items for the path, or null if it's a file
23+
const ensurePathExpanded = async (path: string): Promise<FSItem[] | null> => {
2424
// Base case: if this is the root folder, load and expand it
2525
if (path === urlRootFolder) {
26-
await dispatch(loadFolderContents({ path: urlRootFolder })).unwrap()
26+
const result = await dispatch(loadFolderContents({ path: urlRootFolder })).unwrap()
2727
dispatch(expandNode(urlRootFolder))
28-
return
29-
} else {
30-
const parent = pathParent(path)
31-
32-
// If no valid parent, load and expand root
33-
if (!parent || parent === path || parent === '') {
34-
await dispatch(loadFolderContents({ path: urlRootFolder })).unwrap()
35-
dispatch(expandNode(urlRootFolder))
36-
return
37-
}
28+
return result.items
29+
}
30+
31+
const parent = pathParent(path)
32+
33+
// If no valid parent, load and expand root
34+
if (!parent || parent === path || parent === '') {
35+
const result = await dispatch(loadFolderContents({ path: urlRootFolder })).unwrap()
36+
dispatch(expandNode(urlRootFolder))
37+
return result.items
38+
}
39+
40+
// Recursive case: ensure parent is expanded first
41+
const parentItems = await ensurePathExpanded(parent)
3842

39-
// Recursive case: ensure parent is expanded first
40-
await ensurePathExpanded(parent)
41-
// Load this path's contents
42-
await dispatch(loadFolderContents({ path })).unwrap()
43-
// Expand this path (so its children can be visible)
43+
// Check if current path is a directory based on parent's loaded items
44+
const itemName = pathBasename(path)
45+
const currentItem = parentItems?.find(item => item.name === itemName)
46+
47+
if (currentItem?.isDir) {
48+
// It's a directory, load its contents and expand it
49+
const result = await dispatch(loadFolderContents({ path })).unwrap()
4450
dispatch(expandNode(path))
51+
return result.items
4552
}
53+
54+
// It's a file or not found, don't try to load
55+
return null
4656
}
4757

4858
try {
49-
// Ensure the viewing target and all its ancestors are expanded
5059
await ensurePathExpanded(fullPath)
5160
} catch (error) {
5261
console.error('Failed to expand tree to viewing path:', error)

0 commit comments

Comments
 (0)