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): useActiveThreadRef compiles without manual memoization (#20588)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 22:04 UTC
567bc559acbf3d092cdf35855fe0df9b14497f46
1 file changed
+17
-6
apps/web/src/hooks/useActiveThreadRef.ts
+17
-6
@@ -1,6 +1,15 @@
1
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2
import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
3
-import { useMemo } from "react";
3
+import { useState } from "react";
4
+
5
+function toThreadRef(
6
+ environmentId: EnvironmentId | null,
7
+ threadId: ThreadId | null,
8
+): ScopedThreadRef | null {
9
+ return environmentId !== null && threadId !== null
10
+ ? scopeThreadRef(environmentId, threadId)
11
+ : null;
12
+}
13
14
/**
15
* Takes the thread object but keys on its ids: the shell changes identity on every update
@@ -11,9 +20,11 @@ export function useActiveThreadRef(
20
): ScopedThreadRef | null {
21
const environmentId = thread?.environmentId ?? null;
22
const threadId = thread?.id ?? null;
14
- return useMemo(
15
- () =>
16
- environmentId !== null && threadId !== null ? scopeThreadRef(environmentId, threadId) : null,
17
- [environmentId, threadId],
18
- );
23
+ const [ref, setRef] = useState(() => toThreadRef(environmentId, threadId));
24
+ if ((ref?.environmentId ?? null) !== environmentId || (ref?.threadId ?? null) !== threadId) {
25
+ const next = toThreadRef(environmentId, threadId);
26
+ setRef(next);
27
+ return next;
28
+ }
29
+ return ref;
30
}