Skip to content

Commit 89d0c5a

Browse files
committed
fix(web): keep workspace open when dismissing foreground errors
1 parent 6ac3624 commit 89d0c5a

6 files changed

Lines changed: 230 additions & 7 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,7 @@ export default function ChatView(props: ChatViewProps) {
19351935
const isMobileViewport = useMediaQuery("max-sm");
19361936
const [workspaceLayoutRef, workspaceLayoutWidth] = useElementWidth<HTMLDivElement>();
19371937
const threadPanelPopoverAnchorRef = useRef<HTMLElement | null>(null);
1938+
const errorBannersRef = useRef<HTMLDivElement | null>(null);
19381939
// Tracks whether the user explicitly dismissed the sidebar for the active turn.
19391940
// When set, the thread-change reset effect will open the sidebar instead of closing it.
19401941
// Used by "Implement in a new thread" to carry the sidebar-open intent across navigation.
@@ -11036,6 +11037,7 @@ export default function ChatView(props: ChatViewProps) {
1103611037
) : null;
1103711038
const threadDetailsPanelProps: ThreadDetailsPanelProps = {
1103811039
anchor: threadPanelPopoverAnchorRef,
11040+
errorBannersRef,
1103911041
handle: threadPanelPopoverHandle,
1104011042
onPresentationChange: setThreadPanelPresentation,
1104111043
forceNewWorktree: multipleModelSelections !== null,
@@ -11099,6 +11101,7 @@ export default function ChatView(props: ChatViewProps) {
1109911101
threadPanelOpen,
1110011102
threadPanelPresentation,
1110111103
threadPanelPopoverHandle,
11104+
errorBannersRef,
1110211105
threadPanelShortcutLabel: shortcutLabelForCommand(keybindings, "threadPanel.toggle"),
1110311106
threadPanelHasAttention:
1110411107
activeEnvironmentUnavailableState !== null || showVersionMismatchBanner,
@@ -11268,7 +11271,10 @@ export default function ChatView(props: ChatViewProps) {
1126811271
</div>
1126911272
) : null}
1127011273
{/* Keep banners above workspace panels and below dialogs without shifting the timeline. */}
11271-
<div className="pointer-events-none absolute inset-x-0 top-0 z-[49] flex flex-col">
11274+
<div
11275+
ref={errorBannersRef}
11276+
className="pointer-events-none absolute inset-x-0 top-0 z-[49] flex flex-col"
11277+
>
1127211278
<ProviderStatusBanner
1127311279
status={visibleProviderStatus}
1127411280
onDismiss={() => setDismissedProviderStatusBannerKey(providerStatusBannerKey)}

‎apps/web/src/components/chat/PanelLayoutControls.tsx‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { PanelBottomIcon, PanelRightIcon, SquareMenuIcon } from "lucide-react";
22
import { Maximize2, Minimize2 } from "lucide";
33
import { MorphIcon } from "~/components/MorphIcon";
4-
import { memo, type ReactElement } from "react";
4+
import { memo, type ReactElement, type RefObject } from "react";
55

66
import type { ThreadPanelPresentation } from "../../rightPanelLayout";
77
import { PopoverCreateHandle, PopoverTrigger } from "../ui/popover";
@@ -18,6 +18,7 @@ export interface PanelLayoutControlsProps {
1818
threadPanelOpen: boolean;
1919
threadPanelPresentation: ThreadPanelPresentation;
2020
threadPanelPopoverHandle?: ReturnType<typeof PopoverCreateHandle>;
21+
errorBannersRef?: RefObject<HTMLElement | null>;
2122
threadPanelShortcutLabel: string | null;
2223
threadPanelHasAttention: boolean;
2324
rightPanelAvailable: boolean;
@@ -39,6 +40,7 @@ export const PanelLayoutControls = memo(function PanelLayoutControls({
3940
threadPanelOpen,
4041
threadPanelPresentation,
4142
threadPanelPopoverHandle,
43+
errorBannersRef,
4244
threadPanelShortcutLabel,
4345
threadPanelHasAttention,
4446
rightPanelAvailable,
@@ -87,7 +89,15 @@ export const PanelLayoutControls = memo(function PanelLayoutControls({
8789
{showThreadPanelControl
8890
? threadPanelPresentation === "popover"
8991
? threadPanelTooltip(
90-
<PopoverTrigger handle={threadPanelPopoverHandle} render={threadPanelToggle} />,
92+
<PopoverTrigger
93+
handle={threadPanelPopoverHandle}
94+
render={threadPanelToggle}
95+
onBlurCapture={(event) => {
96+
if (errorBannersRef?.current?.contains(event.relatedTarget)) {
97+
event.stopPropagation();
98+
}
99+
}}
100+
/>,
91101
)
92102
: threadPanelTooltip(threadPanelToggle)
93103
: null}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
// @vitest-environment jsdom
2+
3+
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
4+
import { act, useRef, useState } from "react";
5+
import { createRoot, type Root } from "react-dom/client";
6+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
7+
8+
import { useRightPanelStore } from "../../rightPanelStore";
9+
import { PopoverCreateHandle } from "../ui/popover";
10+
import { PanelLayoutControls } from "./PanelLayoutControls";
11+
import { ThreadDetailsCard } from "./ThreadDetailsCard";
12+
13+
const threadRef = {
14+
environmentId: EnvironmentId.make("banner-dismissal"),
15+
threadId: ThreadId.make("thread"),
16+
};
17+
const noop = () => {};
18+
let root: Root;
19+
let container: HTMLDivElement;
20+
21+
function Fixture() {
22+
const anchor = useRef<HTMLDivElement>(null);
23+
const errorBannersRef = useRef<HTMLDivElement>(null);
24+
const [handle] = useState(PopoverCreateHandle);
25+
const [error, setError] = useState(true);
26+
return (
27+
<>
28+
<div ref={anchor}>
29+
<PanelLayoutControls
30+
terminalAvailable={false}
31+
terminalOpen={false}
32+
terminalShortcutLabel={null}
33+
threadPanelOpen={true}
34+
threadPanelPresentation="popover"
35+
threadPanelPopoverHandle={handle}
36+
errorBannersRef={errorBannersRef}
37+
threadPanelShortcutLabel={null}
38+
threadPanelHasAttention={false}
39+
rightPanelAvailable={false}
40+
rightPanelOpen={false}
41+
rightPanelShortcutLabel={null}
42+
onToggleTerminal={noop}
43+
onToggleThreadPanel={noop}
44+
onToggleRightPanel={noop}
45+
showTerminalControl={false}
46+
showRightPanelControl={false}
47+
/>
48+
</div>
49+
<div ref={errorBannersRef}>
50+
{error && (
51+
<div role="alert">
52+
<span>Provider unavailable</span>
53+
<button onClick={() => setError(false)} aria-label="Dismiss error">
54+
<svg>
55+
<path />
56+
</svg>
57+
</button>
58+
</div>
59+
)}
60+
</div>
61+
<button>Outside</button>
62+
<ThreadDetailsCard
63+
threadRef={threadRef}
64+
anchor={anchor}
65+
errorBannersRef={errorBannersRef}
66+
handle={handle}
67+
onPresentationChange={noop}
68+
>
69+
{() => <button>Project actions</button>}
70+
</ThreadDetailsCard>
71+
</>
72+
);
73+
}
74+
75+
beforeEach(async () => {
76+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
77+
Object.defineProperty(Element.prototype, "getAnimations", {
78+
configurable: true,
79+
value: () => [],
80+
});
81+
vi.stubGlobal(
82+
"ResizeObserver",
83+
class {
84+
observe() {}
85+
unobserve() {}
86+
disconnect() {}
87+
},
88+
);
89+
useRightPanelStore.setState({ threadPanelVisibilityByThreadKey: {} });
90+
container = document.createElement("div");
91+
document.body.append(container);
92+
root = createRoot(container);
93+
await act(() => root.render(<Fixture />));
94+
await click(button("Toggle thread details panel"));
95+
expect(panel()).not.toBeNull();
96+
});
97+
98+
afterEach(async () => {
99+
await act(() => root.unmount());
100+
container.remove();
101+
Reflect.deleteProperty(Element.prototype, "getAnimations");
102+
vi.unstubAllGlobals();
103+
});
104+
105+
function panel() {
106+
return document.querySelector(
107+
'[data-slot="popover-popup"][data-open] [data-thread-details-panel="popover"]',
108+
);
109+
}
110+
111+
function button(label: string) {
112+
return Array.from(document.querySelectorAll("button")).find(
113+
(element) => (element.getAttribute("aria-label") ?? element.textContent) === label,
114+
)!;
115+
}
116+
117+
async function click(element: Element) {
118+
await act(() => {
119+
const pointerDown = new MouseEvent("pointerdown", { bubbles: true });
120+
Object.defineProperty(pointerDown, "pointerType", { value: "mouse" });
121+
element.dispatchEvent(pointerDown);
122+
element.dispatchEvent(new MouseEvent("mousedown", { bubbles: true }));
123+
element.closest("button")?.focus();
124+
element.dispatchEvent(new MouseEvent("mouseup", { bubbles: true }));
125+
element.dispatchEvent(new MouseEvent("pointerup", { bubbles: true }));
126+
element.dispatchEvent(new MouseEvent("click", { bubbles: true }));
127+
});
128+
}
129+
130+
describe("workspace panel and foreground errors", () => {
131+
it("dismisses the error through its icon without closing the panel", async () => {
132+
await click(button("Dismiss error").querySelector("path")!);
133+
expect(document.querySelector('[role="alert"]')).toBeNull();
134+
expect(panel()).not.toBeNull();
135+
});
136+
137+
it("keeps the panel open for banner text, but closes for other outside clicks", async () => {
138+
await click(document.querySelector('[role="alert"] span')!);
139+
expect(panel()).not.toBeNull();
140+
await click(button("Outside"));
141+
expect(panel()).toBeNull();
142+
});
143+
144+
it("allows focus to move to the error control without closing the panel", async () => {
145+
await act(() => button("Project actions").focus());
146+
await act(() => button("Dismiss error").focus());
147+
expect(document.activeElement).toBe(button("Dismiss error"));
148+
expect(panel()).not.toBeNull();
149+
await act(() => button("Dismiss error").click());
150+
expect(document.querySelector('[role="alert"]')).toBeNull();
151+
expect(panel()).not.toBeNull();
152+
});
153+
154+
it("still closes on Escape while an error control has focus", async () => {
155+
await act(() => button("Dismiss error").focus());
156+
await act(() =>
157+
document.activeElement!.dispatchEvent(
158+
new KeyboardEvent("keydown", { key: "Escape", bubbles: true }),
159+
),
160+
);
161+
expect(panel()).toBeNull();
162+
expect(document.querySelector('[role="alert"]')).not.toBeNull();
163+
});
164+
165+
it("returns focus to the workspace trigger on Escape after dismissing an error", async () => {
166+
await act(() => button("Project actions").focus());
167+
await click(button("Dismiss error"));
168+
expect(panel()).not.toBeNull();
169+
await act(() => button("Project actions").focus());
170+
await act(() =>
171+
document.activeElement!.dispatchEvent(
172+
new KeyboardEvent("keydown", { key: "Escape", bubbles: true }),
173+
),
174+
);
175+
await act(() => new Promise<void>((resolve) => requestAnimationFrame(() => resolve())));
176+
expect(panel()).toBeNull();
177+
expect(document.activeElement).toBe(button("Toggle thread details panel"));
178+
});
179+
180+
it("keeps the workspace open when the error is dismissed from trigger focus", async () => {
181+
await act(() => button("Toggle thread details panel").focus());
182+
await click(button("Dismiss error"));
183+
expect(document.querySelector('[role="alert"]')).toBeNull();
184+
expect(panel()).not.toBeNull();
185+
});
186+
});

‎apps/web/src/components/chat/ThreadDetailsCard.tsx‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import {
1515
export function ThreadDetailsCard({
1616
threadRef,
1717
anchor,
18+
errorBannersRef,
1819
handle,
1920
onPresentationChange,
2021
children,
2122
}: {
2223
threadRef: ScopedThreadRef;
2324
anchor: RefObject<Element | null>;
25+
errorBannersRef: RefObject<HTMLElement | null>;
2426
handle: ReturnType<typeof PopoverCreateHandle>;
2527
onPresentationChange: (presentation: ThreadPanelPresentation) => void;
2628
children: (density: "full" | "compact" | "essential") => ReactNode;
@@ -136,9 +138,20 @@ export function ThreadDetailsCard({
136138
<Popover
137139
handle={handle}
138140
open={mode === "popover" && popoverOpen}
139-
onOpenChange={(open) =>
140-
useRightPanelStore.getState().setThreadPanelOpen(threadRef, "popover", open)
141-
}
141+
onOpenChange={(open, details) => {
142+
const target = details.event.target;
143+
// Foreground error controls should not dismiss the workspace underneath them.
144+
if (
145+
!open &&
146+
details.reason === "outside-press" &&
147+
target instanceof Node &&
148+
errorBannersRef.current?.contains(target)
149+
) {
150+
details.cancel();
151+
return;
152+
}
153+
useRightPanelStore.getState().setThreadPanelOpen(threadRef, "popover", open);
154+
}}
142155
>
143156
{placement ? (
144157
inlineOpen ? (
@@ -159,6 +172,12 @@ export function ThreadDetailsCard({
159172
) : null
160173
) : (
161174
<PopoverPopup
175+
onBlurCapture={(event) => {
176+
// Base UI disables focus return before requesting a focus-out close, even if canceled.
177+
if (errorBannersRef.current?.contains(event.relatedTarget)) {
178+
event.stopPropagation();
179+
}
180+
}}
162181
anchor={anchor}
163182
align="end"
164183
alignOffset={0}

‎apps/web/src/components/chat/ThreadDetailsPanel.test.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ describe("ThreadDetailsPanel", () => {
5454

5555
const props: ThreadDetailsPanelProps = {
5656
anchor: { current: null },
57+
errorBannersRef: { current: null },
5758
handle: PopoverCreateHandle(),
5859
onPresentationChange: vi.fn(),
5960
environmentId,

‎apps/web/src/components/chat/ThreadDetailsPanel.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ interface VersionMismatchIssue {
3737

3838
export interface ThreadDetailsPanelProps extends Pick<
3939
ComponentProps<typeof ThreadDetailsCard>,
40-
"anchor" | "handle" | "onPresentationChange"
40+
"anchor" | "errorBannersRef" | "handle" | "onPresentationChange"
4141
> {
4242
forceNewWorktree?: boolean;
4343
environmentId: EnvironmentId;
@@ -117,6 +117,7 @@ export function ThreadDetailsPanel(props: ThreadDetailsPanelProps) {
117117
<ThreadDetailsCard
118118
threadRef={{ environmentId: props.environmentId, threadId: props.threadId }}
119119
anchor={props.anchor}
120+
errorBannersRef={props.errorBannersRef}
120121
handle={props.handle}
121122
onPresentationChange={props.onPresentationChange}
122123
>

0 commit comments

Comments
 (0)