Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(new-chat): Enter or Escape that picks an IME candidate no longer starts a chat or closes the + panel

Huangzhouce19ad825dfab8
diff --git a/src/components/SidebarNewChatPanel.test.ts b/src/components/SidebarNewChatPanel.test.ts
index 59b76ff27d..2bdd03402b 100644
--- a/src/components/SidebarNewChatPanel.test.ts
+++ b/src/components/SidebarNewChatPanel.test.ts
@@ -159,3 +159,49 @@ describe("SidebarNewChatPanel", () => {
expect(props.onClose).toHaveBeenCalledTimes(2);
});
});
+
+describe("SidebarNewChatPanel with an input method", () => {
+ const composing = [
+ { name: "composition", init: { isComposing: true } },
+ { name: "IME key code", init: { keyCode: 229 } },
+ ];
+ let root: Root | undefined;
+ let container: HTMLElement | undefined;
+
+ afterEach(() => {
+ flushSync(() => root?.unmount());
+ container?.remove();
+ root = undefined;
+ fixture.dispatch.mockReset();
+ });
+
+ function open() {
+ const props = { anchor: null, onClose: vi.fn(), onNewBot: vi.fn(), onNewGroup: vi.fn() };
+ container = document.createElement("div");
+ document.body.append(container);
+ root = createRoot(container);
+ flushSync(() => root!.render(createElement(SidebarNewChatPanel, props)));
+ return { props, input: container.querySelector("input")! };
+ }
+ const press = (target: Element, key: string, init: KeyboardEventInit) =>
+ flushSync(() => { target.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true, ...init })); });
+
+ it.each(composing)("does not start a chat or close on keys that belong to the $name", ({ init }) => {
+ const { props, input } = open();
+ const outside = vi.fn();
+ window.addEventListener("keydown", outside);
+ try {
+ flushSync(() => {
+ Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(input, "qu");
+ input.dispatchEvent(new Event("input", { bubbles: true }));
+ });
+ press(input, "Enter", init);
+ press(input, "Escape", init);
+ expect(fixture.dispatch).not.toHaveBeenCalled();
+ expect(props.onClose).not.toHaveBeenCalled();
+ expect(outside).not.toHaveBeenCalled();
+ } finally {
+ window.removeEventListener("keydown", outside);
+ }
+ });
+});
diff --git a/src/components/SidebarNewChatPanel.tsx b/src/components/SidebarNewChatPanel.tsx
index 57c6a01261..e049f6b4ab 100644
--- a/src/components/SidebarNewChatPanel.tsx
+++ b/src/components/SidebarNewChatPanel.tsx
@@ -2,6 +2,7 @@ import { useLayoutEffect, useRef, useState, type CSSProperties } from "react";
import { Plus, Users, X } from "lucide-react";
import { openBot, useStore, type Bot } from "@/state/store";
import { rankByName } from "@/lib/palette-rank";
+import { imeComposing } from "@/lib/send-key";
import { useShowThreads } from "@/lib/thread-preferences";
import { usePopoverDismiss } from "@/hooks/use-popover-dismiss";
import { t } from "@/lib/i18n";
@@ -105,7 +106,8 @@ export function SidebarNewChatPanel({
};
const onKeyDown = (event: React.KeyboardEvent) => {
- if (event.nativeEvent.isComposing) return;
+ // Enter and Escape that pick or cancel an input-method candidate belong to the IME, not to this list or the panel.
+ if (imeComposing(event.nativeEvent)) { event.stopPropagation(); return; }
if (event.key === "ArrowDown") {
event.preventDefault();
setCursor(selected < 0 ? 0 : (selected + 1) % count);