Skip to content

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

fix(sidebar): Enter or Escape that picks an IME candidate no longer saves or cancels a thread rename

Huangzhoucefb1fa57bb05f
diff --git a/src/components/SidebarThreadRow.ime.test.ts b/src/components/SidebarThreadRow.ime.test.ts
new file mode 100644
index 0000000000..b73ef1043a
--- /dev/null
+++ b/src/components/SidebarThreadRow.ime.test.ts
@@ -0,0 +1,75 @@
+// @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 { SidebarThreadRow } from "./SidebarThreadRow";
+
+let root: Root;
+let host: HTMLDivElement;
+beforeEach(() => {
+ host = document.createElement("div");
+ document.body.append(host);
+ root = createRoot(host);
+});
+afterEach(() => {
+ flushSync(() => root.unmount());
+ host.remove();
+});
+
+const IME = [
+ { name: "composition", init: { isComposing: true } },
+ { name: "IME key code", init: { keyCode: 229 } },
+];
+
+function startRenaming() {
+ const onRename = vi.fn();
+ flushSync(() => root.render(createElement(SidebarThreadRow, {
+ task: { threadId: "t1", title: "Weekly notes" }, ownerId: "scout", current: false,
+ onSelect: vi.fn(), onRename, onDelete: vi.fn(), locale: "en",
+ })));
+ const row = host.querySelector<HTMLButtonElement>("[data-sidebar-thread-row]")!;
+ flushSync(() => row.dispatchEvent(new MouseEvent("dblclick", { bubbles: true })));
+ const input = host.querySelector("input")!;
+ Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(input, "周报 ni");
+ flushSync(() => input.dispatchEvent(new Event("input", { bubbles: true })));
+ return { input, onRename };
+}
+
+function press(target: Element, key: string, init: KeyboardEventInit = {}) {
+ const event = new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true, ...init });
+ flushSync(() => target.dispatchEvent(event));
+ return event;
+}
+
+describe("renaming a sidebar thread with an input method", () => {
+ it.each(IME)("keeps editing when Enter or Escape belongs to the $name", ({ init }) => {
+ const { input, onRename } = startRenaming();
+ const outside = vi.fn();
+ document.addEventListener("keydown", outside);
+ try {
+ press(input, "Enter", init);
+ press(input, "Escape", init);
+ expect(onRename).not.toHaveBeenCalled();
+ expect(host.querySelector("input")).toBe(input);
+ expect(input.value).toBe("周报 ni");
+ expect(outside).not.toHaveBeenCalled();
+ } finally {
+ document.removeEventListener("keydown", outside);
+ }
+ });
+
+ it("still saves on a plain Enter", () => {
+ const { input, onRename } = startRenaming();
+ press(input, "Enter");
+ expect(onRename).toHaveBeenCalledWith(expect.objectContaining({ threadId: "t1" }), "周报 ni");
+ });
+
+ it("still cancels on a plain Escape", () => {
+ const { input, onRename } = startRenaming();
+ press(input, "Escape");
+ expect(onRename).not.toHaveBeenCalled();
+ expect(host.querySelector("input")).toBeNull();
+ });
+});
diff --git a/src/components/SidebarThreadRow.tsx b/src/components/SidebarThreadRow.tsx
index f9f1400e9d..661a3cfe23 100644
--- a/src/components/SidebarThreadRow.tsx
+++ b/src/components/SidebarThreadRow.tsx
@@ -7,6 +7,7 @@ import { MENU_SHEET } from "./menu-recipe";
import { useHeldMenuMotion } from "./MenuMotion";
import { intlLocale, t } from "@/lib/i18n";
import { nextRename } from "@/lib/rename";
+import { imeComposing } from "@/lib/send-key";
import { isTickable, opensRowMenuFromKey } from "@/lib/bulk-select";
import { threadRefUrl } from "@/lib/thread-refs";
import { copyText } from "@/lib/copy-text";
@@ -316,7 +317,7 @@ export const SidebarThreadRow = memo(function SidebarThreadRow({ task, ownerId,
<div className={cn("group/thread relative flex min-w-0 items-center rounded-md", selectMode && checked ? "bg-accent/10" : current ? "bg-raised" : "hover:bg-raised/50", panel && cn("items-start rounded-[8px]", current ? "bg-ink/[0.07]" : "hover:bg-ink/[0.04]"))}>
{renaming ? <input autoFocus value={draft} maxLength={80} aria-label={t("task.renameAria")}
onFocus={(event) => event.currentTarget.select()} onChange={(event) => setDraft(event.target.value)} onBlur={() => finishRename(true)}
- onKeyDown={(event) => { if (event.key === "Enter" && !event.nativeEvent.isComposing) { event.preventDefault(); finishRename(true); } else if (event.key === "Escape") { event.preventDefault(); event.stopPropagation(); finishRename(false); } }}
+ onKeyDown={(event) => { if (imeComposing(event.nativeEvent)) { event.stopPropagation(); return; } if (event.key === "Enter") { event.preventDefault(); finishRename(true); } else if (event.key === "Escape") { event.preventDefault(); event.stopPropagation(); finishRename(false); } }}
dir="auto" className="m-1 min-w-0 flex-1 rounded border border-accent/50 bg-inset px-2 py-1 text-[13px] text-ink outline-none" /> : <button
type="button" data-sidebar-thread-row={task.threadId} aria-current={current ? "page" : undefined}
title={[displayTitle, updatedStamp, status, closed ? t("task.closed") : null, archived ? t("task.archived") : null, snoozed ? t("task.snoozed") : null, task.unread ? t("task.unread") : null].filter(Boolean).join(" · ")}