Open History to see the last commit
1import { settlePromise } from "@t3tools/client-runtime/state/runtime";
2import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
3import { resolveMobileMarkdownMediaSource } from "../../lib/markdownMediaSource";
4import { getBrowseDirectoryPath } from "@t3tools/client-runtime/state/projects";
5import { useState } from "react";
6import { RefreshControl, ScrollView, View } from "react-native";
7
8import { tryOpenExternalUrl } from "../../lib/openExternalUrl";
9import { FONT_FAMILIES } from "../../lib/fontFamilies";
10import { resolveNativeMarkdownTypography } from "../../lib/appearancePreferences";
11import { useUniwindTheme } from "../../lib/useUniwindTheme";
12import {
13 ThreadMarkdownImage,
14 ThreadMarkdownImageView,
15 ThreadMarkdownImageUnavailable,
16} from "../threads/ThreadMarkdownImage";
17import { ThreadMarkdownVideo } from "../threads/ThreadMarkdownVideo";
18import { resolveMarkdownMediaPreview } from "../../lib/markdownMedia";
19import { normalizeNativeMarkdownUrl } from "../../lib/markdownLinks";
20import { VideoPreviewModal, type VideoPreviewSource } from "../../components/VideoPreviewModal";
21import { FilePreviewModal, type FilePreviewSource } from "../../components/FilePreviewModal";
22import { useMarkdownImageSource } from "../../native/useMarkdownImageSource";
23import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
24import {
25 SelectableMarkdownText,
26 type MarkdownImageRenderer,
27 type NativeMarkdownTextStyle,
28} from "../../native/SelectableMarkdownText";
29import { resolveWorkspaceFilePath } from "./filePath";
30
31interface MarkdownPreviewStyles {
32 readonly nativeTextStyle: NativeMarkdownTextStyle;
33}
34
35export function useMarkdownPreviewStyles(): MarkdownPreviewStyles {
36 const { appearance } = useAppearancePreferences();
37 const nativeMarkdownTypography = resolveNativeMarkdownTypography(appearance.baseFontSize);
38 const theme = useUniwindTheme();
39 const body = theme["--color-md-body"];
40 const strong = theme["--color-md-strong"];
41 const link = theme["--color-md-link"];
42 const blockquoteBorder = theme["--color-md-blockquote-border"];
43 const codeBackground = theme["--color-md-code-bg"];
44 const codeText = theme["--color-md-code-text"];
45 const horizontalRule = theme["--color-md-hr"];
46 const regularFontFamily = FONT_FAMILIES.regular;
47 const boldFontFamily = FONT_FAMILIES.bold;
48
49 return {
50 nativeTextStyle: {
51 color: body,
52 strongColor: strong,
53 mutedColor: body,
54 linkColor: link,
55 inlineCodeColor: codeText,
56 codeColor: codeText,
57 codeBackgroundColor: codeBackground,
58 codeBlockBackgroundColor: codeBackground,
59 fileTextColor: codeText,
60 quoteMarkerColor: blockquoteBorder,
61 dividerColor: horizontalRule,
62 fontSize: nativeMarkdownTypography.fontSize,
63 lineHeight: nativeMarkdownTypography.lineHeight,
64 headingFontSizes: nativeMarkdownTypography.headingFontSizes,
65 fontFamily: regularFontFamily,
66 headingFontFamily: boldFontFamily,
67 boldFontFamily,
68 },
69 };
70}
71
72export function FileMarkdownPreview(props: {
73 readonly cwd: string;
74 readonly captured?: boolean;
75 readonly environmentId: EnvironmentId;
76 readonly markdown: string;
77 readonly relativePath: string;
78 readonly threadId: ThreadId | null;
79 readonly onRefresh?: () => Promise<void> | void;
80}) {
81 const [isPullRefreshing, setIsPullRefreshing] = useState(false);
82 const [expandedFile, setExpandedFile] = useState<FilePreviewSource | null>(null);
83 const [expandedVideo, setExpandedVideo] = useState<VideoPreviewSource | null>(null);
84 const handlePullToRefresh = async () => {
85 if (!props.onRefresh) {
86 return;
87 }
88 setIsPullRefreshing(true);
89 // RefreshControl only needs the spinner cleared; the caller surfaces its own errors.
90 await settlePromise(async () => props.onRefresh?.());
91 setIsPullRefreshing(false);
92 };
93 const markdownDirectory = getBrowseDirectoryPath(
94 resolveWorkspaceFilePath(props.cwd, props.relativePath),
95 );
96 const renderImage: MarkdownImageRenderer = (image) => {
97 const media = resolveMobileMarkdownMediaSource(image.href, {
98 threadId: props.threadId ?? undefined,
99 workspaceRoot: markdownDirectory,
100 imageEmbed: true,
101 });
102 const preview = resolveMarkdownMediaPreview(image.href, {
103 environmentId: props.environmentId,
104 threadId: props.threadId ?? undefined,
105 workspaceRoot: markdownDirectory,
106 captured: props.captured,
107 imageEmbed: true,
108 });
109 if (media?.access === "direct") {
110 if (media.kind === "video") {
111 return (
112 <ThreadMarkdownVideo
113 source={{
114 type: "media",
115 name: media.name,
116 mimeType: media.mimeType,
117 uri: normalizeNativeMarkdownUrl(media.uri),
118 actionsSource: preview?.source.actionsSource,
119 }}
120 thumbnailVisible
121 />
122 );
123 }
124 return (
125 <ThreadMarkdownImageView
126 uri={normalizeNativeMarkdownUrl(media.uri)}
127 sourceKey={media.uri}
128 unavailable={false}
129 alt={image.alt}
130 format={media.mimeType === "image/svg+xml" ? "svg" : undefined}
131 actionsSource={preview?.source.actionsSource}
132 onPressPreview={setExpandedFile}
133 />
134 );
135 }
136 if (props.captured || media === null || media.access === "unavailable") {
137 return <ThreadMarkdownImageUnavailable alt={image.alt} />;
138 }
139 if (media.kind === "video") {
140 return preview?.kind === "video" ? (
141 <ThreadMarkdownVideo source={preview.source} thumbnailVisible />
142 ) : (
143 <ThreadMarkdownImageUnavailable alt={image.alt} />
144 );
145 }
146 return (
147 <ThreadMarkdownImage
148 environmentId={props.environmentId}
149 resource={media.resource}
150 alt={image.alt}
151 srcFragment={media.srcFragment}
152 actionsSource={preview?.source.actionsSource}
153 onPressPreview={setExpandedFile}
154 />
155 );
156 };
157 const styles = useMarkdownPreviewStyles();
158 const resolveImageSource = useMarkdownImageSource({
159 environmentId: props.environmentId,
160 threadId: props.threadId,
161 workspaceRoot: markdownDirectory,
162 captured: props.captured,
163 });
164 const openMedia = (href: string, imageEmbed = false) => {
165 const media = resolveMarkdownMediaPreview(href, {
166 environmentId: props.environmentId,
167 threadId: props.threadId ?? undefined,
168 workspaceRoot: markdownDirectory,
169 captured: props.captured,
170 imageEmbed,
171 });
172 if (!media) return false;
173 if (media.kind === "video") setExpandedVideo(media.source);
174 else setExpandedFile(media.source);
175 return true;
176 };
177 const onImagePress = (href: string) => {
178 openMedia(href, true);
179 };
180 const onLinkPress = (href: string) => {
181 if (!openMedia(href)) void tryOpenExternalUrl(href, "markdown-link");
182 };
183
184 return (
185 <ScrollView
186 className="flex-1 bg-sheet"
187 contentInsetAdjustmentBehavior="automatic"
188 contentContainerStyle={{ padding: 18 }}
189 refreshControl={
190 props.onRefresh ? (
191 <RefreshControl
192 refreshing={isPullRefreshing}
193 onRefresh={() => void handlePullToRefresh()}
194 />
195 ) : undefined
196 }
197 >
198 <View className="mx-auto w-full max-w-[760px]">
199 <SelectableMarkdownText
200 markdown={props.markdown}
201 onLinkPress={onLinkPress}
202 renderImage={renderImage}
203 resolveImageSource={resolveImageSource}
204 onImagePress={onImagePress}
205 textStyle={styles.nativeTextStyle}
206 />
207 <FilePreviewModal source={expandedFile} onRequestClose={() => setExpandedFile(null)} />
208 <VideoPreviewModal source={expandedVideo} onRequestClose={() => setExpandedVideo(null)} />
209 </View>
210 </ScrollView>
211 );
212}