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, 06:45 UTC
perf(web): usePreviewBridge compiles with the React Compiler (#20644)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 21:57 UTC
9578a59abd28d6fe67138641bdae7864861055d9
1 file changed
+18
-10
apps/web/src/components/preview/usePreviewBridge.ts
+18
-10
@@ -8,7 +8,7 @@ import type {
8
} from "@t3tools/contracts";
9
import { parseScopedThreadKey, scopedThreadKey } from "@t3tools/client-runtime/environment";
10
import * as Option from "effect/Option";
11
-import { useEffect, useEffectEvent, useMemo, useRef } from "react";
11
+import { useEffect, useEffectEvent, useRef } from "react";
12
13
import {
14
flushPendingFaviconsForThread,
@@ -43,13 +43,8 @@ export function usePreviewBridge(input: {
43
const { threadRef, tabId, runtimeTabId, serverDriven = false } = input;
44
const clearBrowserPointer = useBrowserPointerStore((state) => state.clear);
45
const reportStatus = useAtomCommand(previewEnvironment.reportStatus, "preview status report");
46
- const bridge = previewBridge;
46
const threadKey = scopedThreadKey(threadRef);
48
- const stableThreadRef = useMemo(() => {
49
- const parsed = parseScopedThreadKey(threadKey);
50
- if (!parsed) throw new Error(`Invalid scoped thread key: ${threadKey}`);
51
- return parsed;
52
- }, [threadKey]);
47
+ const stableThreadRef = useThreadRefForKey(threadKey);
48
const projectRef = useFaviconProjectRefForThread(stableThreadRef);
49
const preparedConnection = usePreparedConnection(stableThreadRef.environmentId);
50
const environmentHostname = Option.isSome(preparedConnection)
@@ -87,18 +82,31 @@ export function usePreviewBridge(input: {
82
},
83
);
84
useEffect(() => {
90
- if (!bridge || typeof window === "undefined") return;
85
+ if (!previewBridge || typeof window === "undefined") return;
86
lastReportedUrl.current = null;
87
lastReportedKind.current = null;
88
lastDesktopNavStatus.current = null;
94
- return bridge.onStateChange(handleStateChange);
95
- }, [bridge, runtimeTabId, stableThreadRef, tabId]);
89
+ return previewBridge.onStateChange(handleStateChange);
90
+ }, [
91
+ // oxlint-disable-next-line react/exhaustive-effect-dependencies -- A different tab or thread starts its status reports and pointer tracking from scratch.
92
+ runtimeTabId,
93
+ stableThreadRef,
94
+ tabId,
95
+ ]);
96
useEffect(() => {
97
if (!projectRef) return;
98
flushPendingFaviconsForThread(stableThreadRef, projectRef, environmentHostname);
99
}, [environmentHostname, projectRef, stableThreadRef]);
100
}
101
102
+// Callers pass a fresh threadRef each render; the key keeps the ref stable for the effects.
103
+function useThreadRefForKey(threadKey: string): ScopedThreadRef {
104
+ "use memo";
105
+ const parsed = parseScopedThreadKey(threadKey);
106
+ if (!parsed) throw new Error(`Invalid scoped thread key: ${threadKey}`);
107
+ return parsed;
108
+}
109
+
110
function shouldClearBrowserPointer(
111
previous: DesktopPreviewTabState["navStatus"] | null,
112
current: DesktopPreviewTabState["navStatus"],