Skip to content

Commit a9f9f73

Browse files
committed
fix(mobile): preserve file targets and pause hidden videos
1 parent a17963d commit a9f9f73

3 files changed

Lines changed: 84 additions & 95 deletions

File tree

‎apps/mobile/src/features/threads/ThreadMarkdownVideo.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export function ThreadMarkdownVideo(props: {
3838
name={source.name}
3939
thumbnailKey={thumbnailKey}
4040
thumbnailVisible={visible}
41+
paused={!visible}
4142
unavailable={"resource" in source && asset._tag === "Failure"}
4243
actionsSource={source.actionsSource}
4344
/>

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

Lines changed: 78 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EnvironmentId, ThreadId } from "@t3tools/contracts";
22
import { describe, expect, it } from "vite-plus/test";
33

44
import { resolveMarkdownMediaPreview } from "./markdownMedia";
5+
import { resolveMarkdownLinkPresentation } from "./markdownLinks";
56

67
const input = {
78
environmentId: EnvironmentId.make("environment-1"),
@@ -35,9 +36,9 @@ describe("resolveMarkdownMediaPreview", () => {
3536
});
3637

3738
it.each([
38-
["/tmp/frame%23one.png:12", "/tmp/frame#one.png"],
39-
["/tmp/frame%3Fone.png:12:3", "/tmp/frame?one.png"],
40-
["/tmp/frame%2523one.png:12", "/tmp/frame%23one.png"],
39+
["/tmp/frame%23one.png", "/tmp/frame#one.png"],
40+
["/tmp/frame%3Fone.png", "/tmp/frame?one.png"],
41+
["/tmp/frame%2523one.png", "/tmp/frame%23one.png"],
4142
["file://server/share/frame.png", "\\\\server\\share\\frame.png"],
4243
["\\\\server\\share\\frame.png", "\\\\server\\share\\frame.png"],
4344
])("keeps encoded filename and UNC semantics for %s", (href, path) => {
@@ -72,25 +73,16 @@ describe("resolveMarkdownMediaPreview", () => {
7273
});
7374
});
7475

75-
it.each([
76-
"./image.png",
77-
"./clip.mp4",
78-
"./report.pdf",
79-
"./sound.mp3",
80-
"./report.csv",
81-
"./report.tsv",
82-
"./report.docx",
83-
"./report.xlsx",
84-
"./report.pptx",
85-
"./report.unknown",
86-
"./LICENSE",
87-
])("opens relative media through the environment: %s", (href) => {
88-
const preview = resolveMarkdownMediaPreview(href, input);
89-
expect(preview?.source).toMatchObject({
90-
environmentId: input.environmentId,
91-
resource: { _tag: "media-file", threadId: input.threadId, path: `/repo/${href}` },
92-
});
93-
});
76+
it.each(["./image.png", "./clip.mp4", "./report.pdf", "./sound.mp3"])(
77+
"opens relative media through the environment: %s",
78+
(href) => {
79+
const preview = resolveMarkdownMediaPreview(href, input);
80+
expect(preview?.source).toMatchObject({
81+
environmentId: input.environmentId,
82+
resource: { _tag: "media-file", threadId: input.threadId, path: `/repo/${href}` },
83+
});
84+
},
85+
);
9486

9587
it("preserves a signed PDF URL and its authored name", () => {
9688
const href = "//cdn.example.com/report%20one.pdf?signature=a%2fb#page=2";
@@ -109,47 +101,26 @@ describe("resolveMarkdownMediaPreview", () => {
109101
});
110102

111103
it("opens encoded host PDFs without interpreting literal filename characters twice", () => {
112-
expect(resolveMarkdownMediaPreview("/tmp/report%23one.pdf:12#page=2", input)).toMatchObject({
104+
expect(resolveMarkdownMediaPreview("/tmp/report%23one.pdf#page=2", input)).toMatchObject({
113105
kind: "pdf",
114106
source: { resource: { path: "/tmp/report#one.pdf" }, srcFragment: "#page=2" },
115107
});
116108
});
117109

118-
it.each([
119-
"./image.png",
120-
"./clip.mp4",
121-
"./report.pdf",
122-
"./sound.mp3",
123-
"./report.csv",
124-
"./report.tsv",
125-
"./report.docx",
126-
"./report.xlsx",
127-
"./report.pptx",
128-
"./report.unknown",
129-
"./LICENSE",
130-
])("uses the draft workspace when no thread exists: %s", (href) => {
131-
expect(
132-
resolveMarkdownMediaPreview(href, { ...input, threadId: undefined })?.source,
133-
).toMatchObject({
134-
resource: { _tag: "draft-workspace-file", cwd: "/repo", path: `/repo/${href}` },
135-
});
136-
});
110+
it.each(["./image.png", "./clip.mp4", "./report.pdf", "./sound.mp3"])(
111+
"uses the draft workspace when no thread exists: %s",
112+
(href) => {
113+
expect(
114+
resolveMarkdownMediaPreview(href, { ...input, threadId: undefined })?.source,
115+
).toMatchObject({
116+
resource: { _tag: "draft-workspace-file", cwd: "/repo", path: `/repo/${href}` },
117+
});
118+
},
119+
);
137120

138121
it("keeps captured host media unavailable while allowing direct media", () => {
139122
const captured = { ...input, captured: true };
140-
for (const href of [
141-
"./image.png",
142-
"./clip.mp4",
143-
"./report.pdf",
144-
"./sound.mp3",
145-
"./report.csv",
146-
"./report.tsv",
147-
"./report.docx",
148-
"./report.xlsx",
149-
"./report.pptx",
150-
"./report.unknown",
151-
"./LICENSE",
152-
]) {
123+
for (const href of ["./image.png", "./clip.mp4", "./report.pdf", "./sound.mp3"]) {
153124
expect(resolveMarkdownMediaPreview(href, captured)).toBeNull();
154125
}
155126
expect(resolveMarkdownMediaPreview("https://cdn.example.com/report.pdf", captured)?.kind).toBe(
@@ -170,22 +141,21 @@ describe("resolveMarkdownMediaPreview", () => {
170141
},
171142
});
172143
});
173-
it.each([
174-
["/tmp/sound%23one.mp3", "audio", "audio/mpeg", "/tmp/sound#one.mp3"],
175-
["report.csv", "document", "text/csv", "/repo/report.csv"],
176-
["report.tsv", "document", "text/tab-separated-values", "/repo/report.tsv"],
177-
])("opens an authored embed through the host: %s", (href, kind, mimeType, path) => {
178-
expect(resolveMarkdownMediaPreview(href, { ...input, imageEmbed: true })).toMatchObject({
179-
kind,
180-
source: {
181-
kind: "document",
182-
name: path.split("/").at(-1),
183-
mimeType,
184-
environmentId: input.environmentId,
185-
resource: { _tag: "media-file", threadId: input.threadId, path },
186-
},
187-
});
188-
});
144+
it.each([["/tmp/sound%23one.mp3", "audio", "audio/mpeg", "/tmp/sound#one.mp3"]])(
145+
"opens an authored embed through the host: %s",
146+
(href, kind, mimeType, path) => {
147+
expect(resolveMarkdownMediaPreview(href, { ...input, imageEmbed: true })).toMatchObject({
148+
kind,
149+
source: {
150+
kind: "document",
151+
name: path.split("/").at(-1),
152+
mimeType,
153+
environmentId: input.environmentId,
154+
resource: { _tag: "media-file", threadId: input.threadId, path },
155+
},
156+
});
157+
},
158+
);
189159

190160
it.each([
191161
["sound%20one.mp3", "audio"],
@@ -214,31 +184,46 @@ describe("resolveMarkdownMediaPreview", () => {
214184
});
215185

216186
it.each([
187+
"src/main.ts",
188+
"src/main.tsx",
189+
"report.txt",
190+
"report.md",
191+
"report.html",
192+
"package.json",
217193
"report.csv",
218194
"report.tsv",
219195
"report.docx",
220196
"report.xlsx",
221197
"report.pptx",
222198
"report.unknown",
223199
"LICENSE",
224-
])("requests the same signed asset for file links and embeds: %s", (name) => {
200+
"Dockerfile",
201+
])("keeps source and unsupported host files on the file route: %s", (name) => {
225202
for (const href of [name, "./" + name, "/tmp/" + name, "file:///tmp/" + name]) {
226-
const path = href.startsWith("file:")
227-
? href.slice(7)
228-
: href.startsWith("/")
229-
? href
230-
: "/repo/" + href;
231203
for (const imageEmbed of [false, true]) {
232-
expect(resolveMarkdownMediaPreview(href, { ...input, imageEmbed })).toMatchObject({
233-
kind: "document",
234-
source: {
235-
kind: "document",
236-
name,
237-
environmentId: input.environmentId,
238-
resource: { _tag: "media-file", threadId: input.threadId, path },
239-
},
240-
});
204+
expect(resolveMarkdownMediaPreview(href, { ...input, imageEmbed })).toBeNull();
241205
}
206+
expect(resolveMarkdownLinkPresentation(href)).toMatchObject({ kind: "file" });
207+
}
208+
});
209+
210+
it.each([
211+
["src/main.ts:42", "/repo/src/main.ts", 42],
212+
["src/main.ts#L42C3", "/repo/src/main.ts", 42],
213+
["report.csv:142:3", "/repo/report.csv", 142],
214+
["/tmp/report.pdf:12#page=2", "/tmp/report.pdf", 12],
215+
["file:///tmp/sound.mp3#L12", "/tmp/sound.mp3", 12],
216+
["/tmp/frame%23one.png:12", "/tmp/frame#one.png", 12],
217+
["./clip.mp4#L12C3", "/repo/./clip.mp4", 12],
218+
["src/main.ts%3A42%3A3", "/repo/src/main.ts", 42],
219+
])("keeps positioned links on the file route: %s", (href, path, line) => {
220+
expect(resolveMarkdownMediaPreview(href, input)).toBeNull();
221+
const presentation = resolveMarkdownLinkPresentation(href);
222+
expect(presentation).toMatchObject({ kind: "file", line });
223+
if (presentation.kind === "file") {
224+
expect(
225+
presentation.path.startsWith("/") ? presentation.path : "/repo/" + presentation.path,
226+
).toBe(path);
242227
}
243228
});
244229

@@ -256,24 +241,22 @@ describe("resolveMarkdownMediaPreview", () => {
256241

257242
it("does not authorize local files without thread or draft context", () => {
258243
expect(
259-
resolveMarkdownMediaPreview("/tmp/report.docx", {
244+
resolveMarkdownMediaPreview("/tmp/report.pdf", {
260245
...input,
261246
threadId: undefined,
262247
workspaceRoot: null,
263248
}),
264249
).toBeNull();
265-
expect(
266-
resolveMarkdownMediaPreview("report.docx", { ...input, workspaceRoot: null }),
267-
).toBeNull();
250+
expect(resolveMarkdownMediaPreview("report.pdf", { ...input, workspaceRoot: null })).toBeNull();
268251
});
269252

270253
it("decodes document filenames once and preserves image and video embeds", () => {
271-
expect(resolveMarkdownMediaPreview("/tmp/report%23one%2520.docx", input)).toMatchObject({
272-
kind: "document",
254+
expect(resolveMarkdownMediaPreview("/tmp/report%23one%2520.pdf", input)).toMatchObject({
255+
kind: "pdf",
273256
source: {
274-
name: "report#one%20.docx",
275-
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
276-
resource: { path: "/tmp/report#one%20.docx" },
257+
name: "report#one%20.pdf",
258+
mimeType: "application/pdf",
259+
resource: { path: "/tmp/report#one%20.pdf" },
277260
},
278261
});
279262
expect(

‎apps/mobile/src/lib/markdownMedia.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
mediaMimeTypeFromExtension,
88
} from "@t3tools/shared/filePreview";
99
import { splitFilePathPosition } from "@t3tools/shared/fileLinks";
10+
import { parseMarkdownFileLink } from "@t3tools/shared/markdownLinks";
1011
import {
1112
mediaFileReference,
1213
mediaUrlReference,
@@ -46,6 +47,7 @@ export function resolveMarkdownMediaPreview(
4647
| { readonly kind: "pdf" | "audio" | "document"; readonly source: FilePreviewSource }
4748
| { readonly kind: "video"; readonly source: MediaVideoPreviewSource }
4849
| null {
50+
if (parseMarkdownFileLink(href)?.line !== undefined) return null;
4951
const classified = classifyMarkdownImageSource(href, input.workspaceRoot);
5052
if (classified._tag === "Direct" && /^data:(?!image\/|video\/)/i.test(classified.uri))
5153
return null;
@@ -61,6 +63,9 @@ export function resolveMarkdownMediaPreview(
6163
const isFile = classified._tag === "WorkspaceFile" || /^\.[a-z0-9]+$/i.test(extension);
6264
if (name && isFile && mediaMimeTypeFromExtension(extension) === null) {
6365
const audioMimeType = audioMimeTypeFromExtension(extension);
66+
// Host assets only serve supported media; other files use ThreadFile.
67+
if (classified._tag === "WorkspaceFile" && !audioMimeType && extension !== ".pdf")
68+
return null;
6469
const kind = audioMimeType ? "audio" : extension === ".pdf" ? "pdf" : "document";
6570
const mimeType =
6671
audioMimeType ?? DOCUMENT_MIME_TYPES[extension] ?? "application/octet-stream";

0 commit comments

Comments
 (0)