fix(files): root file browser at repo directory instead of display name (#339)

This commit is contained in:
Chris Scott authored and GitHub committed 2026-08-20 09:28:35 -04:00
1 parent 1d477bdee4
commit 99dbc9a9ce
5 files changed
+44 -26

No files matched your search

@@ -1,5 +1,6 @@
import { useEffect, useState, memo, useCallback, useRef } from 'react'
import { FileBrowser, type FileBrowserHandle } from './FileBrowser'
import { getRepoRelativeDisplayPath } from './display-path'
import { Button } from '@/components/ui/button'
import { PathDisplay } from '@/components/ui/path-display'
import { FullscreenSheet, FullscreenSheetHeader, FullscreenSheetContent } from '@/components/ui/fullscreen-sheet'
@@ -71,28 +72,9 @@ export const FileBrowserSheet = memo(function FileBrowserSheet({ isOpen, onClose
return
}
if (!info.currentPath || info.currentPath === '.' || info.currentPath === '') {
setDisplayPath('/')
setCurrentPath(info.currentPath || '.')
return
}
setCurrentPath(info.currentPath)
const pathParts = info.currentPath.split('/').filter(Boolean)
if (repoName) {
const repoIndex = pathParts.findIndex(p => p === repoName || p.startsWith(repoName + '-'))
if (repoIndex >= 0) {
const subPath = pathParts.slice(repoIndex + 1)
setDisplayPath(subPath.length > 0 ? '/' + subPath.join('/') : '/')
} else {
setDisplayPath('/' + pathParts.join('/'))
}
} else {
setDisplayPath('/' + pathParts.join('/'))
}
}, [allowNavigateAboveBase, repoName])
setCurrentPath(info.currentPath || '.')
setDisplayPath(getRepoRelativeDisplayPath(info.currentPath || '.', normalizedBasePath))
}, [allowNavigateAboveBase, normalizedBasePath])
const handleDownloadDirectory = useCallback(async (options: { includeGit?: boolean, includePaths?: string[] }) => {
if (!currentPath) return
@@ -0,0 +1,31 @@
import { describe, it, expect } from 'vitest'
import { getRepoRelativeDisplayPath } from './display-path'
describe('getRepoRelativeDisplayPath', () => {
it('returns root when current path equals the base path', () => {
expect(getRepoRelativeDisplayPath('my-repo', 'my-repo')).toBe('/')
})
it('returns root for the assistant directory regardless of display-name casing', () => {
expect(getRepoRelativeDisplayPath('assistant', 'assistant')).toBe('/')
})
it('strips the base path prefix for subdirectories', () => {
expect(getRepoRelativeDisplayPath('assistant/.opencode/skills', 'assistant')).toBe('/.opencode/skills')
expect(getRepoRelativeDisplayPath('my-repo/src', 'my-repo')).toBe('/src')
})
it('handles worktree directories with branch suffixes', () => {
expect(getRepoRelativeDisplayPath('my-repo-feature', 'my-repo-feature')).toBe('/')
expect(getRepoRelativeDisplayPath('my-repo-feature/src', 'my-repo-feature')).toBe('/src')
})
it('treats "." as the workspace root base', () => {
expect(getRepoRelativeDisplayPath('.', '.')).toBe('/')
expect(getRepoRelativeDisplayPath('some-repo', '.')).toBe('/some-repo')
})
it('falls back to the full path when outside the base path', () => {
expect(getRepoRelativeDisplayPath('other-repo/file', 'assistant')).toBe('/other-repo/file')
})
})
@@ -0,0 +1,7 @@
export function getRepoRelativeDisplayPath(currentPath: string, basePath: string): string {
const baseParts = basePath === '.' ? [] : basePath.split('/').filter(Boolean)
const pathParts = currentPath === '.' ? [] : currentPath.split('/').filter(Boolean)
const isWithinBase = baseParts.every((part, index) => pathParts[index] === part)
const subParts = isWithinBase ? pathParts.slice(baseParts.length) : pathParts
return subParts.length > 0 ? '/' + subParts.join('/') : '/'
}