Skip to content

Commit d6aa89e

Browse files
committed
feat(mobile): play audio and preview pdf and csv files inline in threads
1 parent b727408 commit d6aa89e

10 files changed

Lines changed: 659 additions & 95 deletions

‎apps/mobile/src/components/ui/AppSymbol.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ import IconPlug from "@tabler/icons-react-native/IconPlug";
9797
import IconPhoto from "@tabler/icons-react-native/IconPhoto";
9898
import IconPin from "@tabler/icons-react-native/IconPin";
9999
import IconPinnedOff from "@tabler/icons-react-native/IconPinnedOff";
100+
import IconPlayerPause from "@tabler/icons-react-native/IconPlayerPause";
100101
import IconPlayerPlay from "@tabler/icons-react-native/IconPlayerPlay";
101102
import IconPlayerStopFilled from "@tabler/icons-react-native/IconPlayerStopFilled";
102103
import IconPlus from "@tabler/icons-react-native/IconPlus";
@@ -234,6 +235,7 @@ const ANDROID_ICON_BY_SF_SYMBOL = {
234235
pin: IconPin,
235236
"pin.slash": IconPinnedOff,
236237
play: IconPlayerPlay,
238+
pause: IconPlayerPause,
237239
plus: IconPlus,
238240
minus: IconMinus,
239241
"qrcode.viewfinder": IconQrcode,

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

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import { Toolbar } from "../../components/ui/Toolbar";
22
import * as Dialog from "../../components/ui/Dialog";
3-
/* oxlint-disable react/no-array-index-key -- Captured table rows and columns have stable positions and may contain identical values. */
43
import { NativeStackScreenOptions } from "../../native/StackHeader";
54
import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
65
import type { MenuAction } from "@react-native-menu/menu";
76
import { EnvironmentId } from "@t3tools/contracts";
87
import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments";
98
import { useEffect, useRef, useState } from "react";
10-
import { Platform, ScrollView, View } from "react-native";
9+
import { Platform, View } from "react-native";
1110

1211
import {
1312
AndroidHeaderIconButton,
1413
AndroidScreenHeader,
1514
} from "../../components/ui/AndroidScreenHeader";
16-
import { AppText as Text } from "../../components/ui/AppText";
15+
import { DelimitedFilePreview } from "./DelimitedFilePreview";
1716
import { AudioFilePreview } from "./AudioFilePreview";
1817
import { Menu } from "../../components/ui/Menu";
1918
import { EmptyState } from "../../components/ui/EmptyState";
@@ -89,41 +88,7 @@ function AttachmentDocumentBody(props: {
8988
</FilePreviewNotice>
9089
) : null}
9190
{table && document.activeMode === "table" ? (
92-
<ScrollView className="flex-1">
93-
{table.truncated ? (
94-
<FilePreviewNotice>
95-
Table limited to the first 100 rows and 30 columns. Source shows the rest.
96-
</FilePreviewNotice>
97-
) : null}
98-
<ScrollView horizontal>
99-
<View>
100-
{table.rows.map((row, rowIndex) => (
101-
<View
102-
key={rowIndex}
103-
className={rowIndex === 0 ? "flex-row bg-subtle" : "flex-row"}
104-
>
105-
{row.map((cell, columnIndex) => (
106-
<View
107-
key={columnIndex}
108-
className="w-48 border-r border-b border-border px-3 py-2"
109-
>
110-
<Text
111-
selectable
112-
className={
113-
rowIndex === 0
114-
? "text-sm font-t3-semibold text-foreground"
115-
: "text-sm text-foreground"
116-
}
117-
>
118-
{cell}
119-
</Text>
120-
</View>
121-
))}
122-
</View>
123-
))}
124-
</View>
125-
</ScrollView>
126-
</ScrollView>
91+
<DelimitedFilePreview table={table} />
12792
) : document.activeMode === "markdown" && props.environmentId ? (
12893
<FileMarkdownPreview
12994
cwd=""
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* oxlint-disable react/no-array-index-key -- Captured table cells have stable positions and may contain identical values. */
2+
import { ScrollView, View } from "react-native";
3+
import type { parseDelimitedPreview } from "@t3tools/shared/delimitedPreview";
4+
import { AppText as Text } from "../../components/ui/AppText";
5+
import { FilePreviewNotice } from "./FilePreviewFeedback";
6+
7+
export function DelimitedFilePreview(props: {
8+
readonly table: ReturnType<typeof parseDelimitedPreview>;
9+
}) {
10+
return (
11+
<ScrollView className="flex-1">
12+
{props.table.truncated ? (
13+
<FilePreviewNotice>
14+
Table limited to the first 100 rows and 30 columns. Source shows the rest.
15+
</FilePreviewNotice>
16+
) : null}
17+
<ScrollView horizontal>
18+
<View>
19+
{props.table.rows.map((row, rowIndex) => (
20+
<View key={rowIndex} className={rowIndex === 0 ? "flex-row bg-subtle" : "flex-row"}>
21+
{row.map((cell, columnIndex) => (
22+
<View key={columnIndex} className="w-48 border-r border-b border-border px-3 py-2">
23+
<Text
24+
selectable
25+
className={
26+
rowIndex === 0
27+
? "text-sm font-t3-semibold text-foreground"
28+
: "text-sm text-foreground"
29+
}
30+
>
31+
{cell}
32+
</Text>
33+
</View>
34+
))}
35+
</View>
36+
))}
37+
</View>
38+
</ScrollView>
39+
</ScrollView>
40+
);
41+
}

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
import { mediaFileReference } from "@t3tools/client-runtime/media-reference";
1717

1818
import { ScreenContent } from "../../components/ui/ScreenContent";
19+
import { filePreviewDelimiter, parseDelimitedPreview } from "@t3tools/shared/delimitedPreview";
20+
import { DelimitedFilePreview } from "./DelimitedFilePreview";
1921
import { AudioFilePreview } from "./AudioFilePreview";
2022
import { EmptyState } from "../../components/ui/EmptyState";
2123
import { FilePreviewModal, type FilePreviewSource } from "../../components/FilePreviewModal";
@@ -203,7 +205,8 @@ function defaultViewMode(path: string | null): FileViewMode {
203205
(isWorkspaceBrowserPreviewPath(path) ||
204206
isWorkspaceImagePreviewPath(path) ||
205207
isVideoPreviewFile(path) ||
206-
isAudioPreviewFile(path))
208+
isAudioPreviewFile(path) ||
209+
filePreviewDelimiter({ name: path }) !== null)
207210
? "preview"
208211
: "source";
209212
}
@@ -233,6 +236,7 @@ function FileContent(props: {
233236
const reservedHeaderInset =
234237
Platform.OS === "ios" && props.truncated ? (columnMetrics?.safeArea.top ?? insets.top) : 0;
235238
const isMarkdown = isMarkdownPreviewFile(props.relativePath);
239+
const delimiter = filePreviewDelimiter({ name: props.relativePath });
236240
const isBrowserFile = isWorkspaceBrowserPreviewPath(props.relativePath);
237241
const isImageFile = isWorkspaceImagePreviewPath(props.relativePath);
238242
const isVideoFile = isVideoPreviewFile(props.relativePath);
@@ -308,7 +312,9 @@ function FileContent(props: {
308312
Preview limited to the first 1 MB of a truncated file.
309313
</FilePreviewNotice>
310314
) : null}
311-
{props.activeMode === "preview" && isMarkdown ? (
315+
{props.activeMode === "preview" && delimiter ? (
316+
<DelimitedFilePreview table={parseDelimitedPreview(props.fileContents, delimiter)} />
317+
) : props.activeMode === "preview" && isMarkdown ? (
312318
<FileMarkdownPreview
313319
cwd={props.cwd}
314320
environmentId={props.environmentId}
@@ -604,7 +610,8 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
604610
isBrowserFile ||
605611
isImageFile ||
606612
isVideoFile ||
607-
isAudioFile);
613+
isAudioFile ||
614+
filePreviewDelimiter({ name: relativePath }) !== null);
608615
const activeMode =
609616
relativePath !== null && modeOverride?.path === relativePath
610617
? modeOverride.mode
@@ -666,7 +673,9 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
666673
relativePath !== null &&
667674
!isVideoFile &&
668675
!isAudioFile &&
669-
(resolvedActiveMode === "source" || isMarkdownPreviewFile(relativePath));
676+
(resolvedActiveMode === "source" ||
677+
isMarkdownPreviewFile(relativePath) ||
678+
filePreviewDelimiter({ name: relativePath }) !== null);
670679
const fileAccessSession = useEnvironmentQuery(
671680
environmentId !== null ? environmentSession.sessionStateAtom(environmentId) : null,
672681
);
@@ -742,8 +751,8 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
742751
canToggleMode
743752
? ({
744753
id: "preview",
745-
title: "Preview",
746-
icon: "eye",
754+
title: filePreviewDelimiter({ name: relativePath }) ? "Table" : "Preview",
755+
icon: filePreviewDelimiter({ name: relativePath }) ? "tablecells" : "eye",
747756
inline: true,
748757
onPress: () => setModeOverride({ path: relativePath, mode: "preview" }),
749758
} as const)

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

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ import { assetImageSource } from "../../lib/assetImageSource";
105105
import { composerImageGallery } from "../../lib/composerImageGallery";
106106
import { messageImageGallery } from "./messageImageGallery";
107107
import { FilePreviewModal, type FilePreviewSource } from "../../components/FilePreviewModal";
108-
import { isPdfFile } from "../../lib/filePreview";
108+
import { inlineFilePreviewKind, isPdfFile } from "../../lib/filePreview";
109+
import { ThreadInlineFilePreview } from "./ThreadInlineFilePreview";
109110
import { flattenThemeColor } from "../../lib/mobileTheme";
110111
import { PresentationSource } from "../../components/ui/NativePresentation";
111112
import { useSafeAreaInsets } from "react-native-safe-area-context";
@@ -612,6 +613,55 @@ function MessageAttachmentFile(props: {
612613
})();
613614
};
614615

616+
const openAttachment = () =>
617+
isPdf
618+
? props.onPressPreview({
619+
kind: "pdf",
620+
name: attachment.name,
621+
environmentId: props.environmentId,
622+
resource: {
623+
_tag: "attachment",
624+
attachmentId: attachment.id,
625+
fileName: attachment.name,
626+
mimeType: "application/pdf",
627+
},
628+
sourceIdentifier,
629+
})
630+
: navigation.navigate("ThreadAttachment", {
631+
environmentId: String(props.environmentId),
632+
...(selectedThread ? { threadId: String(selectedThread.id) } : {}),
633+
attachmentId: attachment.id,
634+
name: attachment.name,
635+
mimeType: attachment.mimeType,
636+
sizeBytes: String(attachment.sizeBytes),
637+
});
638+
const inlineKind = inlineFilePreviewKind(attachment);
639+
if (inlineKind) {
640+
return (
641+
<PresentationSource identifier={sourceIdentifier}>
642+
<ThreadInlineFilePreview
643+
key={attachment.id}
644+
kind={inlineKind}
645+
source={{
646+
kind: inlineKind === "pdf" ? "pdf" : "document",
647+
name: attachment.name,
648+
mimeType: attachment.mimeType,
649+
environmentId: props.environmentId,
650+
resource: {
651+
_tag: "attachment",
652+
attachmentId: attachment.id,
653+
fileName: attachment.name,
654+
mimeType: attachment.mimeType,
655+
},
656+
}}
657+
sizeBytes={attachment.sizeBytes}
658+
onOpen={openAttachment}
659+
onLongPress={() => shareFile(sourceIdentifier)}
660+
/>
661+
</PresentationSource>
662+
);
663+
}
664+
615665
if (videoType !== null) {
616666
const sourceIdentifier = `attachment:${props.environmentId}:${attachment.id}`;
617667
return (
@@ -646,29 +696,7 @@ function MessageAttachmentFile(props: {
646696
disabled={opening || httpBaseUrl === null}
647697
className="min-w-0 flex-row items-center gap-3 rounded-xl border border-border bg-card p-3 active:bg-subtle"
648698
onLongPress={() => shareFile(sourceIdentifier)}
649-
onPress={() =>
650-
isPdf
651-
? props.onPressPreview({
652-
kind: "pdf",
653-
name: attachment.name,
654-
environmentId: props.environmentId,
655-
resource: {
656-
_tag: "attachment",
657-
attachmentId: attachment.id,
658-
fileName: attachment.name,
659-
mimeType: "application/pdf",
660-
},
661-
sourceIdentifier,
662-
})
663-
: navigation.navigate("ThreadAttachment", {
664-
environmentId: String(props.environmentId),
665-
...(selectedThread ? { threadId: String(selectedThread.id) } : {}),
666-
attachmentId: attachment.id,
667-
name: attachment.name,
668-
mimeType: attachment.mimeType,
669-
sizeBytes: String(attachment.sizeBytes),
670-
})
671-
}
699+
onPress={openAttachment}
672700
>
673701
<View className="h-12 w-10 shrink-0 items-center justify-center rounded-lg bg-subtle">
674702
{opening ? (
@@ -1808,6 +1836,17 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
18081836
void Haptics.selectionAsync();
18091837
if (media.kind === "video") {
18101838
setExpandedVideo((current) => current ?? media.source);
1839+
} else if (
1840+
(media.kind === "audio" || media.kind === "table") &&
1841+
"resource" in media.source &&
1842+
"path" in media.source.resource &&
1843+
typeof media.source.resource.path === "string"
1844+
) {
1845+
navigation.navigate("ThreadFile", {
1846+
environmentId: String(props.environmentId),
1847+
threadId: String(props.threadId),
1848+
path: fileRoutePathSegments(media.source.resource.path),
1849+
});
18111850
} else {
18121851
setExpandedFile((current) => current ?? media.source);
18131852
}
@@ -1897,6 +1936,16 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
18971936
/>
18981937
);
18991938
}
1939+
if (media && (media.kind === "audio" || media.kind === "pdf" || media.kind === "table")) {
1940+
return (
1941+
<ThreadInlineFilePreview
1942+
key={image.href}
1943+
kind={media.kind}
1944+
source={media.source}
1945+
onOpen={() => onMarkdownLinkPress(image.href, true)}
1946+
/>
1947+
);
1948+
}
19001949
const imageSource = classifyMarkdownImageSource(image.href, props.workspaceRoot ?? null);
19011950
if (imageSource._tag === "Direct") {
19021951
return (

0 commit comments

Comments
 (0)