Skip to content

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

fix(find): Enter or Escape that picks an IME candidate no longer jumps to a match or closes find

hadan89772d7036aec25a
diff --git a/src/components/ChatFindBar.ime.test.ts b/src/components/ChatFindBar.ime.test.ts
new file mode 100644
index 0000000000..80623ad356
--- /dev/null
+++ b/src/components/ChatFindBar.ime.test.ts
@@ -0,0 +1,72 @@
+// @vitest-environment happy-dom
+import { createElement } from "react";
+import { flushSync } from "react-dom";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const fixture = vi.hoisted(() => ({ landOnSearchHit: vi.fn(async () => undefined) }));
+vi.mock("@/lib/focus-message", () => ({ landOnSearchHit: fixture.landOnSearchHit }));
+vi.mock("@/state/store", async (importOriginal) => {
+ const original = await importOriginal<typeof import("@/state/store")>();
+ return {
+ ...original,
+ api: vi.fn(async () => ({ hits: [] })),
+ useStore: () => ({ state: original.initialState, dispatch: vi.fn() }),
+ };
+});
+
+import { ChatFindBar } from "./ChatFindBar";
+
+let root: Root;
+let host: HTMLDivElement;
+let onClose: ReturnType<typeof vi.fn>;
+beforeEach(() => {
+ host = document.createElement("div");
+ document.body.append(host);
+ root = createRoot(host);
+ onClose = vi.fn();
+});
+afterEach(() => {
+ flushSync(() => root.unmount());
+ host.remove();
+ vi.clearAllMocks();
+});
+
+function render() {
+ flushSync(() => root.render(createElement(ChatFindBar, { threadId: "ime-thread", onClose })));
+ return host.querySelector<HTMLInputElement>("input")!;
+}
+
+function press(input: HTMLInputElement, key: string, init: KeyboardEventInit = {}) {
+ const event = new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true, ...init });
+ flushSync(() => input.dispatchEvent(event));
+ return event;
+}
+
+describe("Find in chat with an input method", () => {
+ it.each([
+ { name: "composition", init: { isComposing: true } },
+ { name: "IME key code", init: { keyCode: 229 } },
+ ])("leaves the Enter that belongs to the $name to the input method", ({ init }) => {
+ const input = render();
+ const event = press(input, "Enter", init);
+ expect(event.defaultPrevented).toBe(false);
+ });
+
+ it("keeps the find bar open when Escape only dismisses the candidate list", () => {
+ const input = render();
+ const onDocumentKey = vi.fn();
+ document.addEventListener("keydown", onDocumentKey);
+ press(input, "Escape", { isComposing: true });
+ document.removeEventListener("keydown", onDocumentKey);
+ expect(onClose).not.toHaveBeenCalled();
+ expect(onDocumentKey).not.toHaveBeenCalled();
+ });
+
+ it("still closes on Escape and takes Enter once the text is committed", () => {
+ const input = render();
+ expect(press(input, "Enter").defaultPrevented).toBe(true);
+ press(input, "Escape");
+ expect(onClose).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/src/components/ChatFindBar.tsx b/src/components/ChatFindBar.tsx
index 224118f3f6..52c9564fb3 100644
--- a/src/components/ChatFindBar.tsx
+++ b/src/components/ChatFindBar.tsx
@@ -3,6 +3,7 @@ import { ChevronDown, ChevronUp, Search, X } from "lucide-react";
import { landOnSearchHit } from "@/lib/focus-message";
import { t } from "@/lib/i18n";
+import { imeComposing } from "@/lib/send-key";
import type { SearchHit } from "@/lib/search-hit";
import { useShowThreads } from "@/lib/thread-preferences";
import { api, useStore } from "@/state/store";
@@ -79,6 +80,7 @@ export function ChatFindBar({ threadId, onClose }: { threadId: string; onClose:
value={query}
onChange={(event) => setQuery(event.target.value)}
onKeyDown={(event) => {
+ if (imeComposing(event.nativeEvent)) { event.stopPropagation(); return; }
if (event.key === "Escape") {
event.preventDefault();
onClose();