diff --git a/apps/web/src/browser/openFileInPreview.test.ts b/apps/web/src/browser/openFileInPreview.test.ts new file mode 100644 index 0000000000..ec86cff252 --- /dev/null +++ b/apps/web/src/browser/openFileInPreview.test.ts @@ -0,0 +1,62 @@ +import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime"; +import type { PreviewSessionSnapshot, ScopedThreadRef } from "@t3tools/contracts"; +import { AsyncResult } from "effect/unstable/reactivity"; +import { beforeEach, expect, it } from "vite-plus/test"; + +import { readThreadPreviewState, resetPreviewStateForTests } from "~/previewStateStore"; +import { selectThreadRightPanelState, useRightPanelStore } from "~/rightPanelStore"; + +import { type OpenPreviewMutation, openUrlInPreview } from "./openFileInPreview"; + +const threadRef = { + environmentId: "local" as ScopedThreadRef["environmentId"], + threadId: "thread-1" as ScopedThreadRef["threadId"], +}; + +const snapshot = (tabId: string, url: string): PreviewSessionSnapshot => ({ + threadId: threadRef.threadId, + tabId, + navStatus: { _tag: "Success", url, title: "" }, + canGoBack: false, + canGoForward: false, + updatedAt: "2026-06-21T00:00:00.000Z", +}); + +beforeEach(() => { + resetPreviewStateForTests(); + useRightPanelStore.setState({ byThreadKey: {} }); +}); + +it("does not apply an older preview response after a newer request", async () => { + const firstController = new AbortController(); + const firstSnapshot = snapshot("tab-1", "https://assets.test/first.png"); + const secondSnapshot = snapshot("tab-2", "https://assets.test/second.png"); + let resolveFirst!: (result: AtomCommandResult) => void; + const openPreview: OpenPreviewMutation = ({ input }) => + input.url === "https://assets.test/first.png" + ? new Promise>((resolve) => { + resolveFirst = resolve; + }) + : Promise.resolve(AsyncResult.success(secondSnapshot)); + + const firstRequest = openUrlInPreview({ + threadRef, + url: "https://assets.test/first.png", + openPreview, + signal: firstController.signal, + }); + firstController.abort(); + + await openUrlInPreview({ + threadRef, + url: "https://assets.test/second.png", + openPreview, + }); + resolveFirst(AsyncResult.success(firstSnapshot)); + await firstRequest; + + expect(readThreadPreviewState(threadRef).snapshot).toEqual(secondSnapshot); + expect( + selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, threadRef).surfaces, + ).toEqual([{ id: "browser:tab-2", kind: "preview", resourceId: "tab-2" }]); +}); diff --git a/apps/web/src/browser/openFileInPreview.ts b/apps/web/src/browser/openFileInPreview.ts index b4bbefb132..d07be0f528 100644 --- a/apps/web/src/browser/openFileInPreview.ts +++ b/apps/web/src/browser/openFileInPreview.ts @@ -46,12 +46,14 @@ export async function openUrlInPreview(input: { readonly threadRef: ScopedThreadRef; readonly url: string; readonly openPreview: OpenPreviewMutation; + readonly signal?: AbortSignal; }): Promise> { const result = await input.openPreview({ environmentId: input.threadRef.environmentId, input: { threadId: input.threadRef.threadId, url: input.url }, }); return mapAtomCommandResult(result, (snapshot) => { + if (input.signal?.aborted) return; applyPreviewServerSnapshot(input.threadRef, snapshot); rememberPreviewUrl(input.threadRef, input.url); useRightPanelStore.getState().openBrowser(input.threadRef, snapshot.tabId); @@ -67,6 +69,7 @@ export async function openFileInPreview(input: { readonly input: { readonly resource: AssetResource }; }) => Promise>; readonly openPreview: OpenPreviewMutation; + readonly signal?: AbortSignal; }): Promise> { if (!isPreviewSupportedInRuntime()) { return AsyncResult.failure( @@ -87,6 +90,9 @@ export async function openFileInPreview(input: { }, }, }); + if (input.signal?.aborted) { + return AsyncResult.success(undefined); + } if (assetResult._tag === "Failure") { return AsyncResult.failure(assetResult.cause); } @@ -100,5 +106,6 @@ export async function openFileInPreview(input: { threadRef: input.threadRef, url: assetUrl, openPreview: input.openPreview, + ...(input.signal ? { signal: input.signal } : {}), }); } diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 126a76be1f..1c2cbe200d 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -1023,6 +1023,7 @@ function ChatViewContent(props: ChatViewProps) { const createAssetUrl = useAtomQueryRunner(assetEnvironment.createUrl, { reportFailure: false, }); + const filePreviewAbortControllerRef = useRef(null); const closePreview = useAtomCommand(previewEnvironment.close, "preview close"); const { environments } = useEnvironments(); const primaryEnvironment = usePrimaryEnvironment(); @@ -2751,9 +2752,16 @@ function ChatViewContent(props: ChatViewProps) { if (!activeThreadRef || !activeProject) return; useRightPanelStore.getState().open(activeThreadRef, "files"); }, [activeProject, activeThreadRef]); + useEffect( + () => () => { + filePreviewAbortControllerRef.current?.abort(); + }, + [activeThreadKey], + ); const openFileSurface = useCallback( (relativePath: string) => { if (!activeThreadRef || !activeProject) return; + filePreviewAbortControllerRef.current?.abort(); const openFallback = () => { useRightPanelStore.getState().openFile(activeThreadRef, relativePath); }; @@ -2767,6 +2775,8 @@ function ChatViewContent(props: ChatViewProps) { return; } + const abortController = new AbortController(); + filePreviewAbortControllerRef.current = abortController; const absolutePath = resolvePathLinkTarget(relativePath, activeWorkspaceRoot); void (async () => { const result = await openFileInPreview({ @@ -2775,7 +2785,11 @@ function ChatViewContent(props: ChatViewProps) { httpBaseUrl: activeEnvironmentHttpBaseUrl, createAssetUrl, openPreview, + signal: abortController.signal, }); + if (abortController.signal.aborted) { + return; + } if (result._tag === "Success") { return; } diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsx index bd559e2aa7..d38aec9a61 100644 --- a/apps/web/src/components/DiffPanel.tsx +++ b/apps/web/src/components/DiffPanel.tsx @@ -227,6 +227,7 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff reportFailure: false, }); const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false }); + const filePreviewAbortControllerRef = useRef(null); const serverConfig = useAtomValue( serverEnvironment.configValueAtom(activeThread?.environmentId ?? null), ); @@ -457,8 +458,16 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff codeViewRef.current?.scrollTo({ type: "item", id: file.fileKey, align: "start" }); }, [codeViewFiles, selectedFilePath, selectedFileRevealRequestId]); + useEffect( + () => () => { + filePreviewAbortControllerRef.current?.abort(); + }, + [routeThreadRef?.environmentId, routeThreadRef?.threadId], + ); + const openDiffFile = useCallback( (filePath: string) => { + filePreviewAbortControllerRef.current?.abort(); const openFallback = () => { openDiffFilePrimaryAction({ threadRef: routeThreadRef, @@ -495,6 +504,8 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff return; } + const abortController = new AbortController(); + filePreviewAbortControllerRef.current = abortController; void (async () => { const result = await openFileInPreview({ threadRef: routeThreadRef, @@ -502,7 +513,11 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff httpBaseUrl: environmentHttpBaseUrl, createAssetUrl, openPreview, + signal: abortController.signal, }); + if (abortController.signal.aborted) { + return; + } if (result._tag === "Success") { return; }