Skip to content

Commit 420bc45

Browse files
fix(mobile): attachment document body lives in its own module, breaking the ZIP view import cycle
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8822ffa commit 420bc45

3 files changed

Lines changed: 113 additions & 108 deletions

File tree

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

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

Lines changed: 1 addition & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ import {
1515
AndroidHeaderIconButton,
1616
AndroidScreenHeader,
1717
} from "../../components/ui/AndroidScreenHeader";
18-
import { DelimitedFilePreview } from "./DelimitedFilePreview";
19-
import { AudioFilePreview } from "./AudioFilePreview";
2018
import { Menu } from "../../components/ui/Menu";
21-
import { EmptyState } from "../../components/ui/EmptyState";
2219
import { FilePreviewModal, type FilePreviewSource } from "../../components/FilePreviewModal";
2320
import { useAttachmentDocument } from "../../lib/attachmentDocument";
2421
import { nativeViewerErrorMessage } from "../../lib/attachmentDownload";
@@ -29,13 +26,9 @@ import { removeComposerDraftAttachment, useComposerDraft } from "../../state/use
2926
import { useEnvironmentServerConfig } from "../../state/entities";
3027
import { useRefreshAssetUrl } from "../../state/assets";
3128
import { scopedThreadKey } from "../../lib/scopedEntities";
32-
import { FilePreviewLoading, FilePreviewNotice } from "./FilePreviewFeedback";
33-
import { FileMarkdownPreview } from "./FileMarkdownPreview";
3429
import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
35-
import { SourceFileSurface } from "./SourceFileSurface";
36-
import { WorkspaceFileWebPreview } from "./WorkspaceFileWebPreview";
37-
import { HtmlRenderWebView } from "../threads/HtmlRenderWebView";
3830
import { isZipFile, ZipArchiveView } from "./ZipArchiveView";
31+
import { AttachmentDocumentBody, nativeViewerKind } from "./AttachmentDocumentBody";
3932

4033
export type AttachmentFileRouteParams = {
4134
readonly environmentId?: string;
@@ -50,105 +43,6 @@ export type AttachmentFileRouteParams = {
5043

5144
type AttachmentFileScreenProps = StaticScreenProps<AttachmentFileRouteParams>;
5245

53-
export function nativeViewerKind(kind: ReturnType<typeof useAttachmentDocument>["kind"]) {
54-
if (kind === "image") return "image" as const;
55-
if (kind === "pdf") return "pdf" as const;
56-
if (kind === "video" || kind === "unsupported") return "document" as const;
57-
return null;
58-
}
59-
60-
export function AttachmentDocumentBody(props: {
61-
readonly document: Pick<
62-
ReturnType<typeof useAttachmentDocument>,
63-
| "kind"
64-
| "activeMode"
65-
| "uri"
66-
| "content"
67-
| "table"
68-
| "error"
69-
| "needsText"
70-
| "retry"
71-
| "revision"
72-
>;
73-
readonly name: string;
74-
readonly environmentId: EnvironmentId | null;
75-
readonly htmlRender: boolean;
76-
readonly nativeViewer: "pending" | "open" | "unavailable" | null;
77-
readonly nativeError: string | null;
78-
readonly onOpenNative: () => void;
79-
}) {
80-
const { document } = props;
81-
if (document.error) {
82-
return (
83-
<View className="flex-1 items-center justify-center bg-sheet px-6">
84-
<EmptyState
85-
title="File unavailable"
86-
detail={document.error}
87-
actionLabel="Try again"
88-
onAction={document.retry}
89-
/>
90-
</View>
91-
);
92-
}
93-
if (props.nativeViewer !== null && props.nativeViewer !== "unavailable") {
94-
return <FilePreviewLoading message="Opening in file viewer..." />;
95-
}
96-
// ZIP text entries are read into memory and have no file URI.
97-
if (document.needsText ? !document.content : !document.uri) {
98-
return <FilePreviewLoading message="Loading file..." />;
99-
}
100-
if (document.needsText && document.content) {
101-
const { content, table } = document;
102-
return (
103-
<View className="flex-1 bg-sheet">
104-
{content.truncated ? (
105-
<FilePreviewNotice title="Partial file">
106-
Preview limited to the first 1 MB. Save or share the file to read it in full.
107-
</FilePreviewNotice>
108-
) : null}
109-
{table && document.activeMode === "table" ? (
110-
<DelimitedFilePreview table={table} />
111-
) : document.activeMode === "markdown" && props.environmentId ? (
112-
<FileMarkdownPreview
113-
cwd=""
114-
relativePath=""
115-
environmentId={props.environmentId}
116-
threadId={null}
117-
markdown={content.text}
118-
captured
119-
/>
120-
) : (
121-
<SourceFileSurface contents={content.text} path={props.name} selectable />
122-
)}
123-
</View>
124-
);
125-
}
126-
if (!document.uri) return null;
127-
if (document.kind === "audio") {
128-
return <AudioFilePreview key={document.revision} uri={document.uri} onRetry={document.retry} />;
129-
}
130-
if (document.kind === "html") {
131-
return props.htmlRender ? (
132-
<HtmlRenderWebView key={document.uri} uri={document.uri} title={props.name} nested={false} />
133-
) : (
134-
<WorkspaceFileWebPreview uri={document.uri} />
135-
);
136-
}
137-
return (
138-
<View className="flex-1 items-center justify-center bg-sheet px-6">
139-
<EmptyState
140-
title="No preview for this file"
141-
detail={
142-
props.nativeError ??
143-
"No app on this device can show this format. Save or share it to open it elsewhere."
144-
}
145-
actionLabel="Try again"
146-
onAction={props.onOpenNative}
147-
/>
148-
</View>
149-
);
150-
}
151-
15246
export function AttachmentFileScreen(props: AttachmentFileScreenProps) {
15347
const navigation = useNavigation();
15448
const { appearance, setCodeWordBreak } = useAppearancePreferences();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import {
4343
removePersistedComposerAttachmentFile,
4444
} from "../../lib/composerImages";
4545
import { appendComposerDraftAttachments } from "../../state/use-composer-drafts";
46-
import { AttachmentDocumentBody, nativeViewerKind } from "./AttachmentFileScreen";
46+
import { AttachmentDocumentBody, nativeViewerKind } from "./AttachmentDocumentBody";
4747
import { FilePreviewLoading } from "./FilePreviewFeedback";
4848
import {
4949
buildZipTree,

0 commit comments

Comments
 (0)