Skip to content

Commit 9e31c7d

Browse files
fix(clients): format copied media and workspace paths
Use shared host-path formatting for web and mobile media menus, workspace-header copies, and file-preview copies. Preserve URL payloads and refresh memoized mobile menu actions when the workspace directory changes. Add image, video, drive-path, UNC, and workspace-menu regression coverage.
1 parent f3abf50 commit 9e31c7d

8 files changed

Lines changed: 201 additions & 35 deletions

File tree

‎apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
mediaMimeTypeFromExtension,
1515
} from "@t3tools/shared/filePreview";
1616
import { mediaFileReference } from "@t3tools/client-runtime/media-reference";
17+
import { formatFilePathForClipboard } from "@t3tools/client-runtime/media-actions";
18+
import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
1719

1820
import { MaterialScreenContent } from "../../components/MaterialScreenContent";
1921
import { AudioFilePreview } from "../../components/AudioFilePreview";
@@ -819,7 +821,13 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
819821
title: "Copy path",
820822
icon: "doc.on.doc",
821823
inline: false,
822-
onPress: () => copyTextWithHaptic(relativePath),
824+
onPress: () =>
825+
copyTextWithHaptic(
826+
formatFilePathForClipboard(
827+
relativePath,
828+
resolvePathLinkTarget(relativePath, cwd ?? ""),
829+
),
830+
),
823831
} as const,
824832
]),
825833
// Selecting a long file by hand is painful on a phone, so copying the whole thing is
@@ -881,6 +889,7 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
881889
isBrowserFile,
882890
isImageFile,
883891
isVideoFile,
892+
cwd,
884893
relativePath,
885894
resolvedActiveMode,
886895
mediaSource,

‎apps/mobile/src/lib/mediaActions.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
type AuthSessionState,
66
} from "@t3tools/contracts";
77
import { AsyncResult } from "effect/reactivity";
8+
import { mediaFileReference } from "@t3tools/client-runtime/media-reference";
89
import { beforeEach, expect, it, vi } from "vite-plus/test";
910

1011
const state = vi.hoisted(() => ({
@@ -96,6 +97,70 @@ beforeEach(() => {
9697
state.copy.mockReset();
9798
});
9899

100+
it.each([
101+
[
102+
"C:/repo/assets/shot.png",
103+
"C:/repo",
104+
String.raw`C:\repo\assets\shot.png`,
105+
String.raw`assets\shot.png`,
106+
],
107+
[
108+
"C:/repo/assets/clip with spaces.mp4",
109+
"C:/repo",
110+
String.raw`C:\repo\assets\clip with spaces.mp4`,
111+
String.raw`assets\clip with spaces.mp4`,
112+
],
113+
[
114+
String.raw`\\server\share\repo/assets/shot.png`,
115+
String.raw`\\server\share\repo`,
116+
String.raw`\\server\share\repo\assets\shot.png`,
117+
String.raw`assets\shot.png`,
118+
],
119+
[
120+
String.raw`\\server\share\repo/assets/clip.mp4`,
121+
String.raw`\\server\share\repo`,
122+
String.raw`\\server\share\repo\assets\clip.mp4`,
123+
String.raw`assets\clip.mp4`,
124+
],
125+
["/repo/assets/shot.png", "/repo", "/repo/assets/shot.png", "assets/shot.png"],
126+
["/repo/assets/clip.mp4", "/repo", "/repo/assets/clip.mp4", "assets/clip.mp4"],
127+
] as const)("copies full and relative media paths for %s", (path, root, full, relative) => {
128+
const media = useMediaActions({
129+
environmentId,
130+
threadId,
131+
resource: { _tag: "media-file", threadId, path },
132+
reference: mediaFileReference(path, root),
133+
name: path.split(/[\\/]/).at(-1)!,
134+
mimeType: path.endsWith(".mp4") ? "video/mp4" : "image/png",
135+
});
136+
for (const [actionId, expected] of [
137+
["copy-full-path", full],
138+
["copy-relative-path", relative],
139+
] as const) {
140+
const action = media.actions.find(({ id }) => id === actionId);
141+
if (!action) throw new Error(`Missing media action: ${actionId}`);
142+
action.run();
143+
expect(state.copy).toHaveBeenLastCalledWith(expected);
144+
}
145+
expect(state.refresh).not.toHaveBeenCalled();
146+
expect(state.download).not.toHaveBeenCalled();
147+
});
148+
149+
it.each(["image/png", "video/mp4"])("preserves the %s URL when copying", (mimeType) => {
150+
const url = "https://cdn.test/assets/media?path=C:/repo/media";
151+
const media = useMediaActions({
152+
uri: url,
153+
name: "media",
154+
mimeType,
155+
reference: { kind: "url", url },
156+
});
157+
const action = media.actions.find(({ id }) => id === "copy-url");
158+
if (!action) throw new Error("Copy URL action missing");
159+
action.run();
160+
expect(state.copy).toHaveBeenCalledWith(url);
161+
expect(state.refresh).not.toHaveBeenCalled();
162+
});
163+
99164
it.each(["workspace-file", "media-file"] as const)(
100165
"waits for the %s grant before enabling host menu actions",
101166
(_tag) => {

‎apps/mobile/src/state/mediaActions.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { MediaActionsSource } from "../lib/mediaActionsSource";
22
import { useNavigation } from "@react-navigation/native";
3-
import type { MediaActionId } from "@t3tools/client-runtime/media-actions";
3+
import {
4+
formatFilePathForClipboard,
5+
type MediaActionId,
6+
} from "@t3tools/client-runtime/media-actions";
47
import {
58
AuthFilesystemReadScope,
69
type EnvironmentId,
@@ -108,16 +111,17 @@ export function useMediaActions(source: MediaActionsSource | undefined, onOpenFi
108111
{
109112
id: "copy-full-path" as const,
110113
title: "Copy full path",
111-
run: () => copyTextWithHaptic(reference.path),
114+
run: () => copyTextWithHaptic(formatFilePathForClipboard(reference.path)),
112115
},
113116
]
114117
: []),
115-
...(relativePath
118+
...(relativePath && reference?.kind === "file"
116119
? [
117120
{
118121
id: "copy-relative-path" as const,
119122
title: "Copy relative path",
120-
run: () => copyTextWithHaptic(relativePath),
123+
run: () =>
124+
copyTextWithHaptic(formatFilePathForClipboard(relativePath, reference.path)),
121125
},
122126
]
123127
: []),

‎apps/web/src/components/BranchToolbar.panel.test.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ it.each([
9090
["local", null, "/tmp/project"],
9191
["worktree", null, null],
9292
["worktree", "/tmp/worktree", "/tmp/worktree"],
93+
["worktree", "C:/repo/worktree", String.raw`C:\repo\worktree`],
94+
["worktree", String.raw`\\server\share\repo/worktree`, String.raw`\\server\share\repo\worktree`],
9395
] as const)(
9496
"copies only an existing workspace path with mode %s and worktree %s",
9597
async (envMode, worktreePath, copiedPath) => {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { writeTextToClipboard } from "../hooks/useCopyToClipboard";
2+
import { formatFilePathForClipboard } from "@t3tools/client-runtime/media-actions";
23
import { readLocalApi } from "../localApi";
34
import { stackedThreadToast, toastManager } from "./ui/toast";
45
import { ComposerContextLabel } from "./ComposerContextLabel";
@@ -115,13 +116,14 @@ function showWorkspaceContextMenu(event: ReactMouseEvent, workspacePath: string)
115116
})
116117
.then((action) => {
117118
if (action !== "copy-path") return;
118-
void writeTextToClipboard(workspacePath, "workspace path").then(
119+
const clipboardPath = formatFilePathForClipboard(workspacePath);
120+
void writeTextToClipboard(clipboardPath, "workspace path").then(
119121
(didCopy) => {
120122
if (didCopy) {
121123
toastManager.add({
122124
type: "success",
123125
title: "Path copied",
124-
description: workspacePath,
126+
description: clipboardPath,
125127
});
126128
}
127129
},

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

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { ThreadFind, ThreadFindCanvas, type ThreadFindControls } from "./chat/ThreadFindProvider";
2+
import { formatFilePathForClipboard } from "@t3tools/client-runtime/media-actions";
3+
import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
24
import { THREAD_FIND_BAR_RESERVED_HEIGHT } from "./chat/ThreadFindBar";
35
import { usageLimitRecoveryBannerItem } from "./chat/UsageLimitRecoveryBanner";
46
import {
@@ -6274,37 +6276,45 @@ export default function ChatView(props: ChatViewProps) {
62746276
},
62756277
[activeThreadRef],
62766278
);
6277-
const copyRightPanelFilePath = useCallback((relativePath: string) => {
6278-
if (typeof window === "undefined" || !navigator.clipboard?.writeText) {
6279-
toastManager.add(
6280-
stackedThreadToast({
6281-
type: "error",
6282-
title: "Failed to copy path",
6283-
description: "Clipboard API unavailable.",
6284-
}),
6285-
);
6286-
return;
6287-
}
6288-
6289-
void navigator.clipboard.writeText(relativePath).then(
6290-
() => {
6291-
toastManager.add({
6292-
type: "success",
6293-
title: "Path copied",
6294-
description: relativePath,
6295-
});
6296-
},
6297-
(error) => {
6279+
const fileCopyCwd = gitCwd ?? activeProjectCwd ?? "";
6280+
const copyRightPanelFilePath = useCallback(
6281+
(relativePath: string) => {
6282+
if (typeof window === "undefined" || !navigator.clipboard?.writeText) {
62986283
toastManager.add(
62996284
stackedThreadToast({
63006285
type: "error",
63016286
title: "Failed to copy path",
6302-
description: error instanceof Error ? error.message : "An error occurred.",
6287+
description: "Clipboard API unavailable.",
63036288
}),
63046289
);
6305-
},
6306-
);
6307-
}, []);
6290+
return;
6291+
}
6292+
6293+
const clipboardPath = formatFilePathForClipboard(
6294+
relativePath,
6295+
resolvePathLinkTarget(relativePath, fileCopyCwd),
6296+
);
6297+
void navigator.clipboard.writeText(clipboardPath).then(
6298+
() => {
6299+
toastManager.add({
6300+
type: "success",
6301+
title: "Path copied",
6302+
description: clipboardPath,
6303+
});
6304+
},
6305+
(error) => {
6306+
toastManager.add(
6307+
stackedThreadToast({
6308+
type: "error",
6309+
title: "Failed to copy path",
6310+
description: error instanceof Error ? error.message : "An error occurred.",
6311+
}),
6312+
);
6313+
},
6314+
);
6315+
},
6316+
[fileCopyCwd],
6317+
);
63086318
useEffect(
63096319
() =>
63106320
subscribePreviewAction((action) => {

‎apps/web/src/components/media/MediaActions.test.tsx‎

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
type AuthSessionState,
66
} from "@t3tools/contracts";
77
import { AsyncResult } from "effect/reactivity";
8+
import { mediaFileReference } from "@t3tools/client-runtime/media-reference";
89
import { createElement, isValidElement, type ReactNode } from "react";
910
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
1011

@@ -24,6 +25,7 @@ const state = vi.hoisted(() => ({
2425
showMenu: vi.fn(),
2526
openFile: vi.fn(),
2627
clipboard: vi.fn(),
28+
copyText: vi.fn(),
2729
menuFinished: null as (() => void) | null,
2830
}));
2931

@@ -33,7 +35,7 @@ vi.mock("react", async (importOriginal) => ({
3335
useRef: <A,>(current: A) => ({ current }),
3436
useState: <A,>(initial: A) => [initial, () => {}],
3537
}));
36-
vi.mock("../../hooks/useCopyToClipboard", () => ({ writeTextToClipboard: vi.fn() }));
38+
vi.mock("../../hooks/useCopyToClipboard", () => ({ writeTextToClipboard: state.copyText }));
3739
vi.mock("../../localApi", () => ({
3840
readLocalApi: () => ({ contextMenu: { show: state.showMenu } }),
3941
}));
@@ -128,6 +130,7 @@ beforeEach(() => {
128130
state.png.mockReset().mockResolvedValue(new Blob(["png"], { type: "image/png" }));
129131
state.showMenu.mockReset().mockResolvedValue(null);
130132
state.openFile.mockReset();
133+
state.copyText.mockReset().mockResolvedValue(true);
131134
state.menuFinished = null;
132135
class TestClipboardItem {
133136
constructor(readonly items: Record<string, Promise<Blob>>) {}
@@ -141,6 +144,72 @@ beforeEach(() => {
141144

142145
afterEach(() => vi.unstubAllGlobals());
143146

147+
it.each([
148+
[
149+
"image",
150+
"C:/repo/assets/shot.png",
151+
"C:/repo",
152+
String.raw`C:\repo\assets\shot.png`,
153+
String.raw`assets\shot.png`,
154+
],
155+
[
156+
"video",
157+
"C:/repo/assets/clip with spaces.mp4",
158+
"C:/repo",
159+
String.raw`C:\repo\assets\clip with spaces.mp4`,
160+
String.raw`assets\clip with spaces.mp4`,
161+
],
162+
[
163+
"image",
164+
String.raw`\\server\share\repo/assets/shot.png`,
165+
String.raw`\\server\share\repo`,
166+
String.raw`\\server\share\repo\assets\shot.png`,
167+
String.raw`assets\shot.png`,
168+
],
169+
[
170+
"video",
171+
String.raw`\\server\share\repo/assets/clip.mp4`,
172+
String.raw`\\server\share\repo`,
173+
String.raw`\\server\share\repo\assets\clip.mp4`,
174+
String.raw`assets\clip.mp4`,
175+
],
176+
["image", "/repo/assets/shot.png", "/repo", "/repo/assets/shot.png", "assets/shot.png"],
177+
["video", "/repo/assets/clip.mp4", "/repo", "/repo/assets/clip.mp4", "assets/clip.mp4"],
178+
] as const)(
179+
"copies full and relative paths for %s %s",
180+
async (kind, path, root, full, relative) => {
181+
const source: MediaActionSource = {
182+
...hostSource(),
183+
kind,
184+
reference: mediaFileReference(path, root),
185+
};
186+
for (const [action, expected] of [
187+
["copy-full-path", full],
188+
["copy-relative-path", relative],
189+
] as const) {
190+
const completed = deferred<void>();
191+
state.menuFinished = () => completed.resolve();
192+
state.showMenu.mockResolvedValue(action);
193+
openMenu(source);
194+
await completed.promise;
195+
expect(state.copyText).toHaveBeenLastCalledWith(expected, "file path");
196+
}
197+
expect(state.mint).not.toHaveBeenCalled();
198+
expect(state.download).not.toHaveBeenCalled();
199+
},
200+
);
201+
202+
it.each(["image", "video"] as const)("preserves the %s URL when copying", async (kind) => {
203+
const url = `https://cdn.test/assets/${kind === "image" ? "shot.png" : "clip.mp4"}?path=C:/repo/media`;
204+
const completed = deferred<void>();
205+
state.menuFinished = () => completed.resolve();
206+
state.showMenu.mockResolvedValue("copy-url");
207+
openMenu({ ...hostSource(), kind, src: url, reference: { kind: "url", url } });
208+
await completed.promise;
209+
expect(state.copyText).toHaveBeenCalledWith(url, "URL");
210+
expect(state.mint).not.toHaveBeenCalled();
211+
});
212+
144213
it.each(["workspace-file", "media-file"] as const)(
145214
"waits for the %s grant before enabling host menu actions",
146215
(_tag) => {

‎apps/web/src/components/media/MediaActions.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import type { MediaActionId } from "@t3tools/client-runtime/media-actions";
1+
import {
2+
formatFilePathForClipboard,
3+
type MediaActionId,
4+
} from "@t3tools/client-runtime/media-actions";
25
import {
36
mediaReferenceFileName,
47
type MediaReference,
@@ -171,7 +174,9 @@ export function MediaActions({
171174
? reference.url
172175
: undefined;
173176
if (text !== undefined) {
174-
await writeTextToClipboard(text, reference?.kind === "file" ? "file path" : "URL");
177+
const clipboardText =
178+
reference?.kind === "file" ? formatFilePathForClipboard(text, reference.path) : text;
179+
await writeTextToClipboard(clipboardText, reference?.kind === "file" ? "file path" : "URL");
175180
toastManager.add({
176181
type: "success",
177182
title: action === "copy-url" ? "URL copied" : "Path copied",

0 commit comments

Comments
 (0)