Skip to content

Commit 86fa9e0

Browse files
t3dotggfixfonclaude
authored
fix(web): right panel launcher letters open their surface instead of typing into the composer (pingdotgg#22898)
Co-authored-by: fixfon <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 159f2b8 commit 86fa9e0

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ export function eventPathContainsSelector(event: Event, selector: string): boole
4747
}
4848

4949
const SECRET_REQUEST_SELECTOR = '[data-v2-item-type="secret_request"]';
50+
const SURFACE_LAUNCHER_KEYS_ATTRIBUTE = "data-surface-launcher-keys";
51+
52+
// The right-panel launcher registers its own window capture listener when it
53+
// mounts. When the panel opens after page load that listener runs after ours,
54+
// so we must leave its letters alone or the composer takes them first.
55+
function isSurfaceLauncherKey(key: string): boolean {
56+
const launcherKeys = document
57+
.querySelector(`[${SURFACE_LAUNCHER_KEYS_ATTRIBUTE}]`)
58+
?.getAttribute(SURFACE_LAUNCHER_KEYS_ATTRIBUTE);
59+
return launcherKeys?.toLowerCase().includes(key.toLowerCase()) ?? false;
60+
}
5061

5162
export function shouldRedirectInputToComposer(event: Event): boolean {
5263
if (event.defaultPrevented) return false;
@@ -64,6 +75,7 @@ export function shouldTypeToFocusComposer(event: KeyboardEvent): boolean {
6475
if (event.metaKey || event.ctrlKey || event.altKey) return false;
6576
if (event.key.length !== 1) return false;
6677
if (!shouldRedirectInputToComposer(event)) return false;
78+
if (isSurfaceLauncherKey(event.key)) return false;
6779

6880
return true;
6981
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { useEffect } from "react";
2+
import { expect, it } from "vite-plus/test";
3+
import { userEvent } from "vite-plus/test/browser";
4+
5+
import { render } from "../../../test/render";
6+
import { useChatShortcuts } from "./useChatShortcuts";
7+
8+
const shortcutContext = {
9+
terminalFocus: false,
10+
terminalOpen: false,
11+
previewFocus: false,
12+
previewOpen: false,
13+
isWeb: true,
14+
isDesktop: false,
15+
modelPickerOpen: false,
16+
};
17+
18+
const focusOnMount = (element: HTMLElement | null) => element?.focus();
19+
20+
// Stands in for the right panel's launcher, which needs the whole app around it. Like the
21+
// real one it takes focus, advertises its letters in `data-surface-launcher-keys`, and
22+
// claims one from its own window capture listener unless the key was already handled.
23+
function Launcher(props: { keys: string; received: string[] }) {
24+
useEffect(() => {
25+
const claim = (event: KeyboardEvent) => {
26+
if (event.defaultPrevented) return;
27+
if (!props.keys.toLowerCase().split("").includes(event.key.toLowerCase())) return;
28+
event.preventDefault();
29+
props.received.push(event.key);
30+
};
31+
window.addEventListener("keydown", claim, true);
32+
return () => window.removeEventListener("keydown", claim, true);
33+
}, [props.keys, props.received]);
34+
return (
35+
<div
36+
ref={focusOnMount}
37+
tabIndex={0}
38+
aria-label="Open a surface"
39+
data-surface-launcher-keys={props.keys}
40+
/>
41+
);
42+
}
43+
44+
// The chat's shortcut listener registers when the chat mounts, so a launcher that opens
45+
// later registers after it. Tests mount the chat first and add the launcher by rerendering.
46+
function Chat(props: { composerKeys: string[]; launcher?: { keys: string; received: string[] } }) {
47+
useChatShortcuts({
48+
enabled: true,
49+
keybindings: [],
50+
scriptKeybindings: [],
51+
getShortcutContext: () => shortcutContext,
52+
threadFindActive: false,
53+
typeIntoComposer: (key) => {
54+
props.composerKeys.push(key);
55+
return true;
56+
},
57+
closeThreadFind: () => {},
58+
commands: {},
59+
fallback: () => undefined,
60+
});
61+
return props.launcher ? <Launcher {...props.launcher} /> : null;
62+
}
63+
64+
// router: false because the chat shortcuts and the launcher stand-in never navigate.
65+
const options = { router: false } as const;
66+
67+
it("leaves the launcher's letters to the launcher, in either case", async () => {
68+
const composerKeys: string[] = [];
69+
const received: string[] = [];
70+
await using screen = await render(<Chat composerKeys={composerKeys} />, options);
71+
await screen.rerender(<Chat composerKeys={composerKeys} launcher={{ keys: "BT", received }} />);
72+
73+
await userEvent.keyboard("tT");
74+
75+
expect(received).toEqual(["t", "T"]);
76+
expect(composerKeys).toEqual([]);
77+
});
78+
79+
it("still types other letters into the composer while the launcher is open", async () => {
80+
const composerKeys: string[] = [];
81+
const received: string[] = [];
82+
await using screen = await render(<Chat composerKeys={composerKeys} />, options);
83+
await screen.rerender(<Chat composerKeys={composerKeys} launcher={{ keys: "BT", received }} />);
84+
85+
await userEvent.keyboard("a");
86+
87+
expect(composerKeys).toEqual(["a"]);
88+
expect(received).toEqual([]);
89+
});

0 commit comments

Comments
 (0)