Skip to content

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

fix(sidebar): Enter that picks an IME candidate no longer creates the channel or renames the team

hadan897706bd9e5170bb
diff --git a/src/components/SidebarNewRoomPanel.ime.test.ts b/src/components/SidebarNewRoomPanel.ime.test.ts
new file mode 100644
index 0000000000..689aad909e
--- /dev/null
+++ b/src/components/SidebarNewRoomPanel.ime.test.ts
@@ -0,0 +1,115 @@
+// @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 { Bot } from "@/state/store";
+
+const fixture = vi.hoisted(() => ({ dispatch: vi.fn(), api: vi.fn() }));
+vi.mock("@/state/store", async (importOriginal) => {
+ const original = await importOriginal<typeof import("@/state/store")>();
+ return {
+ ...original,
+ api: fixture.api,
+ useStore: () => ({ state: { ...original.initialState, bots: [bot], sections: ["Research"] }, dispatch: fixture.dispatch }),
+ };
+});
+vi.mock("@/lib/analytics", () => ({ track: vi.fn() }));
+
+import { NewRoomPanel } from "./SidebarNewRoomPanel";
+import { TeamDialog } from "./TeamDialog";
+
+const bot: Bot = {
+ id: "ime-bot", threadId: "ime-thread", name: "Atlas", title: "", description: "",
+ color: "blue", notifications: false, unread: false, busy: false, messages: [], section: "Research",
+ modelSelection: { instanceId: "claude", model: "fake" },
+};
+
+const composing = [
+ { name: "composition", init: { isComposing: true } },
+ { name: "IME key code", init: { keyCode: 229 } },
+];
+
+let root: Root;
+let host: HTMLDivElement;
+beforeEach(() => {
+ host = document.createElement("div");
+ document.body.append(host);
+ root = createRoot(host);
+});
+afterEach(() => {
+ flushSync(() => root.unmount());
+ host.remove();
+ document.body.innerHTML = "";
+ vi.clearAllMocks();
+});
+
+function type(input: HTMLInputElement, value: string) {
+ const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!;
+ flushSync(() => {
+ setter.call(input, value);
+ input.dispatchEvent(new Event("input", { bubbles: true }));
+ });
+}
+
+function press(target: Element, key: string, init: KeyboardEventInit = {}) {
+ flushSync(() => target.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true, ...init })));
+}
+
+describe("New channel panel with an input method", () => {
+ function open() {
+ const onClose = vi.fn();
+ flushSync(() => root.render(createElement(NewRoomPanel, { onClose })));
+ flushSync(() => document.querySelector<HTMLButtonElement>('[role="checkbox"][aria-label="Atlas"]')!.click());
+ const [name, section] = [...document.querySelectorAll<HTMLInputElement>("input")];
+ return { onClose, name, section };
+ }
+
+ it.each(composing)("does not create the channel or close on keys that belong to the $name", ({ init }) => {
+ const { onClose, name, section } = open();
+ for (const input of [name, section]) {
+ type(input, "kaifa");
+ press(input, "Enter", init);
+ press(input, "Escape", init);
+ }
+ expect(fixture.dispatch).not.toHaveBeenCalled();
+ expect(onClose).not.toHaveBeenCalled();
+ });
+
+ it("creates the channel on the Enter after the text is committed", () => {
+ const { onClose, name } = open();
+ type(name, "开发");
+ press(name, "Enter");
+ expect(fixture.dispatch).toHaveBeenCalledWith(expect.objectContaining({ type: "createGroup", name: "开发", memberIds: [bot.id] }));
+ expect(onClose).toHaveBeenCalledOnce();
+ });
+});
+
+describe("Team dialog with an input method", () => {
+ it.each(composing)("does not save a half-typed name or close on keys that belong to the $name", ({ init }) => {
+ const onClose = vi.fn();
+ flushSync(() => root.render(createElement(TeamDialog, { section: "Research", rename: true, onClose })));
+ const input = document.querySelector<HTMLInputElement>('[role="dialog"] input')!;
+ type(input, "yanjiu");
+ press(input, "Enter", init);
+ press(input, "Escape", init);
+ expect(fixture.api).not.toHaveBeenCalled();
+ expect(onClose).not.toHaveBeenCalled();
+ });
+
+ it("renames on the Enter after the text is committed", async () => {
+ fixture.api.mockResolvedValue({ sections: ["研究"] });
+ const onClose = vi.fn();
+ const onRenamed = vi.fn();
+ flushSync(() => root.render(createElement(TeamDialog, { section: "Research", rename: true, onClose, onRenamed })));
+ const input = document.querySelector<HTMLInputElement>('[role="dialog"] input')!;
+ type(input, "研究");
+ press(input, "Enter");
+ await vi.waitFor(() => expect(onRenamed).toHaveBeenCalledWith("Research", "研究"));
+ expect(fixture.api).toHaveBeenCalledWith(
+ "/api/sidebar-sections?section=Research",
+ expect.objectContaining({ method: "PATCH", body: JSON.stringify({ name: "研究" }) }),
+ );
+ expect(onClose).toHaveBeenCalledOnce();
+ });
+});
diff --git a/src/components/SidebarNewRoomPanel.tsx b/src/components/SidebarNewRoomPanel.tsx
index c60f032927..013219ef61 100644
--- a/src/components/SidebarNewRoomPanel.tsx
+++ b/src/components/SidebarNewRoomPanel.tsx
@@ -2,6 +2,7 @@ import { useState } from "react";
import { track } from "@/lib/analytics";
import { useStore } from "@/state/store";
import { t } from "@/lib/i18n";
+import { imeComposing } from "@/lib/send-key";
import { BotPickerList } from "./BotPickerList";
/** Sidebar → New channel: name it, optionally file it under a context, pick its bots. */
@@ -42,6 +43,7 @@ export function NewRoomPanel({ onClose }: { onClose: () => void }) {
value={name}
onChange={(e) => setName(e.target.value)}
onKeyDown={(e) => {
+ if (imeComposing(e.nativeEvent)) return;
if (e.key === "Enter") create();
if (e.key === "Escape") onClose();
}}
@@ -53,6 +55,7 @@ export function NewRoomPanel({ onClose }: { onClose: () => void }) {
maxLength={60}
onChange={(e) => setSection(e.target.value)}
onKeyDown={(e) => {
+ if (imeComposing(e.nativeEvent)) return;
if (e.key === "Enter") create();
if (e.key === "Escape") onClose();
}}
diff --git a/src/components/TeamDialog.tsx b/src/components/TeamDialog.tsx
index bd3bdde77a..7c7d03aead 100644
--- a/src/components/TeamDialog.tsx
+++ b/src/components/TeamDialog.tsx
@@ -4,6 +4,7 @@ import { Plus, X } from "lucide-react";
import { api, useStore, type Bot } from "@/state/store";
import { BotPickerList } from "./BotPickerList";
import { t } from "@/lib/i18n";
+import { imeComposing } from "@/lib/send-key";
export function TeamDialog({ section, rename = false, onClose, onRenamed }: {
section?: string;
@@ -68,6 +69,7 @@ export function TeamDialog({ section, rename = false, onClose, onRenamed }: {
<div ref={dialog} role="dialog" aria-modal="true" aria-labelledby="team-dialog-title"
className="max-h-[90vh] w-full max-w-[430px] overflow-y-auto rounded-2xl border border-hairline/50 bg-panel p-5 text-ink shadow-2xl"
onKeyDown={(event) => {
+ if (imeComposing(event.nativeEvent)) return;
if (event.key === "Escape" && !saving) { event.stopPropagation(); onClose(); }
if (event.key === "Tab") {
const controls = dialog.current?.querySelectorAll<HTMLElement>("input:enabled, button:enabled");
@@ -83,7 +85,7 @@ export function TeamDialog({ section, rename = false, onClose, onRenamed }: {
</div>
{(!moving || rename) && <label className="mb-3 block text-[13px] text-ink-secondary">{t("team.name")}
<input value={name} maxLength={60} disabled={saving} onChange={(event) => setName(event.target.value)}
- onKeyDown={(event) => { if (event.key === "Enter") void save(); }}
+ onKeyDown={(event) => { if (event.key === "Enter" && !imeComposing(event.nativeEvent)) void save(); }}
className="mt-1 w-full rounded-lg border border-hairline/50 bg-inset px-3 py-2 text-[14px] text-ink" />
</label>}
{!rename && <>