Skip to content

Commit e3eb38c

Browse files
committed
fix(web): focus side-chat composer when selecting its tab
Selecting a /btw tab did not focus its input. Generic panel focus excludes the editor's nested workspace region, and selecting an already-active tab does not trigger the panel-change focus request. Request the selected side chat's editor through the existing cancellable workspace focus mechanism in the tab-activation handler. Identify the pane by thread ID so a pending request cannot target a different side chat. Add browser coverage for typing after tab clicks, switching between side chats, preserving drafts, and cancelling delayed focus after user input. Model: GPT-6-Astra. Harness: Codex in T3 Code.
1 parent 8b44f42 commit e3eb38c

4 files changed

Lines changed: 89 additions & 4 deletions

File tree

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

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import { createMemoryHistory } from "@tanstack/react-router";
2-
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
2+
import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
33
import { ThreadId } from "@t3tools/contracts";
44
import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
55
import type * as Context from "effect/Context";
66
import { expect, it, vi } from "vite-plus/test";
7-
import { page } from "vite-plus/test/browser";
7+
import { page, userEvent } from "vite-plus/test/browser";
8+
9+
import "~/index.css";
810

911
import { AppRoot } from "~/AppRoot";
1012
// The route loads ChatView lazily, and its first transform (React Compiler over ~4k lines)
1113
// takes 6-11s; importing it here keeps that out of the test's timeout.
1214
import "~/components/ChatView";
1315
import { DraftId, useComposerDraftStore } from "~/composerDraftStore";
1416
import { __setClientSettingsForTests } from "~/hooks/useSettings";
17+
import { startWorkspaceFocus } from "~/lib/workspaceFocus";
18+
import { useRightPanelStore } from "~/rightPanelStore";
1519
import { getRouter } from "~/router";
1620
import {
1721
ENVIRONMENT_ID,
1822
PROJECT_ID,
1923
installFakeEnvironment,
24+
threadWithHistory,
2025
type FakeDriver,
2126
} from "../../test/fakeEnvironment";
2227
import { render } from "../../test/render";
@@ -36,8 +41,13 @@ vi.mock(import("~/rpc/requestLatencyState"), async (original) => ({
3641
trackRpcRequestSent: () => {},
3742
}));
3843

44+
const parent = threadWithHistory("focus-parent", 1);
45+
const side = { ...threadWithHistory("focus-side", 1), sideChatOf: parent.id };
46+
const otherSide = { ...threadWithHistory("focus-other-side", 1), sideChatOf: parent.id };
47+
const THREADS = [parent, side, otherSide];
48+
3949
it("a new draft does not load its reserved thread before the server lists it", async () => {
40-
using _environment = installFakeEnvironment([]);
50+
using _environment = installFakeEnvironment(THREADS);
4151
using fetchSpy = vi.spyOn(globalThis, "fetch");
4252
__setClientSettingsForTests({
4353
...DEFAULT_CLIENT_SETTINGS,
@@ -65,3 +75,51 @@ it("a new draft does not load its reserved thread before the server lists it", a
6575
// A 404 here marks the thread deleted, and the view never loads it once the send creates it.
6676
expect(threadRequests).toEqual([]);
6777
});
78+
79+
it("accepts typing after clicking a side-chat tab without tabbing into its composer", async () => {
80+
await page.viewport(1400, 900);
81+
using _environment = installFakeEnvironment(THREADS);
82+
__setClientSettingsForTests({
83+
...DEFAULT_CLIENT_SETTINGS,
84+
onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
85+
});
86+
const parentRef = scopeThreadRef(ENVIRONMENT_ID, parent.id);
87+
useRightPanelStore.getState().openSideChat(parentRef, otherSide.id);
88+
useRightPanelStore.getState().openSideChat(parentRef, side.id);
89+
const router = getRouter(
90+
createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/${parent.id}`] }),
91+
);
92+
await router.load();
93+
const container = document.createElement("div");
94+
container.style.cssText = "height: 900px; width: 1400px;";
95+
await using screen = await render(<AppRoot router={router} />, {
96+
container,
97+
// AppRoot brings its own router.
98+
router: false,
99+
});
100+
using _focus = { [Symbol.dispose]: startWorkspaceFocus(router) };
101+
const mainEditor = screen.getByRole("textbox", { name: `Message ${parent.title}`, exact: true });
102+
const sideEditor = screen.getByRole("textbox", { name: `Message ${side.title}`, exact: true });
103+
await expect.element(sideEditor).toBeVisible();
104+
await userEvent.click(mainEditor);
105+
await userEvent.keyboard("main draft");
106+
await userEvent.click(screen.getByRole("button", { name: side.title, exact: true }));
107+
await expect.element(sideEditor).toHaveFocus();
108+
await userEvent.keyboard("side draft");
109+
await expect.element(sideEditor).toHaveTextContent("side draft");
110+
await expect.element(mainEditor).toHaveTextContent("main draft");
111+
112+
await userEvent.click(screen.getByRole("button", { name: otherSide.title, exact: true }));
113+
const otherEditor = screen.getByRole("textbox", {
114+
name: `Message ${otherSide.title}`,
115+
exact: true,
116+
});
117+
await expect.element(otherEditor).toHaveFocus();
118+
await userEvent.keyboard("other draft");
119+
await expect.element(otherEditor).toHaveTextContent("other draft");
120+
await userEvent.click(screen.getByRole("button", { name: side.title, exact: true }));
121+
await expect.element(sideEditor).toHaveFocus();
122+
await userEvent.keyboard(" again");
123+
await expect.element(sideEditor).toHaveTextContent("side draft again");
124+
await expect.element(mainEditor).toHaveTextContent("main draft");
125+
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export default function SideChatPane(props: {
123123
};
124124

125125
return (
126-
<div data-side-chat-pane className="flex h-full min-h-0 flex-col">
126+
<div data-side-chat-pane={threadId} className="flex h-full min-h-0 flex-col">
127127
<div className="flex h-9 shrink-0 items-center gap-1 border-b border-border pr-1 pl-3">
128128
<span className="min-w-0 flex-1 truncate text-xs font-medium">
129129
{shell?.title ?? "Side chat"}

‎apps/web/src/components/chat/useRightPanelActions.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ export function useRightPanelActions(input: RightPanelActionsInput) {
213213
if (surface.kind === "terminal") {
214214
focusTerminal(activeThreadRef, surface.activeTerminalId);
215215
}
216+
if (surface.kind === "side-chat") {
217+
requestWorkspaceFocus({
218+
area: "panel",
219+
selector: `[data-side-chat-pane="${CSS.escape(surface.threadId)}"] [data-workspace-area="composer"] [contenteditable="true"]`,
220+
});
221+
}
216222
if (surface.kind === "diff" && !diffOpen) {
217223
onDiffPanelOpen?.();
218224
}

‎apps/web/src/lib/workspaceFocus.browser.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, expect, it, vi } from "vite-plus/test";
2+
import { fakeTimers } from "../../test/render";
23
import { createWorkspaceFocusController } from "./workspaceFocus";
34

45
function setup() {
@@ -257,6 +258,26 @@ describe("workspace area focus", () => {
257258
await vi.waitFor(() => expect(document.activeElement).toBe(list.button));
258259
});
259260

261+
it.each(["pointerdown", "keydown"])(
262+
"cancels a delayed composer focus request after %s elsewhere",
263+
async (eventType) => {
264+
using fixture = setup();
265+
using _timers = fakeTimers();
266+
const main = fixture.area("composer", 2);
267+
const panel = fixture.area("panel", 3);
268+
main.button.focus();
269+
fixture.controller.request({ area: "panel", selector: '[contenteditable="true"]' });
270+
await Promise.resolve();
271+
main.button.dispatchEvent(new Event(eventType, { bubbles: true }));
272+
const editor = document.createElement("div");
273+
editor.contentEditable = "true";
274+
editor.textContent = "Side draft";
275+
panel.element.append(editor);
276+
await vi.runAllTimersAsync();
277+
expect(document.activeElement).toBe(main.button);
278+
},
279+
);
280+
260281
it("keeps focus on a control that React remounted elsewhere", async () => {
261282
using fixture = setup();
262283
fixture.area("composer", 2);

0 commit comments

Comments
 (0)