Skip to content

Commit 1776cdf

Browse files
fix(web): copy file paths relative to the thread workspace
Continues westline-jordan’s contribution in #20353. Co-authored-by: Westline Marketing <[email protected]>
1 parent 49b0f96 commit 1776cdf

2 files changed

Lines changed: 81 additions & 12 deletions

File tree

‎apps/web/src/components/ChatMarkdown.logic.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { EnvironmentId } from "@t3tools/contracts";
22
import { describe, expect, it } from "vite-plus/test";
33

4+
import { resolveMarkdownFileLinkMeta } from "../markdown-links";
5+
46
import {
57
canUseMarkdownFileShellActions,
8+
markdownFileCopyMenuItems,
69
shouldUseMarkdownFileBrowserPrimaryAction,
710
} from "./ChatMarkdown";
811

@@ -53,3 +56,52 @@ describe("shouldUseMarkdownFileBrowserPrimaryAction", () => {
5356
).toBe(true);
5457
});
5558
});
59+
60+
describe("markdownFileCopyMenuItems", () => {
61+
it.each([
62+
{
63+
name: "project file",
64+
href: "/Users/jordan/project/src/main.ts",
65+
cwd: "/Users/jordan/project",
66+
relative: "src/main.ts",
67+
full: "/Users/jordan/project/src/main.ts",
68+
},
69+
{
70+
name: "worktree file with a line and column",
71+
href: "src/main.ts:12:3",
72+
cwd: "/Users/jordan/project/.claude/worktrees/topic",
73+
relative: "src/main.ts:12:3",
74+
full: "/Users/jordan/project/.claude/worktrees/topic/src/main.ts:12:3",
75+
},
76+
{
77+
name: "main checkout file outside the thread worktree",
78+
href: "/Users/jordan/project/src/main.ts",
79+
cwd: "/Users/jordan/project/.claude/worktrees/topic",
80+
relative: null,
81+
full: "/Users/jordan/project/src/main.ts",
82+
},
83+
])("offers accurate clipboard values for $name", ({ href, cwd, relative, full }) => {
84+
const meta = resolveMarkdownFileLinkMeta(href, cwd);
85+
expect(meta).not.toBeNull();
86+
if (!meta) throw new Error(`Expected a file link for ${href}`);
87+
88+
expect(markdownFileCopyMenuItems(meta)).toEqual([
89+
...(relative === null
90+
? []
91+
: [
92+
{
93+
id: "copy-relative",
94+
label: "Copy relative path",
95+
value: relative,
96+
title: "Relative path",
97+
},
98+
]),
99+
{
100+
id: "copy-full",
101+
label: "Copy full path",
102+
value: full,
103+
title: "Full path",
104+
},
105+
]);
106+
});
107+
});

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

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
buildFileLinkParentSuffixByPath,
44
fileLinkLabel,
55
resolvePathLinkTarget,
6+
splitFilePathPosition,
67
} from "@t3tools/shared/fileLinks";
78
import {
89
isWindowsDrivePathHref,
@@ -272,6 +273,25 @@ function hasMarkdownFilePrimaryAction(input: {
272273
);
273274
}
274275

276+
export function markdownFileCopyMenuItems({
277+
targetPath,
278+
workspaceRelativePath,
279+
}: Pick<MarkdownFileLinkMeta, "targetPath" | "workspaceRelativePath">) {
280+
const items = [
281+
{ id: "copy-full", label: "Copy full path", value: targetPath, title: "Full path" },
282+
];
283+
if (workspaceRelativePath !== null) {
284+
const position = targetPath.slice(splitFilePathPosition(targetPath).path.length);
285+
items.unshift({
286+
id: "copy-relative",
287+
label: "Copy relative path",
288+
value: `${workspaceRelativePath}${position}`,
289+
title: "Relative path",
290+
});
291+
}
292+
return items;
293+
}
294+
275295
export function shouldUseMarkdownFileBrowserPrimaryAction(input: {
276296
iconPath: string;
277297
canOpenInEditor: boolean;
@@ -1201,7 +1221,7 @@ interface MarkdownFileLinkProps {
12011221
href: string;
12021222
targetPath: string;
12031223
iconPath: string;
1204-
displayPath: string;
1224+
workspaceRelativePath: string | null;
12051225
panelPath: string | null;
12061226
line?: number | undefined;
12071227
label: string;
@@ -1902,7 +1922,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
19021922
href,
19031923
targetPath,
19041924
iconPath,
1905-
displayPath,
1925+
workspaceRelativePath,
19061926
panelPath,
19071927
line,
19081928
label,
@@ -2084,6 +2104,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
20842104
if (!api) return;
20852105

20862106
try {
2107+
const copyItems = markdownFileCopyMenuItems({ targetPath, workspaceRelativePath });
20872108
const clicked = await api.contextMenu.show(
20882109
[
20892110
...(onOpenMedia ? ([{ id: "preview-media", label: "Preview media" }] as const) : []),
@@ -2092,8 +2113,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
20922113
? ([{ id: "open-in-browser", label: "Open in integrated browser" }] as const)
20932114
: []),
20942115
...(onReveal && revealLabel ? ([{ id: "reveal", label: revealLabel }] as const) : []),
2095-
{ id: "copy-relative", label: "Copy relative path" },
2096-
{ id: "copy-full", label: "Copy full path" },
2116+
...copyItems.map(({ id, label }) => ({ id, label })),
20972117
] as const,
20982118
position,
20992119
);
@@ -2114,12 +2134,9 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
21142134
handleRevealInFileManager();
21152135
return;
21162136
}
2117-
if (clicked === "copy-relative") {
2118-
handleCopy(displayPath, "Relative path");
2119-
return;
2120-
}
2121-
if (clicked === "copy-full") {
2122-
handleCopy(targetPath, "Full path");
2137+
const copyItem = copyItems.find(({ id }) => id === clicked);
2138+
if (copyItem) {
2139+
handleCopy(copyItem.value, copyItem.title);
21232140
}
21242141
} catch (cause) {
21252142
reportMarkdownActionFailure(
@@ -2229,7 +2246,7 @@ function areMarkdownFileLinkPropsEqual(
22292246
previous.targetPath === next.targetPath &&
22302247
previous.iconPath === next.iconPath &&
22312248
previous.environmentId === next.environmentId &&
2232-
previous.displayPath === next.displayPath &&
2249+
previous.workspaceRelativePath === next.workspaceRelativePath &&
22332250
previous.panelPath === next.panelPath &&
22342251
previous.line === next.line &&
22352252
previous.label === next.label &&
@@ -2636,7 +2653,7 @@ function useChatMarkdownState({
26362653
href={fileLinkMeta.targetPath}
26372654
targetPath={fileLinkMeta.targetPath}
26382655
iconPath={fileLinkMeta.filePath}
2639-
displayPath={fileLinkMeta.displayPath}
2656+
workspaceRelativePath={fileLinkMeta.workspaceRelativePath}
26402657
panelPath={panelPath}
26412658
line={fileLinkMeta.line}
26422659
label={fileLinkLabel(

0 commit comments

Comments
 (0)