Open History to see the last commit
1import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { resolveMediaSource } from "@t3tools/client-runtime/media-source";
3import { getBrowseDirectoryPath } from "@t3tools/client-runtime/state/projects";
4import { useCallback, useMemo, useState } from "react";
5import {
6 Markdown,
7 type CustomRenderers,
8 type NodeStyleOverrides,
9 type PartialMarkdownTheme,
10} from "react-native-nitro-markdown";
11import { RefreshControl, ScrollView, Text as NativeText, View } from "react-native";
12
13import { tryOpenExternalUrl } from "../../lib/openExternalUrl";
14import { useFontFamily } from "../../lib/useFontFamily";
15import {
16 resolveMarkdownFontSizes,
17 resolveNativeMarkdownTypography,
18} from "../../lib/appearancePreferences";
19import { useUniwindTheme } from "../../lib/useUniwindTheme";
20import {
21 ThreadMarkdownImage,
22 ThreadMarkdownImageUnavailable,
23} from "../threads/ThreadMarkdownImage";
24import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
25import {
26 hasNativeSelectableMarkdownText,
27 SelectableMarkdownText,
28 type MarkdownImageRenderer,
29 type NativeMarkdownTextStyle,
30} from "../../native/SelectableMarkdownText";
31import { resolveWorkspaceFilePath } from "./filePath";
32
33interface MarkdownPreviewStyles {
34 readonly theme: PartialMarkdownTheme;
35 readonly styles: NodeStyleOverrides;
36 readonly renderers: CustomRenderers;
37 readonly nativeTextStyle: NativeMarkdownTextStyle;
38}
39
40function useMarkdownPreviewStyles(renderImage?: MarkdownImageRenderer): MarkdownPreviewStyles {
41 const { appearance } = useAppearancePreferences();
42 const markdownFontSizes = useMemo(
43 () => resolveMarkdownFontSizes(appearance.baseFontSize),
44 [appearance.baseFontSize],
45 );
46 const nativeMarkdownTypography = useMemo(
47 () => resolveNativeMarkdownTypography(appearance.baseFontSize),
48 [appearance.baseFontSize],
49 );
50 const theme = useUniwindTheme();
51 const body = theme["--color-md-body"];
52 const strong = theme["--color-md-strong"];
53 const link = theme["--color-md-link"];
54 const blockquoteBorder = theme["--color-md-blockquote-border"];
55 const blockquoteBackground = theme["--color-md-blockquote-bg"];
56 const codeBackground = theme["--color-md-code-bg"];
57 const codeText = theme["--color-md-code-text"];
58 const horizontalRule = theme["--color-md-hr"];
59 const regularFontFamily = useFontFamily("regular");
60 const mediumFontFamily = useFontFamily("medium");
61 const boldFontFamily = useFontFamily("bold");
62
63 return useMemo(() => {
64 const renderers: CustomRenderers = {
65 link: ({ href, children }) => (
66 <NativeText
67 className="font-t3-medium"
68 onPress={() => {
69 if (href) {
70 void tryOpenExternalUrl(href, "markdown-link");
71 }
72 }}
73 style={{
74 color: link,
75 textDecorationLine: "none",
76 }}
77 >
78 {children}
79 </NativeText>
80 ),
81 image: ({ node }) =>
82 node.href && renderImage
83 ? (renderImage({
84 href: node.href,
85 alt: node.alt ?? null,
86 title: node.title ?? null,
87 }) ?? undefined)
88 : undefined,
89 };
90
91 return {
92 theme: {
93 colors: {
94 text: body,
95 heading: strong,
96 link,
97 blockquote: blockquoteBorder,
98 border: horizontalRule,
99 surface: "transparent",
100 surfaceLight: blockquoteBackground,
101 accent: link,
102 tableBorder: horizontalRule,
103 tableHeader: blockquoteBackground,
104 tableHeaderText: strong,
105 tableRowOdd: blockquoteBackground,
106 tableRowEven: "transparent",
107 code: codeText,
108 codeBackground,
109 },
110 },
111 styles: {
112 text: {
113 color: body,
114 fontFamily: regularFontFamily,
115 fontSize: markdownFontSizes.m,
116 lineHeight: markdownFontSizes.bodyLineHeight,
117 },
118 heading: {
119 color: strong,
120 fontFamily: boldFontFamily,
121 },
122 strong: {
123 color: strong,
124 fontFamily: boldFontFamily,
125 },
126 link: {
127 color: link,
128 fontFamily: mediumFontFamily,
129 },
130 blockquote: {
131 backgroundColor: blockquoteBackground,
132 borderLeftColor: blockquoteBorder,
133 borderLeftWidth: 3,
134 paddingLeft: 12,
135 },
136 code: {
137 backgroundColor: codeBackground,
138 color: codeText,
139 fontFamily: "ui-monospace",
140 },
141 codeBlock: {
142 backgroundColor: codeBackground,
143 borderRadius: 12,
144 color: codeText,
145 fontFamily: "ui-monospace",
146 padding: 12,
147 },
148 hr: {
149 backgroundColor: horizontalRule,
150 },
151 },
152 renderers,
153 nativeTextStyle: {
154 color: body,
155 strongColor: strong,
156 mutedColor: body,
157 linkColor: link,
158 inlineCodeColor: codeText,
159 codeColor: codeText,
160 codeBackgroundColor: codeBackground,
161 codeBlockBackgroundColor: codeBackground,
162 fileTextColor: codeText,
163 skillTextColor: codeText,
164 quoteMarkerColor: blockquoteBorder,
165 dividerColor: horizontalRule,
166 fontSize: nativeMarkdownTypography.fontSize,
167 lineHeight: nativeMarkdownTypography.lineHeight,
168 headingFontSizes: nativeMarkdownTypography.headingFontSizes,
169 fontFamily: regularFontFamily,
170 headingFontFamily: boldFontFamily,
171 boldFontFamily,
172 },
173 };
174 }, [
175 blockquoteBackground,
176 blockquoteBorder,
177 body,
178 codeBackground,
179 codeText,
180 horizontalRule,
181 link,
182 markdownFontSizes,
183 mediumFontFamily,
184 nativeMarkdownTypography,
185 regularFontFamily,
186 renderImage,
187 strong,
188 boldFontFamily,
189 ]);
190}
191
192export function FileMarkdownPreview(props: {
193 readonly cwd: string;
194 readonly captured?: boolean;
195 readonly environmentId: EnvironmentId;
196 readonly markdown: string;
197 readonly relativePath: string;
198 /** Absent for a file opened from a project draft, which has no thread yet. */
199 readonly threadId: ThreadId | null;
200 readonly onRefresh?: () => Promise<void> | void;
201}) {
202 const [isPullRefreshing, setIsPullRefreshing] = useState(false);
203 const handlePullToRefresh = useCallback(async () => {
204 if (!props.onRefresh) {
205 return;
206 }
207 setIsPullRefreshing(true);
208 try {
209 await props.onRefresh();
210 } finally {
211 setIsPullRefreshing(false);
212 }
213 }, [props.onRefresh]);
214 const markdownDirectory = useMemo(
215 () => getBrowseDirectoryPath(resolveWorkspaceFilePath(props.cwd, props.relativePath)),
216 [props.cwd, props.relativePath],
217 );
218 const renderImage = useCallback<MarkdownImageRenderer>(
219 (image) => {
220 const media = resolveMediaSource(image.href, {
221 threadId: props.threadId ?? undefined,
222 workspaceRoot: markdownDirectory,
223 imageEmbed: true,
224 });
225 if (media?.access === "direct") {
226 return null;
227 }
228 if (
229 props.captured ||
230 media === null ||
231 media.kind !== "image" ||
232 media.access === "unavailable"
233 ) {
234 return <ThreadMarkdownImageUnavailable alt={image.alt} />;
235 }
236 return (
237 <ThreadMarkdownImage
238 environmentId={props.environmentId}
239 resource={media.resource}
240 alt={image.alt}
241 srcFragment={media.srcFragment}
242 onPressPreview={() => undefined}
243 />
244 );
245 },
246 [markdownDirectory, props.environmentId, props.threadId, props.captured],
247 );
248 const styles = useMarkdownPreviewStyles(renderImage);
249 const onLinkPress = useCallback((href: string) => {
250 void tryOpenExternalUrl(href, "markdown-link");
251 }, []);
252
253 return (
254 <ScrollView
255 className="flex-1 bg-sheet"
256 contentContainerStyle={{ padding: 18 }}
257 refreshControl={
258 props.onRefresh ? (
259 <RefreshControl
260 refreshing={isPullRefreshing}
261 onRefresh={() => void handlePullToRefresh()}
262 />
263 ) : undefined
264 }
265 >
266 <View className="mx-auto w-full max-w-[760px]">
267 {hasNativeSelectableMarkdownText() ? (
268 <SelectableMarkdownText
269 markdown={props.markdown}
270 onLinkPress={onLinkPress}
271 renderImage={renderImage}
272 textStyle={styles.nativeTextStyle}
273 />
274 ) : (
275 <Markdown
276 options={{ gfm: true }}
277 renderers={styles.renderers}
278 styles={styles.styles}
279 theme={styles.theme}
280 >
281 {props.markdown}
282 </Markdown>
283 )}
284 </View>
285 </ScrollView>
286 );
287}