Skip to content

Commit 3bd744f

Browse files
only21milcodexclaude
authored
fix(web): show clipboard errors in anchored copy notifications (#23679)
Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 13f9bc4 commit 3bd744f

3 files changed

Lines changed: 61 additions & 4 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect, it, vi } from "vite-plus/test";
2+
import { page } from "vite-plus/test/browser";
3+
4+
import { render } from "../../test/render";
5+
import { DiffFilePathCopyButton } from "./DiffFilePathCopyButton";
6+
import { AnchoredToastProvider } from "./ui/toast";
7+
8+
it("explains a denied clipboard write and lets the same path be copied again", async () => {
9+
await using screen = await render(
10+
<AnchoredToastProvider timeout={0}>
11+
<DiffFilePathCopyButton filePath="apps/web/src/index.ts" />
12+
</AnchoredToastProvider>,
13+
);
14+
using write = vi
15+
.spyOn(navigator.clipboard, "writeText")
16+
.mockRejectedValueOnce(new DOMException("Clipboard access denied", "NotAllowedError"))
17+
.mockResolvedValue(undefined);
18+
using _console = vi.spyOn(console, "error").mockImplementation(() => undefined);
19+
20+
const copy = screen.getByRole("button", { name: "Copy file path" });
21+
await copy.click();
22+
23+
await expect.element(page.getByText("Failed to copy text to the clipboard.")).toBeVisible();
24+
await page.getByRole("button", { name: "Dismiss notification" }).click();
25+
await copy.click();
26+
27+
await expect.element(page.getByText("Copied!", { exact: true })).toBeVisible();
28+
expect(write).toHaveBeenCalledTimes(2);
29+
expect(write).toHaveBeenLastCalledWith("apps/web/src/index.ts");
30+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect, it, vi } from "vite-plus/test";
2+
import { page } from "vite-plus/test/browser";
3+
4+
import { render } from "../../../test/render";
5+
import { AnchoredToastProvider } from "../ui/toast";
6+
import { MessageCopyButton } from "./MessageCopyButton";
7+
8+
it("explains a denied clipboard write and lets the same message be copied again", async () => {
9+
await using screen = await render(
10+
<AnchoredToastProvider timeout={0}>
11+
<MessageCopyButton text="A message to copy" />
12+
</AnchoredToastProvider>,
13+
);
14+
using _write = vi
15+
.spyOn(navigator.clipboard, "writeText")
16+
.mockRejectedValueOnce(new DOMException("Clipboard access denied", "NotAllowedError"))
17+
.mockResolvedValue(undefined);
18+
using _console = vi.spyOn(console, "error").mockImplementation(() => undefined);
19+
20+
const copy = screen.getByRole("button", { name: "Copy message" });
21+
await copy.click();
22+
23+
await expect.element(page.getByText("Failed to copy text to the clipboard.")).toBeVisible();
24+
await expect.element(copy).toBeEnabled();
25+
await page.getByRole("button", { name: "Dismiss notification" }).click();
26+
await copy.click();
27+
28+
await expect.element(page.getByText("Copied!", { exact: true })).toBeVisible();
29+
await expect.element(copy).toBeDisabled();
30+
});

‎apps/web/src/components/ui/anchoredCopyToast.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@ export function showAnchoredCopySuccessToast(ref: RefObject<HTMLButtonElement |
2020
export function showAnchoredCopyErrorToast(ref: RefObject<HTMLButtonElement | null>, error: Error) {
2121
if (!ref.current) return;
2222
anchoredToastManager.add({
23-
data: {
24-
tooltipStyle: true,
25-
},
23+
type: "error",
2624
positionerProps: {
2725
anchor: ref.current,
2826
},
29-
timeout: ANCHORED_COPY_TOAST_TIMEOUT_MS,
3027
title: "Failed to copy",
3128
description: error.message,
3229
});

0 commit comments

Comments
 (0)