TypeScript
fix(web): copy file paths relative to the thread workspace (#21497)
Julius Marmingea81961d75681
diff --git a/apps/web/src/components/ChatMarkdown.logic.test.ts b/apps/web/src/components/ChatMarkdown.logic.test.tsindex e93edec0374..0e8a55d9fb6 100644--- a/apps/web/src/components/ChatMarkdown.logic.test.ts+++ b/apps/web/src/components/ChatMarkdown.logic.test.ts@@ -1,8 +1,11 @@import { EnvironmentId } from "@t3tools/contracts";import { describe, expect, it } from "vite-plus/test";+import { resolveMarkdownFileLinkMeta } from "../markdown-links";+import {canUseMarkdownFileShellActions,+ markdownFileCopyMenuItems,shouldUseMarkdownFileBrowserPrimaryAction,} from "./ChatMarkdown";@@ -53,3 +56,52 @@ describe("shouldUseMarkdownFileBrowserPrimaryAction", () => {).toBe(true);});});++describe("markdownFileCopyMenuItems", () => {+ it.each([+ {+ name: "project file",+ href: "/Users/jordan/project/src/main.ts",+ cwd: "/Users/jordan/project",+ relative: "src/main.ts",+ full: "/Users/jordan/project/src/main.ts",+ },+ {+ name: "worktree file with a line and column",+ href: "src/main.ts:12:3",+ cwd: "/Users/jordan/project/.claude/worktrees/topic",+ relative: "src/main.ts:12:3",+ full: "/Users/jordan/project/.claude/worktrees/topic/src/main.ts:12:3",+ },+ {+ name: "main checkout file outside the thread worktree",+ href: "/Users/jordan/project/src/main.ts",+ cwd: "/Users/jordan/project/.claude/worktrees/topic",+ relative: null,+ full: "/Users/jordan/project/src/main.ts",+ },+ ])("offers accurate clipboard values for $name", ({ href, cwd, relative, full }) => {+ const meta = resolveMarkdownFileLinkMeta(href, cwd);+ expect(meta).not.toBeNull();+ if (!meta) throw new Error(`Expected a file link for ${href}`);++ expect(markdownFileCopyMenuItems(meta)).toEqual([+ ...(relative === null+ ? []+ : [+ {+ id: "copy-relative",+ label: "Copy relative path",+ value: relative,+ title: "Relative path",+ },+ ]),+ {+ id: "copy-full",+ label: "Copy full path",+ value: full,+ title: "Full path",+ },+ ]);+ });+});diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsxindex 51a0048f5f4..4517ffe5bf5 100644--- a/apps/web/src/components/ChatMarkdown.tsx+++ b/apps/web/src/components/ChatMarkdown.tsx@@ -3,6 +3,7 @@ import {buildFileLinkParentSuffixByPath,fileLinkLabel,resolvePathLinkTarget,+ splitFilePathPosition,} from "@t3tools/shared/fileLinks";import {isWindowsDrivePathHref,@@ -277,6 +278,25 @@ function hasMarkdownFilePrimaryAction(input: {);}+export function markdownFileCopyMenuItems({+ targetPath,+ workspaceRelativePath,+}: Pick<MarkdownFileLinkMeta, "targetPath" | "workspaceRelativePath">) {+ const items = [+ { id: "copy-full", label: "Copy full path", value: targetPath, title: "Full path" },+ ];+ if (workspaceRelativePath !== null) {+ const position = targetPath.slice(splitFilePathPosition(targetPath).path.length);+ items.unshift({+ id: "copy-relative",+ label: "Copy relative path",+ value: `${workspaceRelativePath}${position}`,+ title: "Relative path",+ });+ }+ return items;+}+export function shouldUseMarkdownFileBrowserPrimaryAction(input: {iconPath: string;canOpenInEditor: boolean;@@ -1213,7 +1233,7 @@ interface MarkdownFileLinkProps {href: string;targetPath: string;iconPath: string;- displayPath: string;+ workspaceRelativePath: string | null;panelPath: string | null;line?: number | undefined;label: string;@@ -1950,7 +1970,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({href,targetPath,iconPath,- displayPath,+ workspaceRelativePath,panelPath,line,label,@@ -2132,6 +2152,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({if (!api) return;try {+ const copyItems = markdownFileCopyMenuItems({ targetPath, workspaceRelativePath });const clicked = await api.contextMenu.show([...(onOpenMedia ? ([{ id: "preview-media", label: "Preview media" }] as const) : []),@@ -2140,8 +2161,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({? ([{ id: "open-in-browser", label: "Open in integrated browser" }] as const): []),...(onReveal && revealLabel ? ([{ id: "reveal", label: revealLabel }] as const) : []),- { id: "copy-relative", label: "Copy relative path" },- { id: "copy-full", label: "Copy full path" },+ ...copyItems.map(({ id, label }) => ({ id, label })),] as const,position,);@@ -2162,12 +2182,9 @@ const MarkdownFileLink = memo(function MarkdownFileLink({handleRevealInFileManager();return;}- if (clicked === "copy-relative") {- handleCopy(displayPath, "Relative path");- return;- }- if (clicked === "copy-full") {- handleCopy(targetPath, "Full path");+ const copyItem = copyItems.find(({ id }) => id === clicked);+ if (copyItem) {+ handleCopy(copyItem.value, copyItem.title);}} catch (cause) {reportMarkdownActionFailure(@@ -2277,7 +2294,7 @@ function areMarkdownFileLinkPropsEqual(previous.targetPath === next.targetPath &&previous.iconPath === next.iconPath &&previous.environmentId === next.environmentId &&- previous.displayPath === next.displayPath &&+ previous.workspaceRelativePath === next.workspaceRelativePath &&previous.panelPath === next.panelPath &&previous.line === next.line &&previous.label === next.label &&@@ -2708,7 +2725,7 @@ function useChatMarkdownState({href={fileLinkMeta.targetPath}targetPath={fileLinkMeta.targetPath}iconPath={fileLinkMeta.filePath}- displayPath={fileLinkMeta.displayPath}+ workspaceRelativePath={fileLinkMeta.workspaceRelativePath}panelPath={panelPath}line={fileLinkMeta.line}label={fileLinkLabel(