Skip to content

Commit 4c8e3e8

Browse files
Bil0000claude
andcommitted
fix(web): a removed row's prefetch reads nothing and its pending timer ends
The render that sees a row gone no longer hands its query to the atom, and a rest timer still pending for a removed row is cancelled, so a row that comes back before the timer fires is not selected without new intent. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6a2739c commit 4c8e3e8

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

‎apps/web/src/components/pullRequest/usePullRequestIntentPrefetch.browser.test.tsx‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,17 @@ it("reads the panel's detail only while the pointer rests on a listed row", asyn
8484
await act(() => rest.click());
8585
await act(() => vi.advanceTimersByTime(150));
8686
expect(state.reads.at(-1)).not.toBeNull();
87-
// A row removed from the list fires no leave event.
87+
// A row removed from the list fires no leave event, and coming back is not new intent.
88+
let removedAt = state.reads.length;
8889
await screen.rerender(<Rows listed={false} />);
89-
expect(state.reads.at(-1)).toBeNull();
9090
await screen.rerender(<Rows listed />);
91-
expect(state.reads.at(-1)).toBeNull();
91+
expect(state.reads.slice(removedAt)).not.toContainEqual(expect.anything());
92+
93+
await act(() => rest.click());
94+
await act(() => vi.advanceTimersByTime(100));
95+
removedAt = state.reads.length;
96+
await screen.rerender(<Rows listed={false} />);
97+
await screen.rerender(<Rows listed />);
98+
await act(() => vi.advanceTimersByTime(1_000));
99+
expect(state.reads.slice(removedAt)).not.toContainEqual(expect.anything());
92100
});

‎apps/web/src/components/pullRequest/usePullRequestIntentPrefetch.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { EnvironmentId, ProjectId } from "@t3tools/contracts";
2-
import { useRef, useState } from "react";
2+
import { useEffect, useRef, useState } from "react";
33

44
import { useStableCallback } from "~/hooks/useStableCallback";
55
import { pullRequestEnvironment } from "~/state/pullRequests";
@@ -25,10 +25,18 @@ export function usePullRequestIntentPrefetch(
2525
) {
2626
const [target, setTarget] = useState<PullRequestIntentTarget | null>(null);
2727
const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
28+
const pending = useRef<PullRequestIntentTarget | null>(null);
2829
// A removed row fires no leave event, and coming back is not new intent.
29-
if (target !== null && !isListed(target)) setTarget(null);
30+
const listed = target !== null && isListed(target);
31+
if (target !== null && !listed) setTarget(null);
32+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- a removed row fires no leave event, so its pending intent timer is cancelled once the list commits without it
33+
useEffect(() => {
34+
if (pending.current === null || isListed(pending.current)) return;
35+
clearTimeout(timer.current);
36+
pending.current = null;
37+
});
3038
useEnvironmentQuery(
31-
target === null
39+
target === null || !listed
3240
? null
3341
: pullRequestEnvironment.detail({
3442
environmentId: target.environmentId,
@@ -43,10 +51,14 @@ export function usePullRequestIntentPrefetch(
4351
);
4452
return useStableCallback((next: PullRequestIntentTarget | null) => {
4553
clearTimeout(timer.current);
54+
pending.current = next;
4655
if (next === null) {
4756
setTarget(null);
4857
return;
4958
}
50-
timer.current = setTimeout(() => setTarget(next), INTENT_DELAY_MS);
59+
timer.current = setTimeout(() => {
60+
pending.current = null;
61+
setTarget(next);
62+
}, INTENT_DELAY_MS);
5163
});
5264
}

0 commit comments

Comments
 (0)