From f8bfcf42505af45aa995f9ba705766ad5fd2ffcb Mon Sep 17 00:00:00 2001 From: Xubin Ren <52506698+Re-bin@users.noreply.github.com> Date: Thu, 24 Sep 2026 03:23:08 +0800 Subject: [PATCH] fix(webui): ignore stale file menu clipboard results --- webui/src/components/FileActions.tsx | 17 ++++++++++---- webui/src/tests/file-actions.test.tsx | 33 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/webui/src/components/FileActions.tsx b/webui/src/components/FileActions.tsx index a62d02505..dbd8bc598 100644 --- a/webui/src/components/FileActions.tsx +++ b/webui/src/components/FileActions.tsx @@ -44,6 +44,12 @@ export function FileActions({ const anchor = useRef({ getBoundingClientRect: () => new DOMRect(point.current.x, point.current.y, 0, 0) }); const opener = useRef(null); const interactedOutside = useRef(false); + const copyGeneration = useRef(0); + + const changeOpen = (next: boolean) => { + copyGeneration.current += 1; + setOpen(next); + }; useEffect(() => { let cancelled = false; @@ -58,24 +64,27 @@ export function FileActions({ if (!cancelled) setResolved(value); }).catch(() => { if (!cancelled) setLoadFailed(true); }); } - return () => { cancelled = true; }; + return () => { cancelled = true; copyGeneration.current += 1; }; }, [open, path, metadata, resolveMetadata]); const copy = async (value: string) => { + const generation = ++copyGeneration.current; const success = await copyTextToClipboard(value); + // A dismissed/reopened menu or another reference owns its own feedback. + if (generation !== copyGeneration.current) return; setCopyStatus(success ? "copied" : "failed"); - if (success) setOpen(false); + if (success) changeOpen(false); }; const openMenu = (target: EventTarget, x: number, y: number) => { point.current = { x, y }; opener.current = target instanceof Element ? target.closest("button, a, [tabindex]") : null; interactedOutside.current = false; - setOpen(true); + changeOpen(true); }; const status = copyStatus ?? (loadFailed ? "unavailable" : !resolved && resolveMetadata ? "loading" : null); return ( - + { expect(copyTextToClipboard).toHaveBeenCalledWith("/new/notes.md"); }); + it.each([true, false])("ignores a late clipboard result (%s) after reopening the menu", async (result) => { + const user = userEvent.setup(); + let finish!: (value: boolean) => void; + vi.mocked(copyTextToClipboard).mockImplementationOnce(() => new Promise(resolve => { finish = resolve; })); + render(); + const reference = screen.getByRole("button", { name: "notes.md" }); + fireEvent.contextMenu(reference); + await user.click(screen.getByRole("menuitem", { name: "Copy absolute path" })); + await user.keyboard("{Escape}"); + fireEvent.contextMenu(reference); + await act(async () => finish(result)); + expect(screen.getByRole("menu")).toBeVisible(); + expect(screen.queryByRole("status")).not.toBeInTheDocument(); + await user.click(screen.getByRole("menuitem", { name: "Copy relative path" })); + expect(copyTextToClipboard).toHaveBeenLastCalledWith("notes.md"); + await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument()); + }); + + it("ignores a late clipboard result when the reference changes", async () => { + const user = userEvent.setup(); + let finish!: (value: boolean) => void; + vi.mocked(copyTextToClipboard).mockImplementationOnce(() => new Promise(resolve => { finish = resolve; })); + const view = (path: string, metadata: FileReferenceMetadata) => ; + const { rerender } = render(view("notes.md", details)); + fireEvent.contextMenu(screen.getByRole("button", { name: "notes.md" })); + await user.click(screen.getByRole("menuitem", { name: "Copy absolute path" })); + rerender(view("other.md", { path: "/workspace/other.md", relative_path: "other.md" })); + await act(async () => finish(true)); + expect(screen.getByRole("menu")).toBeVisible(); + await user.click(screen.getByRole("menuitem", { name: "Copy absolute path" })); + expect(copyTextToClipboard).toHaveBeenLastCalledWith("/workspace/other.md"); + }); + it("degrades to the original reference on an older gateway response", async () => { const user = userEvent.setup(); render(