Skip to content

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

fix(composer): a file that finishes attaching after Escape leaves focus out

Absterrg0b43d8f0f4aeb
diff --git a/src/components/Composer.escape.test.ts b/src/components/Composer.escape.test.ts
index 4a82472288..2f706f2ae4 100644
--- a/src/components/Composer.escape.test.ts
+++ b/src/components/Composer.escape.test.ts
@@ -6,7 +6,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { Bot, Message } from "@/state/store";
import { restoreComposerDraft } from "@/lib/drafts";
-const fixture = vi.hoisted(() => ({ dispatch: vi.fn() }));
+const fixture = vi.hoisted(() => ({ dispatch: vi.fn(), finishIntake: () => {} }));
const ADDED_CLAUDE = "claude-5b0e1d2c-7a4f-4e1b-9c3d-2f6a8b1c4d5e";
vi.mock("@/state/store", async (importOriginal) => {
const original = await importOriginal<typeof import("@/state/store")>();
@@ -22,6 +22,11 @@ vi.mock("@/lib/analytics", () => ({ track: vi.fn() }));
vi.mock("./ModelPicker", () => ({ ModelPicker: () => null }));
vi.mock("@/lib/use-owner-or-admin", () => ({ useOwnerOrAdmin: () => false }));
vi.mock("./CallView", () => ({ CallButton: () => null }));
+vi.mock("@/lib/attach-notice", () => ({
+ intakeFilesWithNotice: () => new Promise((resolve) => {
+ fixture.finishIntake = () => resolve({ attachments: [], notice: null });
+ }),
+}));
import { Composer } from "./Composer";
@@ -54,6 +59,20 @@ describe("Escape in the composer", () => {
flushSync(() => input.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true, ...init })));
}
+ function pasteFile(input: HTMLTextAreaElement) {
+ const event = new Event("paste", { bubbles: true, cancelable: true });
+ Object.defineProperty(event, "clipboardData", { value: { files: [new File(["notes"], "notes.txt", { type: "text/plain" })] } });
+ flushSync(() => input.dispatchEvent(event));
+ }
+
+ const nextFrame = () => new Promise((resolve) => requestAnimationFrame(resolve));
+
+ async function finishIntake() {
+ fixture.finishIntake();
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ await nextFrame();
+ }
+
beforeEach(() => {
vi.stubGlobal("ResizeObserver", class { observe() {} disconnect() {} });
vi.spyOn(HTMLElement.prototype, "scrollIntoView").mockImplementation(() => {});
@@ -119,4 +138,24 @@ describe("Escape in the composer", () => {
}
expect(document.activeElement).toBe(input);
});
+
+ it("stays out of the composer when a pasted file finishes after Escape", async () => {
+ const input = render();
+ await nextFrame();
+ focusWith(input, "");
+ pasteFile(input);
+ pressEscape(input);
+ await finishIntake();
+ expect(document.activeElement).not.toBe(input);
+ });
+
+ it("still returns to the composer when a pasted file finishes without Escape", async () => {
+ const input = render();
+ await nextFrame();
+ focusWith(input, "");
+ pasteFile(input);
+ input.blur();
+ await finishIntake();
+ expect(document.activeElement).toBe(input);
+ });
});
diff --git a/src/components/Composer.tsx b/src/components/Composer.tsx
index 5cad612872..1e5e6dac0a 100644
--- a/src/components/Composer.tsx
+++ b/src/components/Composer.tsx
@@ -241,6 +241,7 @@ export function Composer({
draftIdRef.current = draftId;
const caretRef = useRef(0);
caretRef.current = caret;
+ const escapeBlurs = useRef(0);
const refocusInput = useCallback(() => {
requestAnimationFrame(() => {
const input = inputRef.current;
@@ -488,6 +489,7 @@ export function Composer({
const uploadImage = useCallback((file: File) => uploadDraftImage(draftId, file), [draftId]);
const pickFiles = async (picked: FileList | readonly File[] | null) => {
if (!picked?.length) return;
+ const escapesBefore = escapeBlurs.current;
changeDraftAttachmentPending(draftId, true);
try {
const { attachments: added, notice } = await intakeFilesWithNotice(Array.from(picked), {
@@ -502,7 +504,7 @@ export function Composer({
} finally {
changeDraftAttachmentPending(draftId, false);
}
- refocusInput();
+ if (escapeBlurs.current === escapesBefore) refocusInput();
};
const hasContent = Boolean(effectiveText.trim()) || attachments.length > 0;
@@ -908,7 +910,10 @@ export function Composer({
}
if (e.key !== "Escape") return;
if (recording) stopDictation();
- else if (!e.defaultPrevented) e.currentTarget.blur();
+ else if (!e.defaultPrevented) {
+ escapeBlurs.current += 1;
+ e.currentTarget.blur();
+ }
}}
// Not disabled during an upload: a disabled element drops keyboard focus for good. send() refuses while one
// is pending.