The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 08:00 UTC
perf(web): browser viewport resize checks its source through a stable callback (#20465)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 18:46 UTC
635396ca3cdc858f7d829e3b31dd498cda2a8cab
2 files changed
+17
-19
apps/web/reactCompiledFiles.ts
+1
@@ -40,4 +40,5 @@ export const REACT_COMPILED_FILES = [
40
"apps/web/src/components/Sidebar.logic.ts",
41
"apps/web/src/components/pullRequest/usePullRequestFilesViewed.ts",
42
"apps/web/src/hooks/useLiveRefresh.ts",
43
+ "apps/web/src/browser/useBrowserViewportResize.ts",
44
] as const;
apps/web/src/browser/useBrowserViewportResize.ts
+16
-19
@@ -2,7 +2,6 @@
2
3
import type { PreviewViewportSetting, PreviewViewportSize } from "@t3tools/contracts";
4
import {
5
- useCallback,
5
useEffect,
6
useRef,
7
useState,
@@ -10,6 +9,7 @@ import {
9
type PointerEvent as ReactPointerEvent,
10
} from "react";
11
12
+import { useStableCallback } from "../hooks/useStableCallback";
13
import { commitBrowserViewportChange } from "./browserViewportActions";
14
import {
15
browserViewportSettingKey,
@@ -43,8 +43,8 @@ export function useBrowserViewportResize(options: {
43
const keyboardViewportRef = useRef<ViewportDrag | null>(null);
44
const [dragViewport, setDragViewport] = useState<ViewportDrag | null>(null);
45
const sourceViewportKey = browserViewportSettingKey(viewport);
46
- const sourceViewportKeyRef = useRef(sourceViewportKey);
47
- sourceViewportKeyRef.current = sourceViewportKey;
46
+ // Drag listeners and the keyboard commit timer outlive the render that started them.
47
+ const isCurrentSource = useStableCallback((key: string) => key === sourceViewportKey);
48
const activeDrag = dragViewport?.sourceKey === sourceViewportKey ? dragViewport : null;
49
const effectiveViewport = activeDrag
50
? ({
@@ -85,20 +85,17 @@ export function useBrowserViewportResize(options: {
85
keyboardViewportRef.current = null;
86
}, [sourceViewportKey]);
87
88
- const commitViewportChange = useCallback(
89
- (next: PreviewViewportSetting) => {
90
- dragVersionRef.current += 1;
91
- dragCleanupRef.current?.();
92
- if (keyboardCommitTimerRef.current !== null) {
93
- clearTimeout(keyboardCommitTimerRef.current);
94
- keyboardCommitTimerRef.current = null;
95
- }
96
- keyboardViewportRef.current = null;
97
- setDragViewport(null);
98
- return commitBrowserViewportChange(tabId, next);
99
- },
100
- [tabId],
101
- );
88
+ const commitViewportChange = (next: PreviewViewportSetting) => {
89
+ dragVersionRef.current += 1;
90
+ dragCleanupRef.current?.();
91
+ if (keyboardCommitTimerRef.current !== null) {
92
+ clearTimeout(keyboardCommitTimerRef.current);
93
+ keyboardCommitTimerRef.current = null;
94
+ }
95
+ keyboardViewportRef.current = null;
96
+ setDragViewport(null);
97
+ return commitBrowserViewportChange(tabId, next);
98
+ };
99
100
const clearDrag = () => setDragViewport(null);
101
const commitDrag = (next: PreviewViewportSetting) => {
@@ -149,7 +146,7 @@ export function useBrowserViewportResize(options: {
146
keyboardCommitTimerRef.current = setTimeout(() => {
147
keyboardCommitTimerRef.current = null;
148
const latest = keyboardViewportRef.current;
152
- if (!latest || latest.sourceKey !== sourceViewportKeyRef.current) return;
149
+ if (!latest || !isCurrentSource(latest.sourceKey)) return;
150
keyboardViewportRef.current = null;
151
commitDrag({ _tag: "freeform", width: latest.width, height: latest.height });
152
}, KEYBOARD_RESIZE_COMMIT_DELAY_MS);
@@ -187,7 +184,7 @@ export function useBrowserViewportResize(options: {
184
target.setPointerCapture(pointerId);
185
} catch {}
186
190
- const sourceChanged = () => sourceViewportKeyRef.current !== sourceViewportKey;
187
+ const sourceChanged = () => !isCurrentSource(sourceViewportKey);
188
const move = (moveEvent: PointerEvent) => {
189
if (moveEvent.pointerId !== pointerId) return;
190
if (sourceChanged()) {