Skip to content

Commit 5ff5caf

Browse files
perf(web): opening chat and usage dialogs no longer re-renders the whole page
The pull request checkout dialog, device setup wizard, branch-restore and revert confirms kept their open state in ChatView, and the usage model dialog kept its selection in UsagePage. Each now lives in a keep-alive atom read only by a small host, so opening or closing one renders the host. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2fbd431 commit 5ff5caf

5 files changed

Lines changed: 372 additions & 168 deletions

File tree

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -786,11 +786,6 @@ export function collectUserMessageBlobPreviewUrls(message: ChatMessage): string[
786786
return previewUrls;
787787
}
788788

789-
export interface PullRequestDialogState {
790-
initialReference: string | null;
791-
key: number;
792-
}
793-
794789
export { readFileAsDataUrl } from "../lib/imageCompression";
795790

796791
export function resolveSendEnvMode(input: {

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

Lines changed: 41 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,6 @@ import { threadPullRequestPanelTarget } from "./pullRequest/pullRequestDetail.lo
251251
import { RightPanelTabs } from "./RightPanelTabs";
252252
import { LinkPullRequestDialogHost } from "./pullRequest/LinkPullRequestDialog";
253253
import { useDeviceState } from "~/state/device";
254-
import { DeviceSetup } from "./device/DeviceSetup";
255-
import { Dialog } from "./ui/dialog";
256-
import { WizardPopup } from "./ui/wizard";
257254
import { BranchToolbar, type BranchToolbarHandle } from "./BranchToolbar";
258255
import { makeWorkspaceFileDropHandlers } from "./chat/workspaceFileDrop";
259256
import { isEditableFocused } from "../lib/editableFocus";
@@ -387,7 +384,16 @@ import { createPageScrollController, type PageScrollKey } from "./chat/pageScrol
387384
import { isTimelineScrollTarget } from "./chat/timelineScrollTarget";
388385
import { DraftHeroHeadline } from "./chat/DraftHeroHeadline";
389386
import { ExpandedImageDialog } from "./chat/ExpandedImageDialog";
390-
import { PullRequestThreadDialog } from "./PullRequestThreadDialog";
387+
import {
388+
BranchRestoreConfirmHost,
389+
DeviceSetupDialogHost,
390+
openBranchRestoreConfirm,
391+
openDeviceSetupDialog,
392+
openPullRequestThreadDialog,
393+
openRevertConfirm,
394+
PullRequestThreadDialogHost,
395+
RevertConfirmHost,
396+
} from "./chat/ChatOverlayHosts";
391397
import type { AssistantCitationRequest } from "./chat/AssistantCitationSource";
392398
import { MessagesTimeline, type MessagesTimelineHistoryControls } from "./chat/MessagesTimeline";
393399
import { ProviderSubagentBar } from "./chat/ProviderSubagentBar";
@@ -479,7 +485,6 @@ import {
479485
LAST_INVOKED_SCRIPT_BY_PROJECT_KEY,
480486
LastInvokedScriptByProjectSchema,
481487
type LocalDispatchSnapshot,
482-
PullRequestDialogState,
483488
cloneComposerImageForRetry,
484489
deriveLockedProvider,
485490
readFileAsDataUrl,
@@ -519,15 +524,6 @@ import { useAtomCommand } from "../state/use-atom-command";
519524
import { useOrchestrationCommand } from "../state/use-orchestration-command";
520525
import { useAtomQueryRunner } from "../state/use-atom-query-runner";
521526
import { Button, InlineButton } from "./ui/button";
522-
import {
523-
AlertDialog,
524-
AlertDialogClose,
525-
AlertDialogDescription,
526-
AlertDialogFooter,
527-
AlertDialogHeader,
528-
AlertDialogPopup,
529-
AlertDialogTitle,
530-
} from "./ui/alert-dialog";
531527
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
532528
import { ServerUpdateAction } from "./ServerUpdateAction";
533529
import { useAutoBalanceUpdateBanner } from "./chat/useAutoBalanceUpdateBanner";
@@ -1328,8 +1324,6 @@ export default function ChatView(props: ChatViewProps) {
13281324
const [workspaceLayoutElement, setWorkspaceLayoutElement] = useState<HTMLDivElement | null>(null);
13291325
const threadPanelPopoverAnchorRef = useRef<HTMLElement | null>(null);
13301326
const [terminalFocusRequestId, setTerminalFocusRequestId] = useState(0);
1331-
const [pullRequestDialogState, setPullRequestDialogState] =
1332-
useState<PullRequestDialogState | null>(null);
13331327
const [terminalUiLaunchContext, setTerminalUiLaunchContext] =
13341328
useState<TerminalLaunchContext | null>(null);
13351329
const [attachmentPreviewHandoffByMessageId, setAttachmentPreviewHandoffByMessageId] = useState<
@@ -2089,17 +2083,11 @@ export default function ChatView(props: ChatViewProps) {
20892083
if (!canCheckoutPullRequestIntoThread) {
20902084
return;
20912085
}
2092-
setPullRequestDialogState({
2093-
initialReference: reference ?? null,
2094-
key: Date.now(),
2095-
});
2086+
if (activeThreadKey === null) return;
2087+
openPullRequestThreadDialog(activeThreadKey, reference);
20962088
};
20972089
const openPullRequestDialogStable = useStableCallback(openPullRequestDialog);
20982090

2099-
const closePullRequestDialog = () => {
2100-
setPullRequestDialogState(null);
2101-
};
2102-
21032091
const openOrReuseProjectDraftThread = async (input: {
21042092
branch: string;
21052093
worktreePath: string | null;
@@ -3730,11 +3718,10 @@ export default function ChatView(props: ChatViewProps) {
37303718
const { state: deviceState, loaded: deviceStateLoaded } = useDeviceState(
37313719
activeThreadRef?.environmentId ?? null,
37323720
);
3733-
const [deviceSetupThread, setDeviceSetupThread] = useState<ScopedThreadRef | null>(null);
37343721
const addDeviceSurface = () => {
37353722
if (!activeThreadRef) return;
37363723
if (!deviceState.onboardingCompleted || deviceState.hostStatus === "disabled") {
3737-
setDeviceSetupThread(activeThreadRef);
3724+
openDeviceSetupDialog(scopedThreadKey(activeThreadRef));
37383725
return;
37393726
}
37403727
useRightPanelStore.getState().open(activeThreadRef, "device");
@@ -4564,7 +4551,6 @@ export default function ChatView(props: ChatViewProps) {
45644551
if (timelineThreadReset.threadKey !== activeThreadKey) {
45654552
const followEnd = readTimelinePosition(routeThreadKey)?.atEnd !== false;
45664553
setTimelineThreadReset({ threadKey: activeThreadKey, followEnd });
4567-
setPullRequestDialogState(null);
45684554
setTimelineLiveFollowEnabled(followEnd);
45694555
setShowScrollToBottom(!followEnd);
45704556
}
@@ -4988,7 +4974,6 @@ export default function ChatView(props: ChatViewProps) {
49884974
};
49894975
const handleUnsnoozeActiveThreadStable = useStableCallback(handleUnsnoozeActiveThread);
49904976
const [isRestoringThreadBranch, setIsRestoringThreadBranch] = useState(false);
4991-
const [branchRestoreConfirmOpen, setBranchRestoreConfirmOpen] = useState(false);
49924977
const [revealedBranchMismatchKey, setRevealedBranchMismatchKey] = useState<string | null>(null);
49934978
const [, setBranchMismatchDismissTick] = useState(0);
49944979
const composerHasDraftContent = useComposerDraftStore((store) =>
@@ -5075,6 +5060,9 @@ export default function ChatView(props: ChatViewProps) {
50755060
setIsRestoringThreadBranch(false);
50765061
scheduleComposerFocus();
50775062
};
5063+
const handleSwitchCheckoutToThreadStable = useStableCallback(() => {
5064+
void handleSwitchCheckoutToThread();
5065+
});
50785066
// The interrupt path also accepts a completed run while its provider still
50795067
// has background work.
50805068
const activeBackgroundTasks = !isWorking && activeThread ? pendingBackgroundTasks : [];
@@ -5401,7 +5389,7 @@ export default function ChatView(props: ChatViewProps) {
54015389
const handleRestoreThreadBranch = () => {
54025390
if (!canWriteSourceControl) return;
54035391
if (gitStatusQuery.data?.hasWorkingTreeChanges) {
5404-
setBranchRestoreConfirmOpen(true);
5392+
if (activeThreadKey !== null) openBranchRestoreConfirm(activeThreadKey);
54055393
return;
54065394
}
54075395
void handleSwitchCheckoutToThread();
@@ -5761,16 +5749,6 @@ export default function ChatView(props: ChatViewProps) {
57615749
};
57625750
}, [activeThreadId, composerRef]);
57635751

5764-
const [pendingRevert, setPendingRevert] = useState<{
5765-
turnCount: number;
5766-
messageId: MessageId;
5767-
routeThreadKey: string;
5768-
} | null>(null);
5769-
5770-
if (pendingRevert && pendingRevert.routeThreadKey !== routeThreadKey) {
5771-
setPendingRevert(null);
5772-
}
5773-
57745752
const onRevertToTurnCount = async (
57755753
turnCount: number,
57765754
messageId: MessageId,
@@ -5819,7 +5797,7 @@ export default function ChatView(props: ChatViewProps) {
58195797
return;
58205798
}
58215799
if (restoreFiles === undefined) {
5822-
setPendingRevert({ turnCount, messageId, routeThreadKey });
5800+
openRevertConfirm(routeThreadKey, { turnCount, messageId });
58235801
return;
58245802
}
58255803

@@ -5858,6 +5836,10 @@ export default function ChatView(props: ChatViewProps) {
58585836
}
58595837
};
58605838
const onRevertToTurnCountStable = useStableCallback(onRevertToTurnCount);
5839+
const onConfirmRevert = (turnCount: number, messageId: MessageId, restoreFiles: boolean) => {
5840+
void onRevertToTurnCount(turnCount, messageId, restoreFiles);
5841+
};
5842+
const onConfirmRevertStable = useStableCallback(onConfirmRevert);
58615843

58625844
const onRollbackCheckpoint = async (input: {
58635845
readonly checkpointId: string;
@@ -7953,29 +7935,11 @@ export default function ChatView(props: ChatViewProps) {
79537935
ref={setWorkspaceLayoutElement}
79547936
className="relative flex min-h-0 min-w-0 flex-1 overflow-hidden bg-background"
79557937
>
7956-
<Dialog
7957-
open={
7958-
deviceSetupThread !== null &&
7959-
deviceSetupThread.environmentId === activeThreadRef?.environmentId &&
7960-
deviceSetupThread.threadId === activeThreadRef?.threadId
7961-
}
7962-
onOpenChange={(open) => {
7963-
if (!open) setDeviceSetupThread(null);
7964-
}}
7965-
>
7966-
<WizardPopup>
7967-
{activeThreadRef ? (
7968-
<DeviceSetup
7969-
environmentId={activeThreadRef.environmentId}
7970-
state={deviceState}
7971-
onComplete={() => {
7972-
useRightPanelStore.getState().open(activeThreadRef, "device");
7973-
setDeviceSetupThread(null);
7974-
}}
7975-
/>
7976-
) : null}
7977-
</WizardPopup>
7978-
</Dialog>
7938+
<DeviceSetupDialogHost
7939+
threadKey={activeThreadKey}
7940+
threadRef={activeThreadRef}
7941+
state={deviceState}
7942+
/>
79797943
{rightPanelControlsAtRoot ? panelLayoutControls : null}
79807944
<div
79817945
className={cn(
@@ -8561,55 +8525,22 @@ export default function ChatView(props: ChatViewProps) {
85618525
/>
85628526
) : null}
85638527

8564-
<AlertDialog open={branchRestoreConfirmOpen} onOpenChange={setBranchRestoreConfirmOpen}>
8565-
<AlertDialogPopup>
8566-
<AlertDialogHeader>
8567-
<AlertDialogTitle>
8568-
Switch to{" "}
8569-
<code className="font-medium">
8570-
{localCheckoutBranchMismatch?.threadBranch ?? ""}
8571-
</code>
8572-
?
8573-
</AlertDialogTitle>
8574-
<AlertDialogDescription>
8575-
You have uncommitted changes. They'll carry over to the other branch, or block
8576-
the switch if they conflict.
8577-
</AlertDialogDescription>
8578-
</AlertDialogHeader>
8579-
<AlertDialogFooter>
8580-
<AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
8581-
<Button
8582-
variant="default"
8583-
disabled={!canWriteSourceControl}
8584-
onClick={() => {
8585-
setBranchRestoreConfirmOpen(false);
8586-
void handleSwitchCheckoutToThread();
8587-
}}
8588-
>
8589-
Switch branch
8590-
</Button>
8591-
</AlertDialogFooter>
8592-
</AlertDialogPopup>
8593-
</AlertDialog>
8528+
<BranchRestoreConfirmHost
8529+
threadKey={activeThreadKey}
8530+
threadBranch={localCheckoutBranchMismatch?.threadBranch ?? ""}
8531+
canWriteSourceControl={canWriteSourceControl}
8532+
onConfirm={handleSwitchCheckoutToThreadStable}
8533+
/>
85948534

85958535
<ThreadDetailsPanel {...threadDetailsPanelProps} />
85968536

8597-
{pullRequestDialogState ? (
8598-
<PullRequestThreadDialog
8599-
key={pullRequestDialogState.key}
8600-
open
8601-
environmentId={activeThread.environmentId}
8602-
threadId={activeThread.id}
8603-
cwd={activeProject?.workspaceRoot ?? null}
8604-
initialReference={pullRequestDialogState.initialReference}
8605-
onOpenChange={(open) => {
8606-
if (!open) {
8607-
closePullRequestDialog();
8608-
}
8609-
}}
8610-
onPrepared={handlePreparedPullRequestThreadStable}
8611-
/>
8612-
) : null}
8537+
<PullRequestThreadDialogHost
8538+
threadKey={activeThreadKey}
8539+
environmentId={activeThread.environmentId}
8540+
threadId={activeThread.id}
8541+
cwd={activeProject?.workspaceRoot ?? null}
8542+
onPrepared={handlePreparedPullRequestThreadStable}
8543+
/>
86138544
</ThreadFindCanvas>
86148545
</div>
86158546

@@ -8736,44 +8667,7 @@ export default function ChatView(props: ChatViewProps) {
87368667
</RightPanelSheet>
87378668
) : null}
87388669

8739-
<AlertDialog
8740-
open={pendingRevert !== null && pendingRevert.routeThreadKey === routeThreadKey}
8741-
onOpenChange={(open) => {
8742-
if (!open) setPendingRevert(null);
8743-
}}
8744-
>
8745-
<AlertDialogPopup>
8746-
<AlertDialogHeader>
8747-
<AlertDialogTitle>Edit from here?</AlertDialogTitle>
8748-
<AlertDialogDescription>
8749-
Rewind chat to before this message. Your prompt and attachments return to the
8750-
composer.
8751-
</AlertDialogDescription>
8752-
</AlertDialogHeader>
8753-
<AlertDialogFooter>
8754-
<AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
8755-
<Button
8756-
variant="destructive"
8757-
onClick={() => {
8758-
if (!pendingRevert || pendingRevert.routeThreadKey !== routeThreadKey) return;
8759-
setPendingRevert(null);
8760-
void onRevertToTurnCount(pendingRevert.turnCount, pendingRevert.messageId, true);
8761-
}}
8762-
>
8763-
Revert files too
8764-
</Button>
8765-
<Button
8766-
onClick={() => {
8767-
if (!pendingRevert || pendingRevert.routeThreadKey !== routeThreadKey) return;
8768-
setPendingRevert(null);
8769-
void onRevertToTurnCount(pendingRevert.turnCount, pendingRevert.messageId, false);
8770-
}}
8771-
>
8772-
Revert and keep changes
8773-
</Button>
8774-
</AlertDialogFooter>
8775-
</AlertDialogPopup>
8776-
</AlertDialog>
8670+
<RevertConfirmHost threadKey={routeThreadKey} onConfirm={onConfirmRevertStable} />
87778671
<LinkPullRequestDialogHost />
87788672
{expandedImage && (
87798673
<ExpandedImageDialog
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { MessageId } from "@t3tools/contracts";
2+
import { act } from "react";
3+
import { createRoot, type Root } from "react-dom/client";
4+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
5+
6+
import { AppAtomRegistryProvider } from "~/rpc/atomRegistry";
7+
import { openRevertConfirm, RevertConfirmHost } from "./ChatOverlayHosts";
8+
9+
let root: Root;
10+
let container: HTMLDivElement;
11+
12+
beforeEach(() => {
13+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
14+
container = document.createElement("div");
15+
document.body.append(container);
16+
root = createRoot(container);
17+
});
18+
19+
afterEach(async () => {
20+
await act(() => root.unmount());
21+
container.remove();
22+
vi.unstubAllGlobals();
23+
});
24+
25+
const messageId = MessageId.make("message-1");
26+
const dialog = () => document.querySelector('[role="alertdialog"]');
27+
28+
describe("RevertConfirmHost", () => {
29+
it("shows only its thread's request, confirms it once, and drops it when the thread changes", async () => {
30+
const onConfirm = vi.fn();
31+
const host = (threadKey: string) => (
32+
<AppAtomRegistryProvider>
33+
<RevertConfirmHost threadKey={threadKey} onConfirm={onConfirm} />
34+
</AppAtomRegistryProvider>
35+
);
36+
await act(() => root.render(host("env:a")));
37+
38+
await act(() => openRevertConfirm("env:b", { turnCount: 2, messageId }));
39+
expect(dialog()).toBeNull();
40+
41+
await act(() => openRevertConfirm("env:a", { turnCount: 2, messageId }));
42+
const keep = [...(dialog()?.querySelectorAll("button") ?? [])].find(
43+
(button) => button.textContent === "Revert and keep changes",
44+
);
45+
await act(() => keep?.click());
46+
expect(onConfirm).toHaveBeenCalledExactlyOnceWith(2, messageId, false);
47+
await vi.waitFor(() => expect(dialog()).toBeNull());
48+
49+
await act(() => openRevertConfirm("env:a", { turnCount: 3, messageId }));
50+
expect(dialog()).not.toBeNull();
51+
await act(() => root.render(host("env:b")));
52+
await vi.waitFor(() => expect(dialog()).toBeNull());
53+
});
54+
});

0 commit comments

Comments
 (0)