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