Skip to content

Commit 415e69f

Browse files
perf(web): snapshot coordinator keeps the last route target in state
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3161332 commit 415e69f

2 files changed

Lines changed: 67 additions & 37 deletions

File tree

‎apps/web/reactCompiledFiles.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ export const REACT_COMPILED_FILES = [
4141
"apps/web/src/components/pullRequest/usePullRequestFilesViewed.ts",
4242
"apps/web/src/hooks/useLiveRefresh.ts",
4343
"apps/web/src/browser/useBrowserViewportResize.ts",
44+
"apps/web/src/components/desktop/SnapShotCoordinator.tsx",
4445
] as const;

‎apps/web/src/components/desktop/SnapShotCoordinator.tsx‎

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import {
22
type DesktopPendingSnapShot,
3+
type DesktopSnapShotEvent,
34
PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
45
type ScopedThreadRef,
56
} from "@t3tools/contracts";
6-
import { useCallback, useEffect, useRef } from "react";
7+
import { useEffect, useEffectEvent, useRef, useState } from "react";
78

89
import {
910
type DraftId,
1011
type PersistedComposerImageAttachment,
1112
useComposerDraftStore,
1213
} from "../../composerDraftStore";
1314
import { useHandleNewThread } from "../../hooks/useHandleNewThread";
15+
import { useStableCallback } from "../../hooks/useStableCallback";
1416
import { useClientSettings } from "../../hooks/useSettings";
1517
import { readThreadShell } from "../../state/entities";
1618
import { compressImageToByteLimit, dataUrlToFile } from "../../lib/imageCompression";
@@ -36,6 +38,11 @@ import { stackedThreadToast, toastManager } from "../ui/toast";
3638

3739
type CaptureTarget = DraftId | ScopedThreadRef;
3840

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+
3946
export function resolveExistingSnapShotTarget(
4047
target: CaptureTarget,
4148
routeThreadRef: ScopedThreadRef | null,
@@ -207,25 +214,48 @@ export function SnapShotCoordinator() {
207214
);
208215
const animateCaptures = useClientSettings((settings) => settings.snapShotAnimations);
209216
const captureTargetsRef = useRef(new Map<string, Promise<CaptureTarget | null>>());
210-
const lastTargetRef = useRef<CaptureTarget | null>(null);
211217
const targetResolutionRef = useRef<Promise<CaptureTarget | null> | null>(null);
212218
const drainingRef = useRef<Promise<void> | null>(null);
213219
const rerunRequestedRef = useRef(false);
214220
const soundedCaptureIdsRef = useRef(new Set<string>());
215221
const pendingAnimationStartsRef = useRef(new Set<string>());
216222

223+
// Captures go to the last thread or draft on screen, even from routes that show neither.
217224
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+
};
219248

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);
222252
if (lastTarget) {
223253
const existingTarget = resolveExistingSnapShotTarget(lastTarget, routeThreadRef);
224254
if (existingTarget) {
225-
lastTargetRef.current = existingTarget;
255+
settleTarget(routeTarget, existingTarget);
226256
return existingTarget;
227257
}
228-
lastTargetRef.current = null;
258+
settleTarget(routeTarget, null);
229259
}
230260
const projectRef = resolveThreadActionProjectRef({
231261
activeDraftThread,
@@ -236,27 +266,21 @@ export function SnapShotCoordinator() {
236266
if (!projectRef) return null;
237267
const created = await handleNewThread(projectRef);
238268
if (!created) return null;
239-
lastTargetRef.current = created.draftId;
269+
settleTarget(routeTarget, created.draftId);
240270
return created.draftId;
241-
}, [activeDraftThread, activeThread, defaultProjectRef, handleNewThread, routeThreadRef]);
271+
};
242272

243-
const resolveCaptureTarget = useCallback(
244-
() => resolveSnapShotTargetOnce(targetResolutionRef, resolveTarget),
245-
[resolveTarget],
246-
);
273+
const resolveCaptureTarget = () => resolveSnapShotTargetOnce(targetResolutionRef, resolveTarget);
247274

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+
};
258282

259-
const drain = useCallback(async () => {
283+
const drain = async () => {
260284
const bridge = getDesktopSnapShotBridge();
261285
if (!bridge) return;
262286
if (drainingRef.current) {
@@ -326,16 +350,16 @@ export function SnapShotCoordinator() {
326350
});
327351
drainingRef.current = operation;
328352
return operation;
329-
}, [playCaptureSound, resolveCaptureTarget, routeThreadRef]);
353+
};
330354

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) => {
336360
switch (event.type) {
337361
case "requested": {
338-
const current = lastTargetRef.current;
362+
const current = readSettledTarget(readLastRouteTarget());
339363
const target = current ? resolveExistingSnapShotTarget(current, routeThreadRef) : null;
340364
// Creating a new draft would navigate the renderer before a self-capture finishes.
341365
if (target) {
@@ -384,19 +408,24 @@ export function SnapShotCoordinator() {
384408
case "shortcut-changed":
385409
return;
386410
}
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+
}, []);
390419

391420
useEffect(() => {
392421
const dismissOnBlur = () => {
393422
pendingAnimationStartsRef.current.clear();
394423
dismissAllSnapShotAnimations();
395424
};
396-
const drainOnFocus = () => void drain();
425+
const drainOnFocus = () => void drainPending();
397426
const handleVisibilityChange = () => {
398427
if (document.visibilityState === "hidden") dismissOnBlur();
399-
else void drain();
428+
else void drainPending();
400429
};
401430
window.addEventListener("blur", dismissOnBlur);
402431
window.addEventListener("focus", drainOnFocus);
@@ -406,7 +435,7 @@ export function SnapShotCoordinator() {
406435
window.removeEventListener("focus", drainOnFocus);
407436
document.removeEventListener("visibilitychange", handleVisibilityChange);
408437
};
409-
}, [drain]);
438+
}, []);
410439

411440
return null;
412441
}

0 commit comments

Comments
 (0)