Skip to content

Commit 14da10c

Browse files
committed
fix(mobile): route document embeds to file viewers
1 parent a12e83c commit 14da10c

5 files changed

Lines changed: 136 additions & 19 deletions

File tree

‎apps/mobile/src/components/FilePreviewModal.tsx‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import {
1212
import { Keyboard } from "react-native";
1313

1414
import { loadLocalAttachmentPreview } from "../lib/localAttachmentPreview";
15+
import {
16+
downloadAndShareAttachment,
17+
nativeViewerErrorMessage,
18+
shareLocalAttachment,
19+
} from "../lib/attachmentDownload";
1520
import { useRefreshAssetUrl } from "../state/assets";
1621
import { filePreviewGallery, filePreviewKey, filePreviewName } from "../lib/filePreviewGallery";
1722
import { FilePreviewGallery } from "./FilePreviewGallery";
@@ -123,6 +128,32 @@ function ResolvedFilePreview(props: {
123128
[items, resolved],
124129
);
125130
const ready = selected && gallery.length === items.length;
131+
const onOpenError = (error: unknown) => {
132+
if (props.onOpenError) return props.onOpenError(error);
133+
Dialog.alert("Could not open preview", nativeViewerErrorMessage(error), [
134+
{ text: "Cancel", style: "cancel" },
135+
...(selected?.kind === "document"
136+
? [
137+
{
138+
text: "Save or share",
139+
onPress: () => {
140+
const input = {
141+
attachment: {
142+
name: filePreviewName(selected),
143+
mimeType: selected.mimeType ?? "application/octet-stream",
144+
},
145+
signal: new AbortController().signal,
146+
};
147+
const sharing = /^(file|content):/.test(selected.uri)
148+
? shareLocalAttachment({ ...input, uri: selected.uri })
149+
: downloadAndShareAttachment({ ...input, url: selected.uri });
150+
void sharing.catch(() => Dialog.alert("Could not share file", "Please try again."));
151+
},
152+
},
153+
]
154+
: []),
155+
]);
156+
};
126157
return (
127158
<>
128159
{items.map((item) => (
@@ -138,7 +169,7 @@ function ResolvedFilePreview(props: {
138169
<FilePreview
139170
source={{ ...selected, gallery }}
140171
onRequestClose={props.onRequestClose}
141-
onOpenError={props.onOpenError}
172+
onOpenError={onOpenError}
142173
/>
143174
) : null}
144175
</>

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
33
import { resolveMobileMarkdownMediaSource } from "../../lib/markdownMediaSource";
44
import { getBrowseDirectoryPath } from "@t3tools/client-runtime/state/projects";
55
import { useState } from "react";
6+
import { StackActions, useNavigation } from "@react-navigation/native";
67
import { RefreshControl, ScrollView, View } from "react-native";
78

89
import { tryOpenExternalUrl } from "../../lib/openExternalUrl";
@@ -16,7 +17,11 @@ import {
1617
} from "../threads/ThreadMarkdownImage";
1718
import { ThreadMarkdownVideo } from "../threads/ThreadMarkdownVideo";
1819
import { resolveMarkdownMediaPreview } from "../../lib/markdownMedia";
19-
import { normalizeNativeMarkdownUrl } from "../../lib/markdownLinks";
20+
import {
21+
normalizeNativeMarkdownUrl,
22+
resolveMarkdownLinkPresentation,
23+
} from "../../lib/markdownLinks";
24+
import { AppText as Text } from "../../components/ui/AppText";
2025
import { VideoPreviewModal, type VideoPreviewSource } from "../../components/VideoPreviewModal";
2126
import { FilePreviewModal, type FilePreviewSource } from "../../components/FilePreviewModal";
2227
import { useMarkdownImageSource } from "../../native/useMarkdownImageSource";
@@ -26,7 +31,7 @@ import {
2631
type MarkdownImageRenderer,
2732
type NativeMarkdownTextStyle,
2833
} from "../../native/SelectableMarkdownText";
29-
import { resolveWorkspaceFilePath } from "./filePath";
34+
import { fileRoutePathSegments, resolveWorkspaceFilePath } from "./filePath";
3035

3136
interface MarkdownPreviewStyles {
3237
readonly nativeTextStyle: NativeMarkdownTextStyle;
@@ -79,6 +84,7 @@ export function FileMarkdownPreview(props: {
7984
readonly threadId: ThreadId | null;
8085
readonly onRefresh?: () => Promise<void> | void;
8186
}) {
87+
const navigation = useNavigation();
8288
const [isPullRefreshing, setIsPullRefreshing] = useState(false);
8389
const [expandedFile, setExpandedFile] = useState<FilePreviewSource | null>(null);
8490
const [expandedVideo, setExpandedVideo] = useState<VideoPreviewSource | null>(null);
@@ -107,6 +113,24 @@ export function FileMarkdownPreview(props: {
107113
captured: props.captured,
108114
imageEmbed: true,
109115
});
116+
const fileLink = resolveMarkdownLinkPresentation(image.href);
117+
if (
118+
(preview && preview.kind !== "image" && preview.kind !== "video") ||
119+
(!props.captured && fileLink.kind === "file" && fileLink.line !== undefined)
120+
) {
121+
const name =
122+
preview?.source.name ?? (fileLink.kind === "file" ? fileLink.label : "Open file");
123+
return (
124+
<Text
125+
accessibilityRole="link"
126+
accessibilityLabel={`Open ${name}`}
127+
onPress={() => openMedia(image.href, true)}
128+
style={{ color: styles.nativeTextStyle.linkColor }}
129+
>
130+
{name}
131+
</Text>
132+
);
133+
}
110134
if (media?.access === "direct") {
111135
if (media.kind === "video") {
112136
return (
@@ -170,7 +194,21 @@ export function FileMarkdownPreview(props: {
170194
captured: props.captured,
171195
imageEmbed,
172196
});
173-
if (!media) return false;
197+
if (!media || media.kind === "file") {
198+
const fileLink = resolveMarkdownLinkPresentation(href);
199+
if (props.captured || fileLink.kind !== "file") return false;
200+
const params = {
201+
environmentId: String(props.environmentId),
202+
path: fileRoutePathSegments(resolveWorkspaceFilePath(markdownDirectory, fileLink.path)),
203+
...(fileLink.line ? { line: String(fileLink.line) } : {}),
204+
};
205+
if (props.threadId) {
206+
navigation.navigate("ThreadFile", { ...params, threadId: String(props.threadId) });
207+
} else {
208+
navigation.dispatch(StackActions.push("NewTaskFile", { ...params, cwd: props.cwd }));
209+
}
210+
return true;
211+
}
174212
if (media.kind === "video") setExpandedVideo(media.source);
175213
else setExpandedFile(media.source);
176214
return true;

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
18071807
workspaceRoot: props.workspaceRoot,
18081808
imageEmbed,
18091809
});
1810-
if (media) {
1810+
if (media && media.kind !== "file") {
18111811
void Haptics.selectionAsync();
18121812
if (media.kind === "video") {
18131813
setExpandedVideo((current) => current ?? media.source);
@@ -1909,19 +1909,24 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
19091909
/>
19101910
);
19111911
}
1912-
if (media && (media.kind === "pdf" || media.kind === "document")) {
1912+
const fileLink = resolveMarkdownLinkPresentation(image.href);
1913+
if (
1914+
(media && (media.kind === "pdf" || media.kind === "document" || media.kind === "file")) ||
1915+
(fileLink.kind === "file" && fileLink.line !== undefined)
1916+
) {
1917+
const name = media?.source.name ?? (fileLink.kind === "file" ? fileLink.label : "Open file");
19131918
return (
19141919
<Text
19151920
accessibilityRole="link"
1916-
accessibilityLabel={`Open ${media.source.name ?? "file"}`}
1921+
accessibilityLabel={`Open ${name}`}
19171922
onPress={() => onMarkdownLinkPress(image.href, true)}
19181923
style={{
19191924
color: markdownStyles.assistant.nativeTextStyle.linkColor,
19201925
fontSize: markdownStyles.assistant.nativeTextStyle.fontSize,
19211926
lineHeight: markdownStyles.assistant.nativeTextStyle.lineHeight,
19221927
}}
19231928
>
1924-
{image.alt || media.source.name || "Open file"}
1929+
{name}
19251930
</Text>
19261931
);
19271932
}

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

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,21 +204,58 @@ describe("resolveMarkdownMediaPreview", () => {
204204
"package.json",
205205
"report.csv",
206206
"report.tsv",
207-
"report.docx",
208-
"report.xlsx",
209-
"report.pptx",
210-
"report.unknown",
211207
"LICENSE",
212208
"Dockerfile",
213-
])("keeps source and unsupported host files on the file route: %s", (name) => {
209+
])("routes text and table links and embeds to the file screen: %s", (name) => {
214210
for (const href of [name, "./" + name, "/tmp/" + name, "file:///tmp/" + name]) {
215211
for (const imageEmbed of [false, true]) {
216-
expect(resolveMarkdownMediaPreview(href, { ...input, imageEmbed })).toBeNull();
212+
expect(resolveMarkdownMediaPreview(href, { ...input, imageEmbed })).toMatchObject({
213+
kind: "file",
214+
source: { name: name.split("/").at(-1) },
215+
});
217216
}
218217
expect(resolveMarkdownLinkPresentation(href)).toMatchObject({ kind: "file" });
219218
}
220219
});
221220

221+
it.each(["doc", "docx", "xls", "xlsx", "ppt", "pptx", "unknown"])(
222+
"opens host %s links and embeds in the native viewer",
223+
(extension) => {
224+
const name = `report.${extension}`;
225+
for (const href of [name, `./${name}`, `/tmp/${name}`, `file:///tmp/${name}`]) {
226+
for (const imageEmbed of [false, true]) {
227+
expect(resolveMarkdownMediaPreview(href, { ...input, imageEmbed })).toMatchObject({
228+
kind: "document",
229+
source: {
230+
kind: "document",
231+
name,
232+
resource: { _tag: "media-file", threadId: input.threadId },
233+
},
234+
});
235+
}
236+
}
237+
},
238+
);
239+
240+
it.each(["report.csv", "report.tsv", "report.docx", "report.xlsx", "report.unknown"])(
241+
"preserves draft, captured, and encoded document paths: %s",
242+
(name) => {
243+
const href = `/tmp/one%23two%2520.${name.split(".").at(-1)}`;
244+
const preview = resolveMarkdownMediaPreview(href, { ...input, imageEmbed: true });
245+
expect(preview?.source).toMatchObject({
246+
name: `one#two%20.${name.split(".").at(-1)}`,
247+
resource: { path: decodeURIComponent(href) },
248+
});
249+
expect(
250+
resolveMarkdownMediaPreview(href, { ...input, threadId: undefined })?.source,
251+
).toMatchObject({ resource: { _tag: "draft-workspace-file", cwd: "/repo" } });
252+
expect(resolveMarkdownMediaPreview(href, { ...input, captured: true })).toBeNull();
253+
expect(
254+
resolveMarkdownMediaPreview(href, { ...input, threadId: undefined, workspaceRoot: null }),
255+
).toBeNull();
256+
},
257+
);
258+
222259
it.each([
223260
["src/main.ts:42", "/repo/src/main.ts", 42],
224261
["src/main.ts#L42C3", "/repo/src/main.ts", 42],

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
} from "@t3tools/client-runtime/markdown-images";
55
import {
66
audioMimeTypeFromExtension,
7+
filePreviewKind,
78
mediaMimeTypeFromExtension,
89
} from "@t3tools/shared/filePreview";
910
import { splitFilePathPosition } from "@t3tools/shared/fileLinks";
@@ -44,7 +45,7 @@ export function resolveMarkdownMediaPreview(
4445
},
4546
):
4647
| { readonly kind: "image"; readonly source: FilePreviewSource }
47-
| { readonly kind: "pdf" | "audio" | "document"; readonly source: FilePreviewSource }
48+
| { readonly kind: "pdf" | "audio" | "document" | "file"; readonly source: FilePreviewSource }
4849
| { readonly kind: "video"; readonly source: MediaVideoPreviewSource }
4950
| null {
5051
if (parseMarkdownFileLink(href)?.line !== undefined) return null;
@@ -71,10 +72,15 @@ export function resolveMarkdownMediaPreview(
7172
audioMimeTypeFromExtension(extension) !== null)
7273
) {
7374
const audioMimeType = audioMimeTypeFromExtension(extension);
74-
// Host assets only serve supported media; other files use ThreadFile.
75-
if (classified._tag === "WorkspaceFile" && !audioMimeType && extension !== ".pdf")
76-
return null;
77-
const kind = audioMimeType ? "audio" : extension === ".pdf" ? "pdf" : "document";
75+
const previewKind = filePreviewKind({ name });
76+
const kind = audioMimeType
77+
? "audio"
78+
: extension === ".pdf"
79+
? "pdf"
80+
: classified._tag === "WorkspaceFile" &&
81+
(previewKind === "text" || previewKind === "markdown" || previewKind === "html")
82+
? "file"
83+
: "document";
7884
const mimeType =
7985
audioMimeType ?? DOCUMENT_MIME_TYPES[extension] ?? "application/octet-stream";
8086
const common = {

0 commit comments

Comments
 (0)