Skip to content
TypeScript

perf(web): thread status indicators compile without manual memoization (#20630)

Julius Marminged4c49bdd65ee
diff --git a/apps/web/src/components/ThreadStatusIndicators.tsx b/apps/web/src/components/ThreadStatusIndicators.tsx
index 626345bc006..fa0584686a4 100644
--- a/apps/web/src/components/ThreadStatusIndicators.tsx
+++ b/apps/web/src/components/ThreadStatusIndicators.tsx
@@ -4,6 +4,7 @@ import {
scopeThreadRef,
} from "@t3tools/client-runtime/environment";
import { useSupportsMultiplePullRequests } from "~/hooks/useSupportsMultiplePullRequests";
+import { useStableCallback } from "~/hooks/useStableCallback";
import { pullRequestDetailToVcsStatus } from "@t3tools/client-runtime/state/pull-requests";
import {
@@ -16,7 +17,6 @@ import {
} from "@t3tools/contracts";
import { Atom } from "effect/reactivity";
import { FolderGit2Icon, TerminalIcon } from "lucide-react";
-import { useCallback, useMemo } from "react";
import { useEnvironment, usePrimaryEnvironmentId } from "../state/environments";
import { EnvironmentMachineIcon } from "./EnvironmentMachineIcon";
import { useProject } from "../state/entities";
@@ -95,19 +95,11 @@ export function useLinkedThreadPullRequest(
branchPullRequest?: ThreadLinkedPullRequest | null,
): LinkedThreadPullRequestStatus | null {
const supportsLinks = useSupportsMultiplePullRequests(environmentId);
- const current = useMemo(
- () => (supportsLinks ? resolveThreadCurrentPullRequestLink(pullRequests ?? []) : null),
- [pullRequests, supportsLinks],
- );
+ const { current, currentStatus } = useCurrentPullRequestLink(supportsLinks, pullRequests);
const fallback =
current === null ? ((!supportsLinks ? linkedPullRequest : null) ?? branchPullRequest) : null;
- // Stable per link: the shared summary effect keys on this object, and a sidebar row must not
- // touch the cache on every render.
- const reference = useMemo(() => {
- if (fallback == null) return null;
- const host = parseChangeRequestUrl(fallback.url)?.host;
- return { ...fallback, ...(host === undefined ? {} : { host }) };
- }, [fallback]);
+ // A sidebar row must not touch the summary cache on every render.
+ const reference = useLinkedPullRequestReference(fallback);
const queried = useEnvironmentQuery(
!enabled || environmentId === null || reference === null
? null
@@ -120,15 +112,35 @@ export function useLinkedThreadPullRequest(
queried.dataUpdatedAt,
);
- return useMemo(() => {
- if (current !== null) return linkedPullRequestSnapshotStatus(current);
- return detail === null
- ? null
- : {
- pr: pullRequestDetailToVcsStatus(detail),
- sourceControlProvider: { kind: detail.provider, name: detail.provider, baseUrl: "" },
- };
- }, [current, detail]);
+ if (current !== null) return currentStatus;
+ return detail === null
+ ? null
+ : {
+ pr: pullRequestDetailToVcsStatus(detail),
+ sourceControlProvider: { kind: detail.provider, name: detail.provider, baseUrl: "" },
+ };
+}
+
+// Callers key on the returned status, so it stays stable across the query hooks.
+function useCurrentPullRequestLink(
+ supportsLinks: boolean,
+ pullRequests: ReadonlyArray<ThreadPullRequestLink> | undefined,
+) {
+ "use memo";
+ const current = supportsLinks ? resolveThreadCurrentPullRequestLink(pullRequests ?? []) : null;
+ return {
+ current,
+ currentStatus: current === null ? null : linkedPullRequestSnapshotStatus(current),
+ };
+}
+
+// The query atom and shared summary effect key on this object, so it stays
+// stable per link across the hooks that read it.
+function useLinkedPullRequestReference(fallback: ThreadLinkedPullRequest | null | undefined) {
+ "use memo";
+ if (fallback == null) return null;
+ const host = parseChangeRequestUrl(fallback.url)?.host;
+ return { ...fallback, ...(host === undefined ? {} : { host }) };
}
export function linkedPullRequestSnapshotStatus(
@@ -330,10 +342,8 @@ export function ThreadPullRequestsMiniList({
pullRequests: ReadonlyArray<ThreadPullRequestLink>;
onOpenPullRequest?: (event: MouseEvent<HTMLAnchorElement>, url: string) => void;
}) {
- const lines = useMemo(
- () =>
- pullRequestListLines(resolveThreadPullRequestChains(visibleThreadPullRequests(pullRequests))),
- [pullRequests],
+ const lines = pullRequestListLines(
+ resolveThreadPullRequestChains(visibleThreadPullRequests(pullRequests)),
);
if (lines.length === 0) return null;
return (
@@ -873,21 +883,16 @@ export function ThreadRowLeadingStatus({
snapshot?: ThreadChangeRequestSnapshot | undefined;
}) {
const { leaseLiveStatus, rowRef } = useSidebarRowSubscriptionLease(false);
- const statusRef = useCallback(
- (node: HTMLSpanElement | null) => rowRef(node?.parentElement ?? null),
- [rowRef],
+ // A new ref callback would detach and reattach the row, and each reattach sets lease state.
+ const statusRef = useStableCallback((node: HTMLSpanElement | null) =>
+ rowRef(node?.parentElement ?? null),
);
const threadRef = scopeThreadRef(thread.environmentId, thread.id);
const localLastVisitedAt = useUiStateStore(
(state) => state.threadLastVisitedAtById[scopedThreadKey(threadRef)],
);
const lastVisitedAt = resolveThreadLastVisitedAt(thread.lastVisitedAt, localLastVisitedAt);
- const threadProject = useProject(
- useMemo(
- () => scopeProjectRef(thread.environmentId, thread.projectId),
- [thread.environmentId, thread.projectId],
- ),
- );
+ const threadProject = useProject(scopeProjectRef(thread.environmentId, thread.projectId));
const threadProjectCwd = threadProject?.workspaceRoot ?? null;
const gitCwd = thread.worktreePath ?? threadProjectCwd;
const linkedPullRequest = useLinkedThreadPullRequest(