The best way to control Claude Code, Codex and more from any computer or phone
perf(web): snapshot coordinator keeps the last route target in state (#20466)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 18:46 UTC
1544a94578c14c17c1e6ed52c077d9b978769e76
2 files changed
+67
-37
apps/web/reactCompiledFiles.ts
+1
@@ -41,4 +41,5 @@ export const REACT_COMPILED_FILES = [
41
"apps/web/src/components/pullRequest/usePullRequestFilesViewed.ts",
42
"apps/web/src/hooks/useLiveRefresh.ts",
43
"apps/web/src/browser/useBrowserViewportResize.ts",
44
+ "apps/web/src/components/desktop/SnapShotCoordinator.tsx",
45
] as const;
apps/web/src/components/desktop/SnapShotCoordinator.tsx
+66
-37
@@ -1,9 +1,10 @@
1
import {
2
type DesktopPendingSnapShot,
3
+ type DesktopSnapShotEvent,
4
PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
5
type ScopedThreadRef,
6
} from "@t3tools/contracts";
6
-import { useCallback, useEffect, useRef } from "react";
7
+import { useEffect, useEffectEvent, useRef, useState } from "react";
8
9
import {
10
type DraftId,
@@ -11,6 +12,7 @@ import {
12
useComposerDraftStore,
13
} from "../../composerDraftStore";
14
import { useHandleNewThread } from "../../hooks/useHandleNewThread";
15
+import { useStableCallback } from "../../hooks/useStableCallback";
16
import { useClientSettings } from "../../hooks/useSettings";
17
import { readThreadShell } from "../../state/entities";
18
import { compressImageToByteLimit, dataUrlToFile } from "../../lib/imageCompression";
@@ -36,6 +38,11 @@ import { stackedThreadToast, toastManager } from "../ui/toast";
38
39
type CaptureTarget = DraftId | ScopedThreadRef;
40
41
+function isSameCaptureTarget(a: CaptureTarget, b: CaptureTarget): boolean {
42
+ if (typeof a === "string" || typeof b === "string") return a === b;
43
+ return a.environmentId === b.environmentId && a.threadId === b.threadId;
44
+}
45
+
46
export function resolveExistingSnapShotTarget(
47
target: CaptureTarget,
48
routeThreadRef: ScopedThreadRef | null,
@@ -207,25 +214,48 @@ export function SnapShotCoordinator() {
214
);
215
const animateCaptures = useClientSettings((settings) => settings.snapShotAnimations);
216
const captureTargetsRef = useRef(new Map<string, Promise<CaptureTarget | null>>());
210
- const lastTargetRef = useRef<CaptureTarget | null>(null);
217
const targetResolutionRef = useRef<Promise<CaptureTarget | null> | null>(null);
218
const drainingRef = useRef<Promise<void> | null>(null);
219
const rerunRequestedRef = useRef(false);
220
const soundedCaptureIdsRef = useRef(new Set<string>());
221
const pendingAnimationStartsRef = useRef(new Set<string>());
222
223
+ // Captures go to the last thread or draft on screen, even from routes that show neither.
224
const currentTarget = routeThreadRef ?? routeDraftId;
218
- if (currentTarget) lastTargetRef.current = currentTarget;
225
+ const [lastRouteTarget, setLastRouteTarget] = useState(currentTarget);
226
+ if (
227
+ currentTarget &&
228
+ (lastRouteTarget === null || !isSameCaptureTarget(currentTarget, lastRouteTarget))
229
+ ) {
230
+ setLastRouteTarget(currentTarget);
231
+ }
232
+ // What a resolution settled on (a promoted draft, a new draft, or nothing) stands in for the
233
+ // route's target until the route moves on, so captures in one drain share one new draft.
234
+ const settledTargetRef = useRef<{
235
+ readonly routeTarget: CaptureTarget | null;
236
+ readonly target: CaptureTarget | null;
237
+ } | null>(null);
238
+ const readLastRouteTarget = useStableCallback(() => lastRouteTarget);
239
+ // A resolution records what it settled on against the route it started from, so a
240
+ // route change while it is pending cannot hand that draft to the new route.
241
+ const readSettledTarget = (routeTarget: CaptureTarget | null) => {
242
+ const settled = settledTargetRef.current;
243
+ return settled && settled.routeTarget === routeTarget ? settled.target : routeTarget;
244
+ };
245
+ const settleTarget = (routeTarget: CaptureTarget | null, target: CaptureTarget | null) => {
246
+ settledTargetRef.current = { routeTarget, target };
247
+ };
248
220
- const resolveTarget = useCallback(async (): Promise<CaptureTarget | null> => {
221
- const lastTarget = lastTargetRef.current;
249
+ const resolveTarget = async (): Promise<CaptureTarget | null> => {
250
+ const routeTarget = readLastRouteTarget();
251
+ const lastTarget = readSettledTarget(routeTarget);
252
if (lastTarget) {
253
const existingTarget = resolveExistingSnapShotTarget(lastTarget, routeThreadRef);
254
if (existingTarget) {
225
- lastTargetRef.current = existingTarget;
255
+ settleTarget(routeTarget, existingTarget);
256
return existingTarget;
257
}
228
- lastTargetRef.current = null;
258
+ settleTarget(routeTarget, null);
259
}
260
const projectRef = resolveThreadActionProjectRef({
261
activeDraftThread,
@@ -236,27 +266,21 @@ export function SnapShotCoordinator() {
266
if (!projectRef) return null;
267
const created = await handleNewThread(projectRef);
268
if (!created) return null;
239
- lastTargetRef.current = created.draftId;
269
+ settleTarget(routeTarget, created.draftId);
270
return created.draftId;
241
- }, [activeDraftThread, activeThread, defaultProjectRef, handleNewThread, routeThreadRef]);
271
+ };
272
243
- const resolveCaptureTarget = useCallback(
244
- () => resolveSnapShotTargetOnce(targetResolutionRef, resolveTarget),
245
- [resolveTarget],
246
- );
273
+ const resolveCaptureTarget = () => resolveSnapShotTargetOnce(targetResolutionRef, resolveTarget);
274
248
- const playCaptureSound = useCallback(
249
- (id: string) => {
250
- if (!captureSound || soundedCaptureIdsRef.current.has(id)) return;
251
- soundedCaptureIdsRef.current.add(id);
252
- try {
253
- playSnapShotSound(captureSound);
254
- } catch {}
255
- },
256
- [captureSound],
257
- );
275
+ const playCaptureSound = (id: string) => {
276
+ if (!captureSound || soundedCaptureIdsRef.current.has(id)) return;
277
+ soundedCaptureIdsRef.current.add(id);
278
+ try {
279
+ playSnapShotSound(captureSound);
280
+ } catch {}
281
+ };
282
259
- const drain = useCallback(async () => {
283
+ const drain = async () => {
284
const bridge = getDesktopSnapShotBridge();
285
if (!bridge) return;
286
if (drainingRef.current) {
@@ -326,16 +350,16 @@ export function SnapShotCoordinator() {
350
});
351
drainingRef.current = operation;
352
return operation;
329
- }, [playCaptureSound, resolveCaptureTarget, routeThreadRef]);
353
+ };
354
331
- useEffect(() => {
332
- const bridge = getDesktopSnapShotBridge();
333
- if (!bridge) return;
334
- void drain();
335
- const unsubscribe = bridge.onSnapShotEvent((event) => {
355
+ // Subscriptions read the latest handlers through effect events, so they attach once
356
+ // instead of restarting (and re-draining) whenever a handler's inputs change.
357
+ const drainPending = useEffectEvent(() => drain());
358
+ const handleSnapShotEvent = useEffectEvent(
359
+ (bridge: DesktopSnapShotBridge, event: DesktopSnapShotEvent) => {
360
switch (event.type) {
361
case "requested": {
338
- const current = lastTargetRef.current;
362
+ const current = readSettledTarget(readLastRouteTarget());
363
const target = current ? resolveExistingSnapShotTarget(current, routeThreadRef) : null;
364
// Creating a new draft would navigate the renderer before a self-capture finishes.
365
if (target) {
@@ -384,19 +408,24 @@ export function SnapShotCoordinator() {
408
case "shortcut-changed":
409
return;
410
}
387
- });
388
- return unsubscribe;
389
- }, [animateCaptures, drain, playCaptureSound, resolveCaptureTarget, routeThreadRef]);
411
+ },
412
+ );
413
+ useEffect(() => {
414
+ const bridge = getDesktopSnapShotBridge();
415
+ if (!bridge) return;
416
+ void drainPending();
417
+ return bridge.onSnapShotEvent((event) => handleSnapShotEvent(bridge, event));
418
+ }, []);
419
420
useEffect(() => {
421
const dismissOnBlur = () => {
422
pendingAnimationStartsRef.current.clear();
423
dismissAllSnapShotAnimations();
424
};
396
- const drainOnFocus = () => void drain();
425
+ const drainOnFocus = () => void drainPending();
426
const handleVisibilityChange = () => {
427
if (document.visibilityState === "hidden") dismissOnBlur();
399
- else void drain();
428
+ else void drainPending();
429
};
430
window.addEventListener("blur", dismissOnBlur);
431
window.addEventListener("focus", drainOnFocus);
@@ -406,7 +435,7 @@ export function SnapShotCoordinator() {
435
window.removeEventListener("focus", drainOnFocus);
436
document.removeEventListener("visibilitychange", handleVisibilityChange);
437
};
409
- }, [drain]);
438
+ }, []);
439
440
return null;
441
}