@devries-cameron20 / t3code Mirror · read-only / commits / fc4319cea7
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 23:15 UTC

fix(web): Open in editor on a chat file chip opens the file the chip names (#18389)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

chickenputty committed Oct 19, 2026 at 23:56 UTC fc4319cea75879989f6e29edf0bf81d063d3c2e6
4 files changed +167 -16
apps/web/src/components/ChatMarkdown.permissions.browser.test.tsx
+24
@@ -146,6 +146,10 @@ beforeEach(() => {
146 state.copy.mockReset().mockResolvedValue(undefined);
147 useRightPanelStore.setState({ openFile: state.openFile });
148 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 + });
153 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
154 });
155
@@ -202,6 +206,26 @@ it("removes host actions after revocation while keeping file preview and copying
206 expect(state.openEditor).not.toHaveBeenCalled();
207 });
208
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 +
229 it("does not launch an editor after revocation during a native context menu", async () => {
230 let choose: (action: string) => void = () => {
231 throw new Error("Menu not opened");
apps/web/src/components/ChatMarkdown.tsx
+50 -16
@@ -180,6 +180,11 @@ import {
180 shouldOpenMarkdownFileLinkInEditor,
181 type MarkdownFileLinkMeta,
182 } from "../markdown-links";
183 +import {
184 + markdownFileCopyPaths,
185 + markdownFileEditorTarget,
186 + resolveMarkdownFileHostPath,
187 +} from "../markdownFileHostPath";
188 import { isMarkdownFileLinkLabel } from "@t3tools/shared/markdownLinks";
189 import { isElectron } from "../env";
190 import { readLocalApi } from "../localApi";
@@ -1253,8 +1258,9 @@ interface MarkdownFileLinkProps {
1258 copyMarkdown: string;
1259 theme: "light" | "dark";
1260 threadRef?: ScopedThreadRef | undefined;
1256 - onOpen?: ((targetPath: string) => Promise<AtomCommandResult<unknown, unknown>>) | undefined;
1261 + onOpen?: (() => Promise<AtomCommandResult<unknown, unknown>>) | undefined;
1262 onOpenInPanel: (panelPath: string, line: number | undefined) => void;
1263 + resolveCopyPaths: () => Promise<ReturnType<typeof markdownFileCopyPaths>>;
1264 openInEditorMenuLabel: string;
1265 onOpenInBrowser?: (() => Promise<AtomCommandResult<unknown, unknown>>) | undefined;
1266 onOpenMedia?: (() => void) | undefined;
@@ -1997,6 +2003,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
2003 onOpenMedia,
2004 onReveal,
2005 revealLabel,
2006 + resolveCopyPaths,
2007 }: MarkdownFileLinkProps) {
2008 const handleOpenInEditor = () => {
2009 if (!onOpen) {
@@ -2004,7 +2011,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
2011 }
2012 void (async () => {
2013 try {
2007 - const result = await onOpen(targetPath);
2014 + const result = await onOpen();
2015 if (result._tag === "Success" || isAtomCommandInterrupted(result)) {
2016 return;
2017 }
@@ -2124,7 +2131,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
2131 })();
2132 };
2133
2127 - const handleCopy = (value: string, title: string) => {
2134 + const handleCopy = (resolveValue: () => Promise<string>, title: string) => {
2135 if (typeof window === "undefined" || !navigator.clipboard?.writeText) {
2136 toastManager.add(
2137 stackedThreadToast({
@@ -2136,12 +2143,22 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
2143 return;
2144 }
2145
2139 - void navigator.clipboard.writeText(value).then(
2140 - () => {
2146 + const value = resolveValue();
2147 + // Start the write before workspace lookup finishes so Safari retains the user gesture.
2148 + const write =
2149 + typeof navigator.clipboard.write === "function" && typeof ClipboardItem !== "undefined"
2150 + ? navigator.clipboard.write([
2151 + new ClipboardItem({
2152 + "text/plain": value.then((text) => new Blob([text], { type: "text/plain" })),
2153 + }),
2154 + ])
2155 + : value.then((text) => navigator.clipboard.writeText(text));
2156 + void Promise.all([write, value]).then(
2157 + ([, text]) => {
2158 toastManager.add({
2159 type: "success",
2160 title: `${title} copied`,
2144 - description: value,
2161 + description: text,
2162 });
2163 },
2164 (error) => {
@@ -2195,9 +2212,14 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
2212 handleRevealInFileManager();
2213 return;
2214 }
2198 - const copyItem = copyItems.find(({ id }) => id === clicked);
2199 - if (copyItem) {
2200 - handleCopy(copyItem.value, copyItem.title);
2215 + if (clicked === "copy-relative" || clicked === "copy-full") {
2216 + handleCopy(
2217 + () =>
2218 + resolveCopyPaths().then((paths) =>
2219 + clicked === "copy-relative" ? paths.relativePath : paths.fullPath,
2220 + ),
2221 + clicked === "copy-relative" ? "Relative path" : "Full path",
2222 + );
2223 }
2224 } catch (cause) {
2225 reportMarkdownActionFailure(
@@ -2320,6 +2342,7 @@ function areMarkdownFileLinkPropsEqual(
2342 previous.onOpenInBrowser === next.onOpenInBrowser &&
2343 previous.onOpenMedia === next.onOpenMedia &&
2344 previous.onReveal === next.onReveal &&
2345 + previous.resolveCopyPaths === next.resolveCopyPaths &&
2346 previous.revealLabel === next.revealLabel
2347 );
2348 }
@@ -2735,16 +2758,17 @@ function useChatMarkdownState({
2758 openAt(match ?? panelPath);
2759 })();
2760 };
2738 - const revealMarkdownFileInFileManager = async (fileLinkMeta: MarkdownFileLinkMeta) => {
2739 - const match = await findWorkspaceFileMatch(fileLinkMeta);
2740 - const filePath = match && cwd ? resolvePathLinkTarget(match, cwd) : fileLinkMeta.filePath;
2741 - return revealFileInFileManager(filePath);
2742 - };
2761 const fileLinkChip = (
2762 fileLinkMeta: MarkdownFileLinkMeta,
2763 copyMarkdown: string,
2764 mediaSource?: string,
2765 ) => {
2766 + const resolveHostPath = () =>
2767 + resolveMarkdownFileHostPath({
2768 + meta: fileLinkMeta,
2769 + cwd,
2770 + findWorkspaceMatch: () => findWorkspaceFileMatch(fileLinkMeta),
2771 + });
2772 const mediaPath = mediaSource ?? fileLinkMeta.filePath;
2773 const canPreviewMedia =
2774 mediaMimeTypeFromExtension(
@@ -2774,7 +2798,17 @@ function useChatMarkdownState({
2798 copyMarkdown={copyMarkdown}
2799 theme={resolvedTheme}
2800 threadRef={threadRef}
2777 - {...(canUseShellActions ? { onOpen: openInPreferredEditor } : {})}
2801 + onOpen={
2802 + canUseShellActions
2803 + ? async () =>
2804 + openInPreferredEditor(
2805 + markdownFileEditorTarget(fileLinkMeta, await resolveHostPath()),
2806 + )
2807 + : undefined
2808 + }
2809 + resolveCopyPaths={async () =>
2810 + markdownFileCopyPaths(fileLinkMeta, await resolveHostPath(), cwd)
2811 + }
2812 onOpenInPanel={(path) => openFileInPanel(fileLinkMeta, path)}
2813 onOpenMedia={
2814 threadRef && canPreviewMedia
@@ -2784,7 +2818,7 @@ function useChatMarkdownState({
2818 openInEditorMenuLabel={preferredEditorMenuLabel}
2819 onReveal={
2820 canUseShellActions && revealInFileManagerLabel !== undefined
2787 - ? () => revealMarkdownFileInFileManager(fileLinkMeta)
2821 + ? async () => revealFileInFileManager((await resolveHostPath()).absolutePath)
2822 : undefined
2823 }
2824 revealLabel={revealInFileManagerLabel}
apps/web/src/markdown-links.test.ts
+45
@@ -7,6 +7,11 @@ import {
7 shouldOpenMarkdownFileLinkInBrowserByDefault,
8 shouldOpenMarkdownFileLinkInEditor,
9 } from "./markdown-links";
10 +import {
11 + markdownFileCopyPaths,
12 + markdownFileEditorTarget,
13 + resolveMarkdownFileHostPath,
14 +} from "./markdownFileHostPath";
15
16 describe("shouldOpenMarkdownFileLinkInEditor", () => {
17 it("uses command-click on macOS", () => {
@@ -299,3 +304,43 @@ it("routes the project-root code link to the workspace explorer", () => {
304 filePath: cwd,
305 });
306 });
307 +
308 +describe("chat file chip paths", () => {
309 + const cwd = String.raw`C:\Users\alice\project`;
310 +
311 + it("opens and copies the indexed file for a bare filename, preserving its line and column", async () => {
312 + const meta = resolveMarkdownFileLinkMeta("user.ts:261:7", cwd);
313 + if (!meta) throw new Error("Expected a file link");
314 + const hostPath = await resolveMarkdownFileHostPath({
315 + meta,
316 + cwd,
317 + findWorkspaceMatch: async () => "src/discord/events/user.ts",
318 + });
319 +
320 + expect(markdownFileEditorTarget(meta, hostPath)).toBe(
321 + String.raw`C:\Users\alice\project\src\discord\events\user.ts:261:7`,
322 + );
323 + expect(markdownFileCopyPaths(meta, hostPath, cwd)).toEqual({
324 + fullPath: String.raw`C:\Users\alice\project\src\discord\events\user.ts:261:7`,
325 + relativePath: "src/discord/events/user.ts:261:7",
326 + });
327 + });
328 +
329 + it("keeps the authored path and position when the workspace index has no match", async () => {
330 + const meta = resolveMarkdownFileLinkMeta("missing.ts:12:3", cwd);
331 + if (!meta) throw new Error("Expected a file link");
332 + const hostPath = await resolveMarkdownFileHostPath({
333 + meta,
334 + cwd,
335 + findWorkspaceMatch: async () => null,
336 + });
337 +
338 + expect(markdownFileEditorTarget(meta, hostPath)).toBe(
339 + String.raw`C:\Users\alice\project\missing.ts:12:3`,
340 + );
341 + expect(markdownFileCopyPaths(meta, hostPath, cwd)).toEqual({
342 + fullPath: String.raw`C:\Users\alice\project\missing.ts:12:3`,
343 + relativePath: "missing.ts:12:3",
344 + });
345 + });
346 +});
apps/web/src/markdownFileHostPath.ts new
+48
@@ -0,0 +1,48 @@
1 +import { formatFilePathPosition, resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
2 +
3 +import { workspaceRelativeFilePath } from "@t3tools/shared/path";
4 +import type { MarkdownFileLinkMeta } from "./markdown-links";
5 +
6 +interface MarkdownFileHostPath {
7 + absolutePath: string;
8 + matched: boolean;
9 +}
10 +
11 +export async function resolveMarkdownFileHostPath(input: {
12 + meta: MarkdownFileLinkMeta;
13 + cwd: string | undefined;
14 + findWorkspaceMatch: (workspaceRelativePath: string) => Promise<string | null>;
15 +}) {
16 + const { meta, cwd } = input;
17 + const match =
18 + meta.workspaceLookupPath && cwd
19 + ? await input.findWorkspaceMatch(meta.workspaceLookupPath)
20 + : null;
21 + return match && cwd
22 + ? { absolutePath: resolvePathLinkTarget(match, cwd), matched: true }
23 + : { absolutePath: meta.filePath, matched: false };
24 +}
25 +
26 +export function markdownFileEditorTarget(
27 + meta: MarkdownFileLinkMeta,
28 + hostPath: MarkdownFileHostPath,
29 +) {
30 + return hostPath.matched
31 + ? formatFilePathPosition({ ...meta, path: hostPath.absolutePath })
32 + : meta.targetPath;
33 +}
34 +
35 +export function markdownFileCopyPaths(
36 + meta: MarkdownFileLinkMeta,
37 + hostPath: MarkdownFileHostPath,
38 + cwd: string | undefined,
39 +) {
40 + const fullPath = markdownFileEditorTarget(meta, hostPath);
41 + return {
42 + fullPath,
43 + relativePath: formatFilePathPosition({
44 + ...meta,
45 + path: workspaceRelativeFilePath(hostPath.absolutePath, cwd) ?? meta.filePath,
46 + }),
47 + };
48 +}