@devries-cameron20 / t3code Mirror · read-only / commits / d4c49bdd65
Fork 0

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): thread status indicators compile without manual memoization (#20630)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 16, 2026 at 21:59 UTC d4c49bdd65ee5092b61306371b38974c9e5dc804
1 file changed +39 -34
apps/web/src/components/ThreadStatusIndicators.tsx
+39 -34
@@ -4,6 +4,7 @@ import {
4 scopeThreadRef,
5 } from "@t3tools/client-runtime/environment";
6 import { useSupportsMultiplePullRequests } from "~/hooks/useSupportsMultiplePullRequests";
7 +import { useStableCallback } from "~/hooks/useStableCallback";
8
9 import { pullRequestDetailToVcsStatus } from "@t3tools/client-runtime/state/pull-requests";
10 import {
@@ -16,7 +17,6 @@ import {
17 } from "@t3tools/contracts";
18 import { Atom } from "effect/reactivity";
19 import { FolderGit2Icon, TerminalIcon } from "lucide-react";
19 -import { useCallback, useMemo } from "react";
20 import { useEnvironment, usePrimaryEnvironmentId } from "../state/environments";
21 import { EnvironmentMachineIcon } from "./EnvironmentMachineIcon";
22 import { useProject } from "../state/entities";
@@ -95,19 +95,11 @@ export function useLinkedThreadPullRequest(
95 branchPullRequest?: ThreadLinkedPullRequest | null,
96 ): LinkedThreadPullRequestStatus | null {
97 const supportsLinks = useSupportsMultiplePullRequests(environmentId);
98 - const current = useMemo(
99 - () => (supportsLinks ? resolveThreadCurrentPullRequestLink(pullRequests ?? []) : null),
100 - [pullRequests, supportsLinks],
101 - );
98 + const { current, currentStatus } = useCurrentPullRequestLink(supportsLinks, pullRequests);
99 const fallback =
100 current === null ? ((!supportsLinks ? linkedPullRequest : null) ?? branchPullRequest) : null;
104 - // Stable per link: the shared summary effect keys on this object, and a sidebar row must not
105 - // touch the cache on every render.
106 - const reference = useMemo(() => {
107 - if (fallback == null) return null;
108 - const host = parseChangeRequestUrl(fallback.url)?.host;
109 - return { ...fallback, ...(host === undefined ? {} : { host }) };
110 - }, [fallback]);
101 + // A sidebar row must not touch the summary cache on every render.
102 + const reference = useLinkedPullRequestReference(fallback);
103 const queried = useEnvironmentQuery(
104 !enabled || environmentId === null || reference === null
105 ? null
@@ -120,15 +112,35 @@ export function useLinkedThreadPullRequest(
112 queried.dataUpdatedAt,
113 );
114
123 - return useMemo(() => {
124 - if (current !== null) return linkedPullRequestSnapshotStatus(current);
125 - return detail === null
126 - ? null
127 - : {
128 - pr: pullRequestDetailToVcsStatus(detail),
129 - sourceControlProvider: { kind: detail.provider, name: detail.provider, baseUrl: "" },
130 - };
131 - }, [current, detail]);
115 + if (current !== null) return currentStatus;
116 + return detail === null
117 + ? null
118 + : {
119 + pr: pullRequestDetailToVcsStatus(detail),
120 + sourceControlProvider: { kind: detail.provider, name: detail.provider, baseUrl: "" },
121 + };
122 +}
123 +
124 +// Callers key on the returned status, so it stays stable across the query hooks.
125 +function useCurrentPullRequestLink(
126 + supportsLinks: boolean,
127 + pullRequests: ReadonlyArray<ThreadPullRequestLink> | undefined,
128 +) {
129 + "use memo";
130 + const current = supportsLinks ? resolveThreadCurrentPullRequestLink(pullRequests ?? []) : null;
131 + return {
132 + current,
133 + currentStatus: current === null ? null : linkedPullRequestSnapshotStatus(current),
134 + };
135 +}
136 +
137 +// The query atom and shared summary effect key on this object, so it stays
138 +// stable per link across the hooks that read it.
139 +function useLinkedPullRequestReference(fallback: ThreadLinkedPullRequest | null | undefined) {
140 + "use memo";
141 + if (fallback == null) return null;
142 + const host = parseChangeRequestUrl(fallback.url)?.host;
143 + return { ...fallback, ...(host === undefined ? {} : { host }) };
144 }
145
146 export function linkedPullRequestSnapshotStatus(
@@ -330,10 +342,8 @@ export function ThreadPullRequestsMiniList({
342 pullRequests: ReadonlyArray<ThreadPullRequestLink>;
343 onOpenPullRequest?: (event: MouseEvent<HTMLAnchorElement>, url: string) => void;
344 }) {
333 - const lines = useMemo(
334 - () =>
335 - pullRequestListLines(resolveThreadPullRequestChains(visibleThreadPullRequests(pullRequests))),
336 - [pullRequests],
345 + const lines = pullRequestListLines(
346 + resolveThreadPullRequestChains(visibleThreadPullRequests(pullRequests)),
347 );
348 if (lines.length === 0) return null;
349 return (
@@ -873,21 +883,16 @@ export function ThreadRowLeadingStatus({
883 snapshot?: ThreadChangeRequestSnapshot | undefined;
884 }) {
885 const { leaseLiveStatus, rowRef } = useSidebarRowSubscriptionLease(false);
876 - const statusRef = useCallback(
877 - (node: HTMLSpanElement | null) => rowRef(node?.parentElement ?? null),
878 - [rowRef],
886 + // A new ref callback would detach and reattach the row, and each reattach sets lease state.
887 + const statusRef = useStableCallback((node: HTMLSpanElement | null) =>
888 + rowRef(node?.parentElement ?? null),
889 );
890 const threadRef = scopeThreadRef(thread.environmentId, thread.id);
891 const localLastVisitedAt = useUiStateStore(
892 (state) => state.threadLastVisitedAtById[scopedThreadKey(threadRef)],
893 );
894 const lastVisitedAt = resolveThreadLastVisitedAt(thread.lastVisitedAt, localLastVisitedAt);
885 - const threadProject = useProject(
886 - useMemo(
887 - () => scopeProjectRef(thread.environmentId, thread.projectId),
888 - [thread.environmentId, thread.projectId],
889 - ),
890 - );
895 + const threadProject = useProject(scopeProjectRef(thread.environmentId, thread.projectId));
896 const threadProjectCwd = threadProject?.workspaceRoot ?? null;
897 const gitCwd = thread.worktreePath ?? threadProjectCwd;
898 const linkedPullRequest = useLinkedThreadPullRequest(