Skip to content

Commit 9a063bf

Browse files
perf(web): the thread panel and branch toolbar keep their environment list across streaming ticks
ChatView rebuilt the logical-project environment list on every render, and its ThreadDetailsPanel props were a plain object spread into the uncached outer JSX, so both the panel and BranchToolbar re-rendered with ChatView on every projection tick. The list now comes from useLogicalProjectEnvironments, which reads only environments, projects, the primary environment id and the grouping settings, and the panel's props are written inline so the compiler caches its element on its real inputs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 76aa2bd commit 9a063bf

2 files changed

Lines changed: 139 additions & 102 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 59 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
rememberCheckoutIsRepo,
2424
} from "./ChatView.logic";
2525
import { useLoadBalancedEnvironment } from "../hooks/useLoadBalancedEnvironment";
26+
import { useLogicalProjectEnvironments } from "../hooks/useLogicalProjectEnvironments";
2627
import { useScratchProject } from "../hooks/useScratchProject";
2728
import { useStableCallback } from "../hooks/useStableCallback";
2829
import { useProjectScriptEditing } from "./chat/useProjectScriptEditing";
@@ -65,7 +66,6 @@ import {
6566
type MessageId,
6667
type ModelSelection,
6768
type ProjectScript,
68-
type ProjectId,
6969
type PreviewAnnotationPayload,
7070
ProviderInstanceId,
7171
type ServerProvider,
@@ -76,7 +76,6 @@ import {
7676
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
7777
ProviderInteractionMode,
7878
ProviderDriverKind,
79-
resolveEnvironmentMachineKind,
8079
resolveProjectProviderInstanceEnabled,
8180
RuntimeMode,
8281
} from "@t3tools/contracts";
@@ -419,10 +418,9 @@ import {
419418
RightPanelMaximizeControl,
420419
} from "./chat/PanelLayoutControls";
421420
import { expandedImageKey, type ExpandedImagePreview } from "./chat/ExpandedImagePreview";
422-
import { ThreadDetailsPanel, type ThreadDetailsPanelProps } from "./chat/ThreadDetailsPanel";
421+
import { ThreadDetailsPanel } from "./chat/ThreadDetailsPanel";
423422
import { NoActiveThreadState } from "./NoActiveThreadState";
424423
import {
425-
type EnvironmentOption,
426424
resolveEffectiveEnvMode,
427425
resolveLocalCheckoutBranchMismatch,
428426
shouldShowComposerContextStrip,
@@ -2014,60 +2012,17 @@ export default function ChatView(props: ChatViewProps) {
20142012
const handleDisconnectActiveEnvironmentStable = useStableCallback(
20152013
handleDisconnectActiveEnvironment,
20162014
);
2017-
const { scratchWorkspaceRootFor, openScratchProject } = useScratchProject();
2015+
const { openScratchProject } = useScratchProject();
20182016
const activeProjectIsScratch =
20192017
activeProject !== null &&
20202018
isScratchProject(
20212019
activeProject,
20222020
environmentById.get(activeProject.environmentId)?.serverConfig?.scratchWorkspaceRoot ?? null,
20232021
);
2024-
const logicalProjectEnvironments = (() => {
2025-
if (!activeProject) return [];
2026-
const envs: EnvironmentOption[] = [];
2027-
const pushEnvironment = (environmentId: EnvironmentId, projectId: ProjectId | null) => {
2028-
const environment = environmentById.get(environmentId) ?? null;
2029-
envs.push({
2030-
environmentId,
2031-
projectId,
2032-
label: environment?.label ?? environmentId,
2033-
isPrimary: environmentId === primaryEnvironmentId,
2034-
machine: resolveEnvironmentMachineKind(environment?.serverConfig ?? null),
2035-
});
2036-
};
2037-
if (activeProjectIsScratch && draftId) {
2038-
for (const environment of environments) {
2039-
const scratchRoot = scratchWorkspaceRootFor(environment.environmentId);
2040-
if (scratchRoot === null && environment.environmentId !== activeProject.environmentId)
2041-
continue;
2042-
const scratchProject =
2043-
environment.environmentId === activeProject.environmentId
2044-
? activeProject
2045-
: allProjects.find(
2046-
(p) =>
2047-
p.environmentId === environment.environmentId && isScratchProject(p, scratchRoot),
2048-
);
2049-
pushEnvironment(environment.environmentId, scratchProject?.id ?? null);
2050-
}
2051-
} else {
2052-
const logicalKey = deriveLogicalProjectKeyFromSettings(
2053-
activeProject,
2054-
projectGroupingSettings,
2055-
);
2056-
const seen = new Set<string>();
2057-
for (const p of allProjects) {
2058-
if (seen.has(p.environmentId)) continue;
2059-
if (deriveLogicalProjectKeyFromSettings(p, projectGroupingSettings) !== logicalKey)
2060-
continue;
2061-
seen.add(p.environmentId);
2062-
pushEnvironment(p.environmentId, p.id);
2063-
}
2064-
}
2065-
envs.sort((a, b) => {
2066-
if (a.isPrimary !== b.isPrimary) return a.isPrimary ? -1 : 1;
2067-
return a.label.localeCompare(b.label);
2068-
});
2069-
return envs;
2070-
})();
2022+
const logicalProjectEnvironments = useLogicalProjectEnvironments(
2023+
activeProject,
2024+
activeProjectIsScratch && draftId !== null,
2025+
);
20712026
const hasMultipleEnvironments = logicalProjectEnvironments.length > 1;
20722027
const canAutoBalanceEnvironments = hasMultipleEnvironments && !activeProjectIsScratch;
20732028
const activeEnvironmentOption =
@@ -7754,55 +7709,6 @@ export default function ChatView(props: ChatViewProps) {
77547709
}}
77557710
/>
77567711
) : null;
7757-
const threadDetailsPanelProps: ThreadDetailsPanelProps = {
7758-
anchor: threadPanelPopoverAnchorRef,
7759-
handle: threadPanelPopoverHandle,
7760-
onPresentationChange: setThreadPanelPresentation,
7761-
forceNewWorktree: multipleModelSelections !== null,
7762-
environmentId: activeThread.environmentId,
7763-
threadId: activeThread.id,
7764-
...(draftId ? { draftId } : {}),
7765-
activeProjectName: activeProject?.title,
7766-
activeProjectScripts: activeProject ? activeProjectScripts : undefined,
7767-
preferredScriptId: activeProject
7768-
? (lastInvokedScriptByProjectId[activeProject.id] ?? null)
7769-
: null,
7770-
keybindings,
7771-
availableEditors,
7772-
showOpenInPicker,
7773-
gitCwd,
7774-
isGitRepo,
7775-
envLocked,
7776-
availableEnvironments: logicalProjectEnvironments,
7777-
autoEnvironmentLabel,
7778-
onAutoEnvironment:
7779-
draftId &&
7780-
!envLocked &&
7781-
canAutoBalanceEnvironments &&
7782-
loadBalancingSettings.loadBalancingEnabled
7783-
? onAutoEnvironmentStable
7784-
: undefined,
7785-
onEnvironmentChange: onEnvironmentChangeStable,
7786-
onEnvModeChange: onEnvModeChangeStable,
7787-
envMode,
7788-
...(canOverrideServerThreadEnvMode
7789-
? {
7790-
activeThreadBranchOverride: activeThreadBranch,
7791-
onActiveThreadBranchOverrideChange: setPendingServerThreadBranch,
7792-
}
7793-
: {}),
7794-
startFromOrigin,
7795-
onStartFromOriginChange,
7796-
...(canCheckoutPullRequestIntoThread
7797-
? { onCheckoutPullRequestRequest: openPullRequestDialogStable }
7798-
: {}),
7799-
onComposerFocusRequest: scheduleComposerFocusStable,
7800-
...(isServerThread && isGitRepo ? { onOpenChanges: openChangesFromThreadPanelStable } : {}),
7801-
onRunProjectScript: runProjectScriptStable,
7802-
onAddProjectScript: saveProjectScriptStable,
7803-
onUpdateProjectScript: updateProjectScriptStable,
7804-
onDeleteProjectScript: deleteProjectScriptStable,
7805-
};
78067712
const panelToggleControlProps = {
78077713
terminalAvailable: activeProject !== null,
78087714
terminalOpen: terminalUiState.terminalOpen,
@@ -8472,7 +8378,58 @@ export default function ChatView(props: ChatViewProps) {
84728378
onConfirm={handleSwitchCheckoutToThreadStable}
84738379
/>
84748380

8475-
<ThreadDetailsPanel {...threadDetailsPanelProps} />
8381+
<ThreadDetailsPanel
8382+
anchor={threadPanelPopoverAnchorRef}
8383+
handle={threadPanelPopoverHandle}
8384+
onPresentationChange={setThreadPanelPresentation}
8385+
forceNewWorktree={multipleModelSelections !== null}
8386+
environmentId={activeThread.environmentId}
8387+
threadId={activeThread.id}
8388+
{...(draftId ? { draftId } : {})}
8389+
activeProjectName={activeProject?.title}
8390+
activeProjectScripts={activeProject ? activeProjectScripts : undefined}
8391+
preferredScriptId={
8392+
activeProject ? (lastInvokedScriptByProjectId[activeProject.id] ?? null) : null
8393+
}
8394+
keybindings={keybindings}
8395+
availableEditors={availableEditors}
8396+
showOpenInPicker={showOpenInPicker}
8397+
gitCwd={gitCwd}
8398+
isGitRepo={isGitRepo}
8399+
envLocked={envLocked}
8400+
availableEnvironments={logicalProjectEnvironments}
8401+
autoEnvironmentLabel={autoEnvironmentLabel}
8402+
onAutoEnvironment={
8403+
draftId &&
8404+
!envLocked &&
8405+
canAutoBalanceEnvironments &&
8406+
loadBalancingSettings.loadBalancingEnabled
8407+
? onAutoEnvironmentStable
8408+
: undefined
8409+
}
8410+
onEnvironmentChange={onEnvironmentChangeStable}
8411+
onEnvModeChange={onEnvModeChangeStable}
8412+
envMode={envMode}
8413+
{...(canOverrideServerThreadEnvMode
8414+
? {
8415+
activeThreadBranchOverride: activeThreadBranch,
8416+
onActiveThreadBranchOverrideChange: setPendingServerThreadBranch,
8417+
}
8418+
: {})}
8419+
startFromOrigin={startFromOrigin}
8420+
onStartFromOriginChange={onStartFromOriginChange}
8421+
{...(canCheckoutPullRequestIntoThread
8422+
? { onCheckoutPullRequestRequest: openPullRequestDialogStable }
8423+
: {})}
8424+
onComposerFocusRequest={scheduleComposerFocusStable}
8425+
{...(isServerThread && isGitRepo
8426+
? { onOpenChanges: openChangesFromThreadPanelStable }
8427+
: {})}
8428+
onRunProjectScript={runProjectScriptStable}
8429+
onAddProjectScript={saveProjectScriptStable}
8430+
onUpdateProjectScript={updateProjectScriptStable}
8431+
onDeleteProjectScript={deleteProjectScriptStable}
8432+
/>
84768433

84778434
<PullRequestThreadDialogHost
84788435
threadKey={activeThreadKey}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { availableScratchWorkspaceRoot } from "@t3tools/client-runtime/operations/projects";
2+
import { isScratchProject } from "@t3tools/client-runtime/state/projects";
3+
import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
4+
import {
5+
type EnvironmentId,
6+
type ProjectId,
7+
resolveEnvironmentMachineKind,
8+
} from "@t3tools/contracts";
9+
10+
import type { EnvironmentOption } from "../components/BranchToolbar.logic";
11+
import {
12+
deriveLogicalProjectKeyFromSettings,
13+
selectProjectGroupingSettings,
14+
} from "../logicalProject";
15+
import { useProjects } from "../state/entities";
16+
import { useEnvironments, usePrimaryEnvironmentId } from "../state/environments";
17+
import { useClientSettings } from "./useSettings";
18+
19+
/**
20+
* The environments a project (or a scratch draft) can move between. It reads only the
21+
* environment, project and grouping inputs, so the list keeps its identity while the thread
22+
* streams and the chat chrome it feeds can skip those renders.
23+
*/
24+
export function useLogicalProjectEnvironments(
25+
activeProject: EnvironmentProject | null,
26+
scratchDraft: boolean,
27+
): readonly EnvironmentOption[] {
28+
const { environments } = useEnvironments();
29+
const allProjects = useProjects();
30+
const primaryEnvironmentId = usePrimaryEnvironmentId();
31+
const projectGroupingSettings = useClientSettings(selectProjectGroupingSettings);
32+
33+
if (!activeProject) return [];
34+
const environmentById = new Map(
35+
environments.map((environment) => [environment.environmentId, environment]),
36+
);
37+
const envs: EnvironmentOption[] = [];
38+
const pushEnvironment = (environmentId: EnvironmentId, projectId: ProjectId | null) => {
39+
const environment = environmentById.get(environmentId) ?? null;
40+
envs.push({
41+
environmentId,
42+
projectId,
43+
label: environment?.label ?? environmentId,
44+
isPrimary: environmentId === primaryEnvironmentId,
45+
machine: resolveEnvironmentMachineKind(environment?.serverConfig ?? null),
46+
});
47+
};
48+
if (scratchDraft) {
49+
for (const environment of environments) {
50+
const scratchRoot = availableScratchWorkspaceRoot(
51+
environment.connection.phase,
52+
environment.serverConfig,
53+
);
54+
if (scratchRoot === null && environment.environmentId !== activeProject.environmentId)
55+
continue;
56+
const scratchProject =
57+
environment.environmentId === activeProject.environmentId
58+
? activeProject
59+
: allProjects.find(
60+
(p) =>
61+
p.environmentId === environment.environmentId && isScratchProject(p, scratchRoot),
62+
);
63+
pushEnvironment(environment.environmentId, scratchProject?.id ?? null);
64+
}
65+
} else {
66+
const logicalKey = deriveLogicalProjectKeyFromSettings(activeProject, projectGroupingSettings);
67+
const seen = new Set<string>();
68+
for (const p of allProjects) {
69+
if (seen.has(p.environmentId)) continue;
70+
if (deriveLogicalProjectKeyFromSettings(p, projectGroupingSettings) !== logicalKey) continue;
71+
seen.add(p.environmentId);
72+
pushEnvironment(p.environmentId, p.id);
73+
}
74+
}
75+
envs.sort((a, b) => {
76+
if (a.isPrimary !== b.isPrimary) return a.isPrimary ? -1 : 1;
77+
return a.label.localeCompare(b.label);
78+
});
79+
return envs;
80+
}

0 commit comments

Comments
 (0)