Skip to content

Commit 392a71c

Browse files
perf(web): useActiveThreadRef compiles without manual memoization
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 70d60e0 commit 392a71c

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

‎apps/web/reactCompiledFiles.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,5 @@ export const REACT_COMPILED_FILES = [
9696
"apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx",
9797
"apps/web/src/components/ThreadTerminalDrawer.tsx",
9898
"apps/web/src/components/settings/SettingsPanels.tsx",
99+
"apps/web/src/hooks/useActiveThreadRef.ts",
99100
] as const;
Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
22
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+
}
413

514
/**
615
* Takes the thread object but keys on its ids: the shell changes identity on every update
@@ -11,9 +20,11 @@ export function useActiveThreadRef(
1120
): ScopedThreadRef | null {
1221
const environmentId = thread?.environmentId ?? null;
1322
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;
1930
}

0 commit comments

Comments
 (0)