Skip to content

Commit 3161332

Browse files
perf(web): browser viewport resize checks its source through a stable callback
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 843c8f7 commit 3161332

2 files changed

Lines changed: 17 additions & 19 deletions

File tree

‎apps/web/reactCompiledFiles.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ export const REACT_COMPILED_FILES = [
4040
"apps/web/src/components/Sidebar.logic.ts",
4141
"apps/web/src/components/pullRequest/usePullRequestFilesViewed.ts",
4242
"apps/web/src/hooks/useLiveRefresh.ts",
43+
"apps/web/src/browser/useBrowserViewportResize.ts",
4344
] as const;

‎apps/web/src/browser/useBrowserViewportResize.ts‎

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import type { PreviewViewportSetting, PreviewViewportSize } from "@t3tools/contracts";
44
import {
5-
useCallback,
65
useEffect,
76
useRef,
87
useState,
98
type KeyboardEvent as ReactKeyboardEvent,
109
type PointerEvent as ReactPointerEvent,
1110
} from "react";
1211

12+
import { useStableCallback } from "../hooks/useStableCallback";
1313
import { commitBrowserViewportChange } from "./browserViewportActions";
1414
import {
1515
browserViewportSettingKey,
@@ -43,8 +43,8 @@ export function useBrowserViewportResize(options: {
4343
const keyboardViewportRef = useRef<ViewportDrag | null>(null);
4444
const [dragViewport, setDragViewport] = useState<ViewportDrag | null>(null);
4545
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);
4848
const activeDrag = dragViewport?.sourceKey === sourceViewportKey ? dragViewport : null;
4949
const effectiveViewport = activeDrag
5050
? ({
@@ -85,20 +85,17 @@ export function useBrowserViewportResize(options: {
8585
keyboardViewportRef.current = null;
8686
}, [sourceViewportKey]);
8787

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

103100
const clearDrag = () => setDragViewport(null);
104101
const commitDrag = (next: PreviewViewportSetting) => {
@@ -149,7 +146,7 @@ export function useBrowserViewportResize(options: {
149146
keyboardCommitTimerRef.current = setTimeout(() => {
150147
keyboardCommitTimerRef.current = null;
151148
const latest = keyboardViewportRef.current;
152-
if (!latest || latest.sourceKey !== sourceViewportKeyRef.current) return;
149+
if (!latest || !isCurrentSource(latest.sourceKey)) return;
153150
keyboardViewportRef.current = null;
154151
commitDrag({ _tag: "freeform", width: latest.width, height: latest.height });
155152
}, KEYBOARD_RESIZE_COMMIT_DELAY_MS);
@@ -187,7 +184,7 @@ export function useBrowserViewportResize(options: {
187184
target.setPointerCapture(pointerId);
188185
} catch {}
189186

190-
const sourceChanged = () => sourceViewportKeyRef.current !== sourceViewportKey;
187+
const sourceChanged = () => !isCurrentSource(sourceViewportKey);
191188
const move = (moveEvent: PointerEvent) => {
192189
if (moveEvent.pointerId !== pointerId) return;
193190
if (sourceChanged()) {

0 commit comments

Comments
 (0)