Skip to content

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

Fix IME handling and localized thread search in the picker

focuspointsb6ecce163f69
diff --git a/src/components/TaskPicker.test.ts b/src/components/TaskPicker.test.ts
index 29ca12d9bd..19dbc1f752 100644
--- a/src/components/TaskPicker.test.ts
+++ b/src/components/TaskPicker.test.ts
@@ -1,6 +1,6 @@
-import { describe, expect, it } from "vitest";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
-import { t } from "@/lib/i18n";
+import { activeLocale, loadLocale, setLocale, t } from "@/lib/i18n";
import {
TASK_PICKER_DISMISS_MS,
filterTasks,
@@ -71,6 +71,31 @@ describe("task picker copy", () => {
});
describe("filterTasks", () => {
+ let previousLocale: string;
+ beforeEach(() => { previousLocale = activeLocale(); });
+ afterEach(() => { setLocale(previousLocale); });
+
+ it.each([
+ ["zh", "New thread", "新建对话"],
+ ["zh", "Shared chat", "共享聊天"],
+ ["zh-tw", "New thread", "新增對話"],
+ ["zh-tw", "Shared chat", "共享聊天"],
+ ])("matches %s display title for %s while preserving raw search and prefix ranking", async (locale, raw, displayed) => {
+ expect(await loadLocale(locale)).toBe(locale);
+ const candidates = [
+ { title: `归档${displayed}` },
+ { title: raw },
+ { title: `${displayed} notes` },
+ { title: `Archived ${raw}` },
+ { title: `${raw} notes` },
+ ];
+ expect(filterTasks(candidates, ` ${displayed} `)).toEqual([candidates[1], candidates[2], candidates[0]]);
+ expect(filterTasks(candidates, displayed.slice(1))).toEqual(candidates.slice(0, 3));
+ expect(filterTasks(candidates, raw.toUpperCase())).toEqual([candidates[1], candidates[4], candidates[3]]);
+ const grouped = groupThreadTasks(candidates.map((task, index) => ({ ...task, threadId: String(index), createdAt: index })), [], displayed);
+ expect(grouped.flatMap((group) => group.tasks.map((task) => task.threadId))).toEqual(["1", "2", "0"]);
+ });
+
const tasks = [
{ title: "Clean up" },
{ title: "OpenMausBot Update" },
diff --git a/src/components/TaskPicker.tsx b/src/components/TaskPicker.tsx
index caad303d47..35a625ffeb 100644
--- a/src/components/TaskPicker.tsx
+++ b/src/components/TaskPicker.tsx
@@ -10,6 +10,7 @@ import { localizedTaskTitle } from "@/lib/task-title";
import { COMPACT_BUBBLE } from "@/lib/compact-chip";
import { formatTaskTokens, headlineTokens, usageDetail } from "@/lib/usage";
import { nextRename } from "@/lib/rename";
+import { imeComposing } from "@/lib/send-key";
import { FolderIcon, NewThreadButton } from "./BotProjects";
import { useShowThreads } from "@/lib/thread-preferences";
import { attentionJumpAction, attentionOwnerName, AttentionThreadRows, crossBotAttentionThreads, threadsWhenTreeHidden, type AttentionThread } from "./SidebarBotActivity";
@@ -37,8 +38,9 @@ export function filterTasks<T extends { title: string }>(tasks: readonly T[], qu
const substring: T[] = [];
for (const task of tasks) {
const title = task.title.toLowerCase();
- if (title.startsWith(needle)) prefix.push(task);
- else if (title.includes(needle)) substring.push(task);
+ const displayed = localizedTaskTitle(task.title).toLowerCase();
+ if (title.startsWith(needle) || displayed.startsWith(needle)) prefix.push(task);
+ else if (title.includes(needle) || displayed.includes(needle)) substring.push(task);
}
return [...prefix, ...substring];
}
@@ -257,6 +259,7 @@ function ConversationTaskPicker({
onClick={(e) => e.stopPropagation()}
onMouseDown={(e) => e.stopPropagation()}
onKeyDown={(e) => {
+ if (imeComposing(e.nativeEvent)) { e.stopPropagation(); return; }
if (e.key === "Escape") {
e.preventDefault();
e.stopPropagation();
@@ -264,7 +267,7 @@ function ConversationTaskPicker({
else closeMenu();
return;
}
- if (e.key === "Enter" && !e.nativeEvent.isComposing) {
+ if (e.key === "Enter") {
e.preventDefault();
const first = results[0];
if (!first) return;
@@ -311,7 +314,8 @@ function ConversationTaskPicker({
onMouseDown={(e) => e.stopPropagation()}
onBlur={() => commitRename(task.threadId, true)}
onKeyDown={(e) => {
- if (e.key === "Enter" && !e.nativeEvent.isComposing) {
+ if (imeComposing(e.nativeEvent)) { e.stopPropagation(); return; }
+ if (e.key === "Enter") {
e.preventDefault();
e.stopPropagation();
commitRename(task.threadId, true);
diff --git a/src/components/TaskPicker.visibility.test.ts b/src/components/TaskPicker.visibility.test.ts
index c8436f335c..916c1270ad 100644
--- a/src/components/TaskPicker.visibility.test.ts
+++ b/src/components/TaskPicker.visibility.test.ts
@@ -1,6 +1,10 @@
+// @vitest-environment happy-dom
import { createElement } from "react";
+import { flushSync } from "react-dom";
+import { createRoot, type Root } from "react-dom/client";
import { renderToStaticMarkup } from "react-dom/server";
-import { beforeEach, describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { t } from "@/lib/i18n";
import type { Bot, Group } from "@/state/store";
import { Select } from "./Select";
@@ -26,6 +30,99 @@ const bot: Bot = {
};
beforeEach(() => { fixture.showThreads = true; fixture.queued = {}; fixture.bots = []; fixture.dispatch.mockClear(); });
+describe("task picker IME keyboard handling", () => {
+ const group: Group = {
+ id: "team", name: "Team", threadId: "current", memberIds: [], defaultResponder: { kind: "everyone" },
+ bulletin: "", createdAt: 1, unread: false, messages: [],
+ tasks: [{ threadId: "current", title: "Current chat", createdAt: 1 }, { threadId: "other", title: "Other chat", createdAt: 2 }],
+ };
+ const composing = [{ isComposing: true }, { keyCode: 229 }];
+ let host: HTMLDivElement;
+ let root: Root;
+
+ const input = (label: string) => host.querySelector<HTMLInputElement>(`input[aria-label="${label}"]`);
+ const search = () => input(t("task.search"));
+ const rename = () => input(t("task.renameAria"));
+ const press = (target: Element, key: string, init: KeyboardEventInit = {}) => {
+ const event = new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true, ...init });
+ flushSync(() => target.dispatchEvent(event));
+ return event;
+ };
+ const type = (target: HTMLInputElement, value: string) => {
+ const setValue = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!;
+ flushSync(() => {
+ setValue.call(target, value);
+ target.dispatchEvent(new Event("input", { bubbles: true }));
+ });
+ };
+ const startRename = () => {
+ const button = [...host.querySelectorAll("button")].find((item) => item.getAttribute("aria-label") === t("task.renameNamed", { title: "Current chat" }))!;
+ flushSync(() => button.click());
+ type(rename()!, "新的名字");
+ };
+
+ beforeEach(() => {
+ document.documentElement.dataset.reducedMotion = "true";
+ host = document.createElement("div");
+ document.body.append(host);
+ root = createRoot(host);
+ flushSync(() => root.render(createElement(GroupTaskPicker, { group })));
+ flushSync(() => host.querySelector<HTMLButtonElement>("button")!.click());
+ });
+ afterEach(() => {
+ flushSync(() => root.unmount());
+ host.remove();
+ delete document.documentElement.dataset.reducedMotion;
+ });
+
+ it.each(composing)("does not select or close on a composing search Enter: %j", (init) => {
+ type(search()!, "Other");
+ expect(press(search()!, "Enter", init).defaultPrevented).toBe(false);
+ expect(search()?.value).toBe("Other");
+ expect(fixture.dispatch).not.toHaveBeenCalled();
+ expect(press(search()!, "Enter").defaultPrevented).toBe(true);
+ expect(fixture.dispatch).toHaveBeenCalledExactlyOnceWith({ type: "switchGroupTask", groupId: "team", threadId: "other" });
+ expect(search()).toBeNull();
+ });
+
+ describe.each(["", "Other"])("with search query %j", (query) => {
+ it.each(composing)("does not clear or dismiss on a composing search Escape: %j", (init) => {
+ type(search()!, query);
+ expect(press(search()!, "Escape", init).defaultPrevented).toBe(false);
+ expect(search()?.value).toBe(query);
+ expect(fixture.dispatch).not.toHaveBeenCalled();
+ expect(press(search()!, "Escape").defaultPrevented).toBe(true);
+ if (query) {
+ expect(search()?.value).toBe("");
+ press(search()!, "Escape");
+ }
+ expect(search()).toBeNull();
+ });
+ });
+
+ it.each(composing)("does not commit a rename on a composing Enter: %j", (init) => {
+ startRename();
+ expect(press(rename()!, "Enter", init).defaultPrevented).toBe(false);
+ expect(rename()?.value).toBe("新的名字");
+ expect(fixture.dispatch).not.toHaveBeenCalled();
+ expect(press(rename()!, "Enter").defaultPrevented).toBe(true);
+ expect(fixture.dispatch).toHaveBeenCalledExactlyOnceWith({ type: "renameGroupTask", groupId: "team", threadId: "current", title: "新的名字" });
+ expect(rename()).toBeNull();
+ expect(search()).not.toBeNull();
+ });
+
+ it.each(composing)("does not cancel a rename on a composing Escape: %j", (init) => {
+ startRename();
+ expect(press(rename()!, "Escape", init).defaultPrevented).toBe(false);
+ expect(rename()?.value).toBe("新的名字");
+ expect(fixture.dispatch).not.toHaveBeenCalled();
+ expect(press(rename()!, "Escape").defaultPrevented).toBe(true);
+ expect(rename()).toBeNull();
+ expect(search()).not.toBeNull();
+ expect(fixture.dispatch).not.toHaveBeenCalled();
+ });
+});
+
describe("optional bot thread picker", () => {
it("keeps the usual picker when threads are shown", () => {
expect(renderToStaticMarkup(createElement(TaskPicker, { bot }))).toContain('aria-label="All threads"');