Skip to content

Commit 3296fb9

Browse files
fix(web): start file path copies before workspace lookup completes
1 parent e6dfde8 commit 3296fb9

2 files changed

Lines changed: 42 additions & 6 deletions

File tree

‎apps/web/src/components/ChatMarkdown.permissions.browser.test.tsx‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ beforeEach(() => {
146146
state.copy.mockReset().mockResolvedValue(undefined);
147147
useRightPanelStore.setState({ openFile: state.openFile });
148148
vi.spyOn(navigator.clipboard, "writeText").mockImplementation(state.copy);
149+
vi.spyOn(navigator.clipboard, "write").mockImplementation(async (items) => {
150+
const item = items[0]!;
151+
await state.copy(await (await item.getType("text/plain")).text());
152+
});
149153
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
150154
});
151155

@@ -202,6 +206,26 @@ it("removes host actions after revocation while keeping file preview and copying
202206
expect(state.openEditor).not.toHaveBeenCalled();
203207
});
204208

209+
it("starts copying before the nested-file lookup finishes", async () => {
210+
let finishLookup: () => void = () => {
211+
throw new Error("Lookup not started");
212+
};
213+
state.search.mockImplementationOnce(
214+
() =>
215+
new Promise((resolve) => {
216+
finishLookup = () =>
217+
resolve(AsyncResult.success({ entries: [{ path: "src/readme.md", kind: "file" }] }));
218+
}),
219+
);
220+
state.choose.mockResolvedValue("copy-full");
221+
await renderMarkdown("`readme.md:10:2`");
222+
await openContextMenu();
223+
expect(navigator.clipboard.write).toHaveBeenCalledOnce();
224+
expect(state.copy).not.toHaveBeenCalled();
225+
await act(async () => finishLookup());
226+
await vi.waitFor(() => expect(state.copy).toHaveBeenCalledWith("/work/src/readme.md:10:2"));
227+
});
228+
205229
it("does not launch an editor after revocation during a native context menu", async () => {
206230
let choose: (action: string) => void = () => {
207231
throw new Error("Menu not opened");

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
20442044
})();
20452045
};
20462046

2047-
const handleCopy = (value: string, title: string) => {
2047+
const handleCopy = (resolveValue: () => Promise<string>, title: string) => {
20482048
if (typeof window === "undefined" || !navigator.clipboard?.writeText) {
20492049
toastManager.add(
20502050
stackedThreadToast({
@@ -2056,12 +2056,22 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
20562056
return;
20572057
}
20582058

2059-
void navigator.clipboard.writeText(value).then(
2060-
() => {
2059+
const value = resolveValue();
2060+
// Start the write before workspace lookup finishes so Safari retains the user gesture.
2061+
const write =
2062+
typeof navigator.clipboard.write === "function" && typeof ClipboardItem !== "undefined"
2063+
? navigator.clipboard.write([
2064+
new ClipboardItem({
2065+
"text/plain": value.then((text) => new Blob([text], { type: "text/plain" })),
2066+
}),
2067+
])
2068+
: value.then((text) => navigator.clipboard.writeText(text));
2069+
void Promise.all([write, value]).then(
2070+
([, text]) => {
20612071
toastManager.add({
20622072
type: "success",
20632073
title: `${title} copied`,
2064-
description: value,
2074+
description: text,
20652075
});
20662076
},
20672077
(error) => {
@@ -2116,9 +2126,11 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
21162126
return;
21172127
}
21182128
if (clicked === "copy-relative" || clicked === "copy-full") {
2119-
const paths = await resolveCopyPaths();
21202129
handleCopy(
2121-
clicked === "copy-relative" ? paths.relativePath : paths.fullPath,
2130+
() =>
2131+
resolveCopyPaths().then((paths) =>
2132+
clicked === "copy-relative" ? paths.relativePath : paths.fullPath,
2133+
),
21222134
clicked === "copy-relative" ? "Relative path" : "Full path",
21232135
);
21242136
}

0 commit comments

Comments
 (0)