Skip to content

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

fix(settings): an IME's confirming Enter no longer saves a half-typed computer name

hadan897700c436dae202
diff --git a/src/components/ComputerNameRow.ime.test.ts b/src/components/ComputerNameRow.ime.test.ts
new file mode 100644
index 0000000000..abe5623545
--- /dev/null
+++ b/src/components/ComputerNameRow.ime.test.ts
@@ -0,0 +1,84 @@
+// @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(() => ({ api: vi.fn() }));
+vi.mock("@/state/store", async (importOriginal) => {
+ const original = await importOriginal<typeof import("@/state/store")>();
+ return { ...original, api: fixture.api };
+});
+
+import { ComputerNameRow } from "./ComputerNameRow";
+
+let root: Root;
+let host: HTMLDivElement;
+beforeEach(() => {
+ fixture.api.mockReset();
+ fixture.api.mockResolvedValue({ computerName: "Study PC" });
+ host = document.createElement("div");
+ document.body.append(host);
+ root = createRoot(host);
+});
+afterEach(() => {
+ flushSync(() => root.unmount());
+ host.remove();
+ vi.clearAllMocks();
+});
+
+function mount() {
+ const onSaved = vi.fn();
+ flushSync(() => root.render(createElement(ComputerNameRow, { saved: "Office Mac", onSaved })));
+ const input = host.querySelector<HTMLInputElement>("#computer-name")!;
+ flushSync(() => input.focus());
+ return { input, onSaved };
+}
+
+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(input: HTMLInputElement, init: KeyboardEventInit = {}) {
+ const blur = vi.spyOn(input, "blur");
+ flushSync(() => input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true, ...init })));
+ return blur;
+}
+
+const IME = [
+ { name: "composition", init: { isComposing: true } },
+ { name: "IME key code", init: { keyCode: 229 } },
+];
+
+describe("renaming this computer with an input method", () => {
+ it.each(IME)("does not save on the Enter that belongs to the $name", async ({ init }) => {
+ const { input, onSaved } = mount();
+ type(input, "书房 diannao");
+ const blur = press(input, init);
+ await Promise.resolve();
+ await Promise.resolve();
+ expect(blur).not.toHaveBeenCalled();
+ expect(fixture.api).not.toHaveBeenCalled();
+ expect(onSaved).not.toHaveBeenCalled();
+ expect(input.value).toBe("书房 diannao");
+ });
+
+ it("still saves on a plain Enter", async () => {
+ const { input, onSaved } = mount();
+ type(input, "Study PC");
+ const blur = press(input);
+ expect(blur).toHaveBeenCalledOnce();
+ // happy-dom does not deliver a React onBlur from the programmatic blur() the handler uses.
+ flushSync(() => input.dispatchEvent(new FocusEvent("blur", { bubbles: true })));
+ await vi.waitFor(() => expect(fixture.api).toHaveBeenCalledOnce());
+ expect(fixture.api).toHaveBeenCalledWith("/api/config", expect.objectContaining({
+ method: "PATCH",
+ body: JSON.stringify({ computerName: "Study PC" }),
+ }));
+ await vi.waitFor(() => expect(onSaved).toHaveBeenCalledWith("Study PC"));
+ });
+});
diff --git a/src/components/ComputerNameRow.tsx b/src/components/ComputerNameRow.tsx
index f6d3f4bb73..999c298dc2 100644
--- a/src/components/ComputerNameRow.tsx
+++ b/src/components/ComputerNameRow.tsx
@@ -1,6 +1,7 @@
import { useEffect, useState } from "react";
import { t } from "@/lib/i18n";
+import { imeComposing } from "@/lib/send-key";
import { cn } from "@/lib/cn";
import { api, type ConfigStatus } from "@/state/store";
@@ -31,7 +32,7 @@ export function ComputerNameRow({ saved, onSaved, className }: { saved: string;
<label htmlFor="computer-name" className="text-[13px] font-medium text-ink">{t("remote.computerName.title")}</label>
<input id="computer-name" value={draft} disabled={saving} placeholder={t("remote.computerName.placeholder")}
aria-describedby="computer-name-help" onChange={(event) => setDraft(event.target.value)} onBlur={() => void save()}
- onKeyDown={(event) => { if (event.key === "Enter") event.currentTarget.blur(); }}
+ onKeyDown={(event) => { if (event.key !== "Enter" || imeComposing(event.nativeEvent)) return; event.currentTarget.blur(); }}
className="w-full max-w-xs rounded-lg border border-hairline/40 bg-inset px-3 py-2 text-[13px] text-ink placeholder:text-ink-secondary focus:outline-none" />
<p id="computer-name-help" className="text-[12px] leading-4 text-ink-secondary">{t("remote.computerName.help")}</p>
{error ? <p role="alert" className="text-[12px] leading-4 text-danger">{error}</p> : null}