diff --git a/frontend/src/components/file-browser/FileBrowser.tsx b/frontend/src/components/file-browser/FileBrowser.tsx index 61eecefa..a27ec8a1 100644 --- a/frontend/src/components/file-browser/FileBrowser.tsx +++ b/frontend/src/components/file-browser/FileBrowser.tsx @@ -451,11 +451,13 @@ useEffect(() => { return } setSelectedFile(initialFileData) - if (isMobile) { - setIsPreviewModalOpen(true) - onPreviewStateChange?.(true) - } - }, [initialFileData, isMobile, onPreviewStateChange, loadFiles]) + }, [initialFileData, loadFiles]) + + useEffect(() => { + if (!initialFileData || initialFileData.isDirectory || !isMobile) return + setIsPreviewModalOpen(true) + onPreviewStateChange?.(true) + }, [initialFileData, isMobile, onPreviewStateChange]) useEffect(() => { const handleFileSaved = (event: CustomEvent<{ path: string; content?: string }>) => { diff --git a/frontend/src/components/file-browser/FileBrowserSheet.test.tsx b/frontend/src/components/file-browser/FileBrowserSheet.test.tsx index 5de2d78d..ac97b36d 100644 --- a/frontend/src/components/file-browser/FileBrowserSheet.test.tsx +++ b/frontend/src/components/file-browser/FileBrowserSheet.test.tsx @@ -121,6 +121,51 @@ describe('FileBrowser initial selection', () => { vi.unstubAllGlobals() } }) + + it('does not reload the initial directory when the viewport changes', async () => { + const directory = (path: string, children: string[]) => ({ + name: path.split('/').pop(), + path, + isDirectory: true, + size: 0, + lastModified: new Date().toISOString(), + children: children.map((name) => ({ name, path: `${path}/${name}`, isDirectory: true, size: 0, lastModified: new Date().toISOString() })), + }) + const fetchMock = vi.fn(async (input: RequestInfo | URL) => { + const path = new URL(String(input), 'http://localhost').searchParams.get('path') + if (path === 'repo') { + await new Promise((resolve) => setTimeout(resolve, 50)) + return new Response(JSON.stringify(directory('repo', ['frontend'])), { status: 200, headers: { 'Content-Type': 'application/json' } }) + } + return new Response(JSON.stringify(directory('repo/frontend', ['src'])), { status: 200, headers: { 'Content-Type': 'application/json' } }) + }) + vi.stubGlobal('fetch', fetchMock) + const mobileSpy = vi.spyOn(useMobile, 'useMobile').mockReturnValue(false) + const ref = { current: null as unknown as FileBrowserHandle } + + try { + const { rerender } = render( + , + { wrapper: createWrapper() } + ) + + expect(await screen.findByText('src')).toBeInTheDocument() + await new Promise((resolve) => setTimeout(resolve, 100)) + const callsBefore = fetchMock.mock.calls.length + + mobileSpy.mockReturnValue(true) + rerender( + + ) + + await new Promise((resolve) => setTimeout(resolve, 100)) + expect(fetchMock.mock.calls.length).toBe(callsBefore) + expect(ref.current.getCurrentPath()).toBe('repo/frontend') + } finally { + mobileSpy.mockRestore() + vi.unstubAllGlobals() + } + }) }) describe('FileBrowser navigation', () => { diff --git a/frontend/src/components/message/FileToolRender.tsx b/frontend/src/components/message/FileToolRender.tsx index d87d1e24..7f3a1044 100644 --- a/frontend/src/components/message/FileToolRender.tsx +++ b/frontend/src/components/message/FileToolRender.tsx @@ -141,8 +141,10 @@ function fileDiffs(part: SessionMessageAssistantTool): FileDiffInfo[] { /** Returns the file path a file tool was called with, accepting both the `path` and legacy `filePath` input keys. */ export function getToolInputPath(input: Record | undefined): string | undefined { - const value = input?.path ?? input?.filePath - return typeof value === 'string' && value ? value : undefined + const path = input?.path + if (typeof path === 'string' && path) return path + const filePath = input?.filePath + return typeof filePath === 'string' && filePath ? filePath : undefined } export function getToolSpecificRender(part: SessionMessageAssistantTool, onFileClick?: (filePath: string) => void): React.ReactElement | null { diff --git a/frontend/src/components/message/MessagePart.test.tsx b/frontend/src/components/message/MessagePart.test.tsx index efe6258d..a7f973f3 100644 --- a/frontend/src/components/message/MessagePart.test.tsx +++ b/frontend/src/components/message/MessagePart.test.tsx @@ -406,6 +406,25 @@ describe('MessagePart', () => { expect(screen.getByText('+1')).toBeInTheDocument() }) + it('falls back to filePath when path is empty', () => { + const onFileClick = vi.fn() + renderWithProviders( + , + ) + + fireEvent.click(screen.getByText('/test/fallback.html')) + + expect(onFileClick).toHaveBeenCalledWith('/test/fallback.html') + }) + it('renders every file of a multi-file patch with per-file links', () => { const onFileClick = vi.fn() renderWithProviders(