Open History to see the last commit
1import type { EnvironmentId } from "@t3tools/contracts";
2import { useGT } from "gt-react-native";
3import { View } from "react-native";
4
5import { EmptyState } from "../../components/ui/EmptyState";
6import { useContentTopInset } from "../../components/ui/useBarInset";
7import type { useAttachmentDocument } from "../../lib/attachmentDocument";
8import { HtmlRenderWebView } from "../threads/HtmlRenderWebView";
9import { AudioFilePreview } from "./AudioFilePreview";
10import { DelimitedFilePreview } from "./DelimitedFilePreview";
11import { EpubFilePreview } from "./EpubFilePreview";
12import { FileMarkdownPreview } from "./FileMarkdownPreview";
13import { FilePreviewLoading, FilePreviewNotice } from "./FilePreviewFeedback";
14import { SourceFileSurface } from "./SourceFileSurface";
15import { WorkspaceFileWebPreview } from "./WorkspaceFileWebPreview";
16
17export function nativeViewerKind(kind: ReturnType<typeof useAttachmentDocument>["kind"]) {
18 if (kind === "image") return "image" as const;
19 if (kind === "pdf") return "pdf" as const;
20 if (kind === "video" || kind === "unsupported") return "document" as const;
21 return null;
22}
23
24export function AttachmentDocumentBody(props: {
25 readonly document: Pick<
26 ReturnType<typeof useAttachmentDocument>,
27 | "kind"
28 | "activeMode"
29 | "uri"
30 | "content"
31 | "table"
32 | "error"
33 | "needsText"
34 | "retry"
35 | "revision"
36 >;
37 readonly name: string;
38 readonly environmentId: EnvironmentId | null;
39 readonly htmlRender: boolean;
40 readonly nativeViewer: "pending" | "open" | "unavailable" | null;
41 readonly nativeError: string | null;
42 readonly onOpenNative: () => void;
43}) {
44 const { document } = props;
45 const gt = useGT();
46 const headerInset = useContentTopInset();
47 if (document.error) {
48 return (
49 <View className="flex-1 items-center justify-center bg-sheet px-6">
50 <EmptyState
51 title={gt`File unavailable`}
52 detail={document.error}
53 actionLabel={gt`Try again`}
54 onAction={document.retry}
55 />
56 </View>
57 );
58 }
59 if (props.nativeViewer !== null && props.nativeViewer !== "unavailable") {
60 return <FilePreviewLoading message={gt`Opening in file viewer...`} />;
61 }
62 // ZIP text entries are read into memory and have no file URI.
63 if (document.needsText ? !document.content : !document.uri) {
64 return <FilePreviewLoading message={gt`Loading file...`} />;
65 }
66 if (document.needsText && document.content) {
67 const { content, table } = document;
68 return (
69 <View className="flex-1 bg-sheet" style={{ paddingTop: content.truncated ? headerInset : 0 }}>
70 {content.truncated ? (
71 <FilePreviewNotice title={gt`Partial file`}>
72 {gt`Preview limited to the first 1 MB. Save or share the file to read it in full.`}
73 </FilePreviewNotice>
74 ) : null}
75 {table && document.activeMode === "table" ? (
76 <DelimitedFilePreview table={table} />
77 ) : document.activeMode === "markdown" && props.environmentId ? (
78 <FileMarkdownPreview
79 cwd=""
80 relativePath=""
81 environmentId={props.environmentId}
82 threadId={null}
83 markdown={content.text}
84 captured
85 />
86 ) : (
87 <SourceFileSurface contents={content.text} path={props.name} selectable />
88 )}
89 </View>
90 );
91 }
92 if (!document.uri) return null;
93 if (document.kind === "epub")
94 return <EpubFilePreview key={document.uri} uri={document.uri} name={props.name} />;
95 if (document.kind === "audio") {
96 return <AudioFilePreview key={document.revision} uri={document.uri} onRetry={document.retry} />;
97 }
98 if (document.kind === "html") {
99 return props.htmlRender ? (
100 <HtmlRenderWebView key={document.uri} uri={document.uri} title={props.name} nested={false} />
101 ) : (
102 <WorkspaceFileWebPreview uri={document.uri} />
103 );
104 }
105 return (
106 <View className="flex-1 items-center justify-center bg-sheet px-6">
107 <EmptyState
108 title={gt`No preview for this file`}
109 detail={
110 props.nativeError ??
111 gt`No app on this device can show this format. Save or share it to open it elsewhere.`
112 }
113 actionLabel={gt`Try again`}
114 onAction={props.onOpenNative}
115 />
116 </View>
117 );
118}