Skip to content

Commit a81961d

Browse files
fix(web): copy file paths relative to the thread workspace (pingdotgg#21497)
Co-authored-by: Westline Marketing <[email protected]>
1 parent 05d32e5 commit a81961d

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,
@@ -277,6 +278,25 @@ function hasMarkdownFilePrimaryAction(input: {
277278
);
278279
}
279280

281+
export function markdownFileCopyMenuItems({
282+
targetPath,
283+
workspaceRelativePath,
284+
}: Pick<MarkdownFileLinkMeta, "targetPath" | "workspaceRelativePath">) {
285+
const items = [
286+
{ id: "copy-full", label: "Copy full path", value: targetPath, title: "Full path" },
287+
];
288+
if (workspaceRelativePath !== null) {
289+
const position = targetPath.slice(splitFilePathPosition(targetPath).path.length);
290+
items.unshift({
291+
id: "copy-relative",
292+
label: "Copy relative path",
293+
value: `${workspaceRelativePath}${position}`,
294+
title: "Relative path",
295+
});
296+
}
297+
return items;
298+
}
299+
280300
export function shouldUseMarkdownFileBrowserPrimaryAction(input: {
281301
iconPath: string;
282302
canOpenInEditor: boolean;
@@ -1213,7 +1233,7 @@ interface MarkdownFileLinkProps {
12131233
href: string;
12141234
targetPath: string;
12151235
iconPath: string;
1216-
displayPath: string;
1236+
workspaceRelativePath: string | null;
12171237
panelPath: string | null;
12181238
line?: number | undefined;
12191239
label: string;
@@ -1950,7 +1970,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
19501970
href,
19511971
targetPath,
19521972
iconPath,
1953-
displayPath,
1973+
workspaceRelativePath,
19541974
panelPath,
19551975
line,
19561976
label,
@@ -2132,6 +2152,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
21322152
if (!api) return;
21332153

21342154
try {
2155+
const copyItems = markdownFileCopyMenuItems({ targetPath, workspaceRelativePath });
21352156
const clicked = await api.contextMenu.show(
21362157
[
21372158
...(onOpenMedia ? ([{ id: "preview-media", label: "Preview media" }] as const) : []),
@@ -2140,8 +2161,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
21402161
? ([{ id: "open-in-browser", label: "Open in integrated browser" }] as const)
21412162
: []),
21422163
...(onReveal && revealLabel ? ([{ id: "reveal", label: revealLabel }] as const) : []),
2143-
{ id: "copy-relative", label: "Copy relative path" },
2144-
{ id: "copy-full", label: "Copy full path" },
2164+
...copyItems.map(({ id, label }) => ({ id, label })),
21452165
] as const,
21462166
position,
21472167
);
@@ -2162,12 +2182,9 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
21622182
handleRevealInFileManager();
21632183
return;
21642184
}
2165-
if (clicked === "copy-relative") {
2166-
handleCopy(displayPath, "Relative path");
2167-
return;
2168-
}
2169-
if (clicked === "copy-full") {
2170-
handleCopy(targetPath, "Full path");
2185+
const copyItem = copyItems.find(({ id }) => id === clicked);
2186+
if (copyItem) {
2187+
handleCopy(copyItem.value, copyItem.title);
21712188
}
21722189
} catch (cause) {
21732190
reportMarkdownActionFailure(
@@ -2277,7 +2294,7 @@ function areMarkdownFileLinkPropsEqual(
22772294
previous.targetPath === next.targetPath &&
22782295
previous.iconPath === next.iconPath &&
22792296
previous.environmentId === next.environmentId &&
2280-
previous.displayPath === next.displayPath &&
2297+
previous.workspaceRelativePath === next.workspaceRelativePath &&
22812298
previous.panelPath === next.panelPath &&
22822299
previous.line === next.line &&
22832300
previous.label === next.label &&
@@ -2708,7 +2725,7 @@ function useChatMarkdownState({
27082725
href={fileLinkMeta.targetPath}
27092726
targetPath={fileLinkMeta.targetPath}
27102727
iconPath={fileLinkMeta.filePath}
2711-
displayPath={fileLinkMeta.displayPath}
2728+
workspaceRelativePath={fileLinkMeta.workspaceRelativePath}
27122729
panelPath={panelPath}
27132730
line={fileLinkMeta.line}
27142731
label={fileLinkLabel(

0 commit comments

Comments
 (0)