mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
fix(files): address review feedback on navigation and path fallback
Key initial directory navigation to the initial selection so viewport changes do not reset it, and open the mobile preview from a separate effect. Fall back to the legacy filePath input when path is empty, and cover both with regression tests.
This commit is contained in:
1 parent
cd7d139a31
commit
12eba5097c
4 files changed
+75
-7
No files matched your search
@@ -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 }>) => {
|
||||
|
||||
@@ -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(
|
||||
<FileBrowser ref={ref as never} basePath="repo" embedded={true} initialSelectedFile="repo/frontend" />,
|
||||
{ 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(
|
||||
<FileBrowser ref={ref as never} basePath="repo" embedded={true} initialSelectedFile="repo/frontend" />
|
||||
)
|
||||
|
||||
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', () => {
|
||||
|
||||
@@ -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<string, unknown> | 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 {
|
||||
|
||||
@@ -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(
|
||||
<MessagePart
|
||||
part={toolPart('read', {
|
||||
status: 'completed',
|
||||
input: { path: '', filePath: '/test/fallback.html' },
|
||||
content: [{ type: 'text', text: 'ok' }],
|
||||
metadata: {},
|
||||
})}
|
||||
onFileClick={onFileClick}
|
||||
/>,
|
||||
)
|
||||
|
||||
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(
|
||||
|
||||
Reference in new issue
Block a user