The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useRef, useState } from "react"; |
| 2 | import { expect, it } from "vite-plus/test"; |
| 3 | import { page, userEvent } from "vite-plus/test/browser"; |
| 4 | |
| 5 | import { act, render } from "../../../test/render"; |
| 6 | import { Dialog, DialogPopup } from "../ui/dialog"; |
| 7 | import { ComposerStashMenu } from "./ComposerStashMenu"; |
| 8 | |
| 9 | function Composer({ dialog = false }: { dialog?: boolean }) { |
| 10 | const composerFormRef = useRef<HTMLFormElement>(null); |
| 11 | const [entries, setEntries] = useState( |
| 12 | ["first", "second"].map((id) => ({ |
| 13 | id, |
| 14 | createdAt: "2026-10-01T00:00:00.000Z", |
| 15 | prompt: `${id} prompt`, |
| 16 | attachments: [], |
| 17 | droppedImageNames: [], |
| 18 | })), |
| 19 | ); |
| 20 | const [draft, setDraft] = useState("Unsent draft"); |
| 21 | const [open, setOpen] = useState(true); |
| 22 | const [dialogOpen, setDialogOpen] = useState(dialog); |
| 23 | const [answer, setAnswer] = useState<string | null>(null); |
| 24 | return ( |
| 25 | <> |
| 26 | <form ref={composerFormRef}> |
| 27 | <textarea |
| 28 | aria-label="Message" |
| 29 | value={draft} |
| 30 | onChange={(event) => setDraft(event.target.value)} |
| 31 | /> |
| 32 | <button type="button" onClick={() => setAnswer("Continue")}> |
| 33 | Continue |
| 34 | </button> |
| 35 | <p role="status">{answer ? `Selected: ${answer}` : "Choose an answer"}</p> |
| 36 | </form> |
| 37 | <input aria-label="Search threads" /> |
| 38 | <button onClick={() => setOpen(true)}>Open stash</button> |
| 39 | {open && ( |
| 40 | <ComposerStashMenu |
| 41 | composerFormRef={composerFormRef} |
| 42 | entries={entries} |
| 43 | stashShortcutLabel="Ctrl+S" |
| 44 | onRestore={(entry) => { |
| 45 | setDraft(entry.prompt); |
| 46 | setEntries((current) => current.filter(({ id }) => id !== entry.id)); |
| 47 | setOpen(false); |
| 48 | }} |
| 49 | onDelete={(entry) => { |
| 50 | setEntries((current) => current.filter(({ id }) => id !== entry.id)); |
| 51 | if (entries.length === 1) setOpen(false); |
| 52 | }} |
| 53 | onClose={() => setOpen(false)} |
| 54 | /> |
| 55 | )} |
| 56 | <Dialog open={dialogOpen} onOpenChange={setDialogOpen}> |
| 57 | <DialogPopup aria-label="Command palette"> |
| 58 | <input aria-label="Search commands" /> |
| 59 | </DialogPopup> |
| 60 | </Dialog> |
| 61 | </> |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | const editor = () => page.getByRole("textbox", { name: "Message" }); |
| 66 | const stash = () => page.getByRole("list", { name: "Stashed prompts" }); |
| 67 | const press = (keys: string) => userEvent.keyboard(keys); |
| 68 | const focus = (element: Element) => act(async () => (element as HTMLElement).focus()); |
| 69 | |
| 70 | it("leaves dialog, unrelated input and question button shortcuts alone while the stash is open", async () => { |
| 71 | await using _screen = await render(<Composer dialog />); |
| 72 | const input = page.getByRole("textbox", { name: "Search commands" }).element(); |
| 73 | await focus(input); |
| 74 | |
| 75 | for (const key of [ |
| 76 | { key: "ArrowDown" }, |
| 77 | { key: "ArrowUp" }, |
| 78 | { key: "Enter" }, |
| 79 | { key: "Backspace", ctrlKey: true }, |
| 80 | { key: "Backspace", metaKey: true }, |
| 81 | ]) { |
| 82 | const event = new KeyboardEvent("keydown", { ...key, bubbles: true, cancelable: true }); |
| 83 | await act(async () => { |
| 84 | input.dispatchEvent(event); |
| 85 | }); |
| 86 | expect(event.defaultPrevented, key.key).toBe(false); |
| 87 | } |
| 88 | |
| 89 | await press("{Escape}"); |
| 90 | await expect.element(page.getByRole("dialog")).not.toBeInTheDocument(); |
| 91 | expect(stash().element().textContent).toContain("first prompt"); |
| 92 | expect(stash().element().textContent).toContain("second prompt"); |
| 93 | await expect.element(editor()).toHaveValue("Unsent draft"); |
| 94 | |
| 95 | await focus(page.getByRole("textbox", { name: "Search threads" }).element()); |
| 96 | await press("{ArrowDown}{Enter}{Control>}{Backspace}{/Control}{Meta>}{Backspace}{/Meta}{Escape}"); |
| 97 | expect(stash().element().textContent).toContain("first prompt"); |
| 98 | expect(stash().element().textContent).toContain("second prompt"); |
| 99 | await expect.element(editor()).toHaveValue("Unsent draft"); |
| 100 | |
| 101 | await focus(page.getByRole("button", { name: "Continue", exact: true }).element()); |
| 102 | await press("{Enter}"); |
| 103 | expect(page.getByRole("status").element().textContent).toBe("Selected: Continue"); |
| 104 | expect(stash().element().textContent).toContain("first prompt"); |
| 105 | expect(stash().element().textContent).toContain("second prompt"); |
| 106 | await press("{Control>}{Backspace}{/Control}{Meta>}{Backspace}{/Meta}"); |
| 107 | expect(stash().element().textContent).toContain("first prompt"); |
| 108 | expect(stash().element().textContent).toContain("second prompt"); |
| 109 | await expect.element(editor()).toHaveValue("Unsent draft"); |
| 110 | }); |