diff --git a/packages/ui/src/features/sidebar/useTaskPrStatus.test.ts b/packages/ui/src/features/sidebar/useTaskPrStatus.test.ts index 0e16f9904a..ecaa82bb60 100644 --- a/packages/ui/src/features/sidebar/useTaskPrStatus.test.ts +++ b/packages/ui/src/features/sidebar/useTaskPrStatus.test.ts @@ -108,4 +108,18 @@ describe("useTaskPrStatus", () => { ); expect(lastQueryOptions?.enabled).toBe(true); }); + + it("ignores leftover placeholder data from a previous task when the query is disabled", () => { + // Simulates switching from a cloud task with a PR (query data populated) + // to a fresh cloud task with no PR yet — TanStack's `placeholderData` + // would otherwise keep serving the previous task's resolved data since + // the disabled query never fetches to overwrite it. + queryData = { prState: "open", hasDiff: true }; + const { result } = renderHook(() => + useTaskPrStatus( + makeTask({ taskRunEnvironment: "cloud", cloudPrUrl: null }), + ), + ); + expect(result.current).toEqual({ prState: null, hasDiff: false }); + }); }); diff --git a/packages/ui/src/features/sidebar/useTaskPrStatus.ts b/packages/ui/src/features/sidebar/useTaskPrStatus.ts index d0db43aa62..e04e749179 100644 --- a/packages/ui/src/features/sidebar/useTaskPrStatus.ts +++ b/packages/ui/src/features/sidebar/useTaskPrStatus.ts @@ -35,6 +35,11 @@ export function useTaskPrStatus(task: { ), ); - if (!data || (!data.prState && !data.hasDiff)) return EMPTY; + // When the query is disabled, `data` can still be populated: + // `placeholderData: (prev) => prev` carries over whatever the previous + // task's query resolved to, and a disabled query never fetches to replace + // it. Without this guard, switching from a task with a PR to one without + // (e.g. a fresh cloud task) would keep showing the old task's PR status. + if (skipQuery || !data || (!data.prState && !data.hasDiff)) return EMPTY; return data; }