Skip to content

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

fix(ask): don't send a question's answer on the input method's confirming Enter

hadan8977d5395bc13ffd
diff --git a/src/components/AskCard.tsx b/src/components/AskCard.tsx
index e6e3e51bc8..85717d7825 100644
--- a/src/components/AskCard.tsx
+++ b/src/components/AskCard.tsx
@@ -4,6 +4,7 @@ import { botButton, type BotButton } from "@/lib/bot-button";
import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
import type { MausColor } from "@/lib/mascot";
+import { imeComposing } from "@/lib/send-key";
import type { Bot } from "@/state/store";
import { BotAvatar } from "./Avatar";
import { ELEVATED_CARD, ELEVATED_FLOAT } from "./elevated";
@@ -198,7 +199,7 @@ export function AskReply({ label, placeholder, color, onSend, sending = false, v
disabled={sending}
onChange={(event) => onChange(event.target.value)}
onKeyDown={(event) => {
- if (event.key !== "Enter" || event.shiftKey || event.nativeEvent.isComposing) return;
+ if (event.key !== "Enter" || event.shiftKey || imeComposing(event.nativeEvent)) return;
event.preventDefault();
send();
}}
diff --git a/src/components/AskReply.ime.test.ts b/src/components/AskReply.ime.test.ts
new file mode 100644
index 0000000000..31b5e18908
--- /dev/null
+++ b/src/components/AskReply.ime.test.ts
@@ -0,0 +1,81 @@
+// @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";
+
+import type { InboxRow } from "../../shared/inbox";
+import { AskReply } from "./AskCard";
+import { InboxQuestionAnswer } from "./inbox/InboxQuestionAnswer";
+
+let root: Root;
+let host: HTMLDivElement;
+beforeEach(() => {
+ host = document.createElement("div");
+ document.body.append(host);
+ root = createRoot(host);
+});
+afterEach(() => {
+ flushSync(() => root.unmount());
+ host.remove();
+});
+
+function press(field: HTMLTextAreaElement, init: KeyboardEventInit = {}) {
+ const event = new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true, ...init });
+ flushSync(() => field.dispatchEvent(event));
+ return event;
+}
+
+function type(field: HTMLTextAreaElement, text: string) {
+ Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, "value")!.set!.call(field, text);
+ flushSync(() => field.dispatchEvent(new Event("input", { bubbles: true })));
+}
+
+const IME = [
+ { name: "composition", init: { isComposing: true } },
+ { name: "IME key code", init: { keyCode: 229 } },
+];
+
+describe("answering a bot's question with an input method", () => {
+ function renderReply() {
+ const onSend = vi.fn();
+ flushSync(() => root.render(createElement(AskReply, { label: "Answer", placeholder: "Answer", color: undefined, onSend, value: "ni hao", onChange: () => {} })));
+ return { field: host.querySelector("textarea")!, onSend };
+ }
+
+ it.each(IME)("AskReply leaves the Enter that belongs to the $name to the input method", ({ init }) => {
+ const { field, onSend } = renderReply();
+ const event = press(field, init);
+ expect(event.defaultPrevented).toBe(false);
+ expect(onSend).not.toHaveBeenCalled();
+ });
+
+ it("AskReply still sends on a plain Enter", () => {
+ const { field, onSend } = renderReply();
+ press(field);
+ expect(onSend).toHaveBeenCalledWith("ni hao");
+ });
+
+ function renderInbox() {
+ const onAnswer = vi.fn();
+ const row = { id: "row-1" } as unknown as InboxRow;
+ flushSync(() => root.render(createElement(InboxQuestionAnswer, { row, question: { question: "Which branch?", options: [] }, onAnswer })));
+ const field = host.querySelector("textarea")!;
+ type(field, "ni hao");
+ return { field, onAnswer };
+ }
+
+ it.each(IME)("the inbox answer leaves the Enter that belongs to the $name to the input method", ({ init }) => {
+ const { field, onAnswer } = renderInbox();
+ const event = press(field, init);
+ expect(event.defaultPrevented).toBe(false);
+ expect(onAnswer).not.toHaveBeenCalled();
+ });
+
+ it("the inbox answer still sends on a plain Enter", () => {
+ const { field, onAnswer } = renderInbox();
+ press(field);
+ expect(onAnswer).toHaveBeenCalledTimes(1);
+ expect(onAnswer.mock.calls[0][0]).toMatchObject({ behavior: "answer", message: "ni hao" });
+ });
+});
diff --git a/src/components/inbox/InboxQuestionAnswer.tsx b/src/components/inbox/InboxQuestionAnswer.tsx
index efd5554ee9..e05bf0d2a9 100644
--- a/src/components/inbox/InboxQuestionAnswer.tsx
+++ b/src/components/inbox/InboxQuestionAnswer.tsx
@@ -4,6 +4,7 @@ import { formatQuestionAnswers, MAX_CUSTOM_ANSWER, type AskQuestion } from "../.
import type { InboxRow } from "../../../shared/inbox";
import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
+import { imeComposing } from "@/lib/send-key";
import type { CardResponse } from "@/lib/card-answer";
import { askPrimary, ASK_ICON_SHAPE, AskError, type Asker } from "../AskCard";
import { cardAnswerFor } from "../bot-access/access-card";
@@ -109,7 +110,7 @@ export function InboxQuestionAnswer({ row, question, asker, onAnswer }: {
placeholder={question.options.length ? t("inbox.note") : t("question.answerPlaceholder")}
onChange={(event) => setNote(event.target.value)}
onKeyDown={(event) => {
- if (event.key !== "Enter" || event.shiftKey || event.nativeEvent.isComposing) return;
+ if (event.key !== "Enter" || event.shiftKey || imeComposing(event.nativeEvent)) return;
event.preventDefault();
send();
}}