diff --git a/frontend/src/components/file-browser/FileBrowserSheet.tsx b/frontend/src/components/file-browser/FileBrowserSheet.tsx index c7fbdc95..50e55b27 100644 --- a/frontend/src/components/file-browser/FileBrowserSheet.tsx +++ b/frontend/src/components/file-browser/FileBrowserSheet.tsx @@ -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 diff --git a/frontend/src/components/file-browser/display-path.test.ts b/frontend/src/components/file-browser/display-path.test.ts new file mode 100644 index 00000000..cdaf3810 --- /dev/null +++ b/frontend/src/components/file-browser/display-path.test.ts @@ -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') + }) +}) diff --git a/frontend/src/components/file-browser/display-path.ts b/frontend/src/components/file-browser/display-path.ts new file mode 100644 index 00000000..974491d9 --- /dev/null +++ b/frontend/src/components/file-browser/display-path.ts @@ -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('/') : '/' +} diff --git a/frontend/src/pages/AssistantRedirect.tsx b/frontend/src/pages/AssistantRedirect.tsx index 2b211434..c1bd0bdf 100644 --- a/frontend/src/pages/AssistantRedirect.tsx +++ b/frontend/src/pages/AssistantRedirect.tsx @@ -37,7 +37,6 @@ export function AssistantRedirect() { }) const assistantDirectory = repo?.fullPath - const assistantFileBasePath = assistantDirectory?.split('/').filter(Boolean).at(-1) useSSE(opcodeUrl, assistantDirectory) @@ -82,7 +81,7 @@ export function AssistantRedirect() { {assistantDirectory && ( <> - setFileBrowserOpen(false)} basePath={assistantFileBasePath} repoName="Assistant" repoId={repoId} /> + setFileBrowserOpen(false)} basePath={repo?.localPath} repoName="Assistant" repoId={repoId} /> {assistantDirectory && opcodeUrl ? ( { setActivePromptFileBasePath(sessionDirectory ? workspaceBasePath ?? null : null)