fix(webui): ignore stale file menu clipboard results
This commit is contained in:
1 parent
7aac9355ec
commit
f8bfcf4250
2 files changed
+46
-4
No files matched your search
@@ -44,6 +44,12 @@ export function FileActions({
|
||||
const anchor = useRef({ getBoundingClientRect: () => new DOMRect(point.current.x, point.current.y, 0, 0) });
|
||||
const opener = useRef<HTMLElement | null>(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<HTMLElement>("button, a, [tabindex]") : null;
|
||||
interactedOutside.current = false;
|
||||
setOpen(true);
|
||||
changeOpen(true);
|
||||
};
|
||||
const status = copyStatus ?? (loadFailed ? "unavailable" : !resolved && resolveMetadata ? "loading" : null);
|
||||
|
||||
return (
|
||||
<Menu.Root open={open} onOpenChange={setOpen}>
|
||||
<Menu.Root open={open} onOpenChange={changeOpen}>
|
||||
<Menu.Anchor virtualRef={anchor} />
|
||||
<span
|
||||
className={cn("not-prose inline-flex max-w-full items-baseline align-baseline", className)}
|
||||
|
||||
@@ -99,6 +99,39 @@ describe("File actions", () => {
|
||||
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(<FileActions path="notes.md" metadata={details}><button>notes.md</button></FileActions>);
|
||||
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) => <FileActions path={path} metadata={metadata}><button>{path}</button></FileActions>;
|
||||
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(<FileActionsProvider value={{ resolveMetadata: vi.fn().mockResolvedValue({ path: "/workspace/notes.md", content: "old preview response" }) }}>
|
||||
|
||||
Reference in new issue
Block a user