Skip to content

Commit 531c917

Browse files
author
IM.codes
committed
perf(web): simplify pinned file preview flow
1 parent a269c7f commit 531c917

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

web/src/app.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2279,7 +2279,7 @@ export function App() {
22792279
serverId: selectedServerId ?? '',
22802280
subSessions,
22812281
inputRefsMap,
2282-
onPreviewFile: (request) => handlePreviewFileRequest({ ...request, sourcePreviewLive: true }),
2282+
onPreviewFile: (request) => handlePreviewFileRequest({ ...request, sourcePreviewLive: false }),
22832283
onPreviewStateChange: handlePreviewStateChange,
22842284
activeSession,
22852285
activeProjectDir: activeSessionInfo?.projectDir,

web/src/components/FileBrowser.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,10 @@ export function FileBrowser({
675675
const loadingPreview: FileBrowserPreviewState = { status: 'loading', path: filePath };
676676
setPreview(loadingPreview);
677677
setShowDiff(preferDiff);
678-
if (onPreviewFile) onPreviewFile({ path: filePath, preferDiff, preview: loadingPreview });
678+
if (onPreviewFile) {
679+
onPreviewFile({ path: filePath, preferDiff, preview: loadingPreview });
680+
return;
681+
}
679682
const requestId = ws.fsReadFile(filePath);
680683
pendingReadRef.current.set(requestId, filePath);
681684
const diffId = ws.fsGitDiff(filePath);
@@ -801,7 +804,8 @@ export function FileBrowser({
801804
if (currentPreviewPath === autoPreviewPath && preview.status !== 'idle') {
802805
setShowDiff(autoPreviewPreferDiff);
803806
if (preview.status === 'loading' && initialPreview?.status === 'loading' && !skipAutoPreviewIfLoading) {
804-
fetchPreview(autoPreviewPath, autoPreviewPreferDiff);
807+
const hasPendingRead = [...pendingReadRef.current.values()].includes(autoPreviewPath);
808+
if (!hasPendingRead) fetchPreview(autoPreviewPath, autoPreviewPreferDiff);
805809
}
806810
return;
807811
}

web/test/components/FileBrowser.test.tsx

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -746,10 +746,9 @@ describe('FileBrowser', () => {
746746
});
747747
});
748748

749-
it('pushes loaded preview state updates for externally hosted change previews', async () => {
749+
it('does not start local preview fetches when using an external preview host', async () => {
750750
const { ws, respond, sendMsg } = makeWsFactory();
751751
const onPreviewFile = vi.fn();
752-
const onPreviewStateChange = vi.fn();
753752
render(
754753
<FileBrowser
755754
ws={ws}
@@ -758,7 +757,6 @@ describe('FileBrowser', () => {
758757
initialPath="/home/user"
759758
changesRootPath="/home/user"
760759
onPreviewFile={onPreviewFile}
761-
onPreviewStateChange={onPreviewStateChange}
762760
onConfirm={vi.fn()}
763761
/>,
764762
);
@@ -781,28 +779,13 @@ describe('FileBrowser', () => {
781779
const changeItem = document.querySelector('.fb-changes-item') as HTMLElement;
782780
await act(async () => { fireEvent.click(changeItem); });
783781

784-
expect((ws.fsReadFile as any).mock.calls).toHaveLength(1);
785-
expect((ws.fsGitDiff as any).mock.calls).toHaveLength(1);
782+
expect((ws.fsReadFile as any).mock.calls).toHaveLength(0);
783+
expect((ws.fsGitDiff as any).mock.calls).toHaveLength(0);
786784
expect(onPreviewFile).toHaveBeenLastCalledWith({
787785
path: '/home/user/foo.ts',
788786
preferDiff: true,
789787
preview: { status: 'loading', path: '/home/user/foo.ts' },
790788
});
791-
792-
await act(async () => {
793-
sendMsg({ type: 'fs.read_response', requestId: 'mock-read-id', path: '/home/user/foo.ts', status: 'ok', content: 'const x = 1;' });
794-
sendMsg({ type: 'fs.git_diff_response', requestId: 'mock-git-diff-id', path: '/home/user/foo.ts', status: 'ok', diff: '+const x = 1;' });
795-
});
796-
797-
expect(onPreviewStateChange).toHaveBeenCalledWith(expect.objectContaining({
798-
path: '/home/user/foo.ts',
799-
preferDiff: true,
800-
preview: expect.objectContaining({
801-
status: 'ok',
802-
path: '/home/user/foo.ts',
803-
content: 'const x = 1;',
804-
}),
805-
}));
806789
});
807790

808791
it('does not render an inline preview when an external preview host is provided', async () => {
@@ -837,10 +820,6 @@ describe('FileBrowser', () => {
837820

838821
const changeItem = document.querySelector('.fb-changes-item') as HTMLElement;
839822
await act(async () => { fireEvent.click(changeItem); });
840-
await act(async () => {
841-
sendMsg({ type: 'fs.read_response', requestId: 'mock-read-id', path: '/home/user/foo.ts', status: 'ok', content: 'const x = 1;' });
842-
sendMsg({ type: 'fs.git_diff_response', requestId: 'mock-git-diff-id', path: '/home/user/foo.ts', status: 'ok', diff: '+const x = 1;' });
843-
});
844823

845824
expect(onPreviewFile).toHaveBeenCalled();
846825
expect(document.querySelector('.fb-preview')).toBeNull();
@@ -900,6 +879,27 @@ describe('FileBrowser', () => {
900879
expect((ws.fsGitDiff as any).mock.calls).toHaveLength(0);
901880
});
902881

882+
it('fetches preview data when a floating preview is hydrated with a loading state', () => {
883+
const { ws } = makeWsFactory();
884+
render(
885+
<FileBrowser
886+
ws={ws}
887+
mode="file-single"
888+
layout="panel"
889+
initialPath="/home/user"
890+
initialPreview={{ status: 'loading', path: '/home/user/foo.ts' }}
891+
autoPreviewPath="/home/user/foo.ts"
892+
autoPreviewPreferDiff
893+
onConfirm={vi.fn()}
894+
/>,
895+
);
896+
897+
expect((ws.fsReadFile as any).mock.calls).toHaveLength(1);
898+
expect((ws.fsGitDiff as any).mock.calls).toHaveLength(1);
899+
expect((ws.fsReadFile as any).mock.calls[0]?.[0]).toBe('/home/user/foo.ts');
900+
expect((ws.fsGitDiff as any).mock.calls[0]?.[0]).toBe('/home/user/foo.ts');
901+
});
902+
903903
it('does not immediately reopen an auto-preview after the preview close button is pressed', async () => {
904904
const { ws } = makeWsFactory();
905905
const onClose = vi.fn();

0 commit comments

Comments
 (0)