Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

FileMarkdownPreview.tsx

280 lines8.3 KB

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;
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 ]);
191​
192export function FileMarkdownPreview(props: {
193 readonly cwd: string;
194 readonly environmentId: EnvironmentId;
195 readonly markdown: string;
196 readonly relativePath: string;
197 readonly threadId: ThreadId;
198 readonly onRefresh?: () => Promise<void> | void;
199}) {
200 const [isPullRefreshing, setIsPullRefreshing] = useState(false);
201 const handlePullToRefresh = useCallback(async () => {
202 if (!props.onRefresh) {
203 return;
204 }
205 setIsPullRefreshing(true);
206 try {
207 await props.onRefresh();
208 } finally {
209 setIsPullRefreshing(false);
210 }
211 }, [props.onRefresh]);
212 const markdownDirectory = useMemo(
213 () => getBrowseDirectoryPath(resolveWorkspaceFilePath(props.cwd, props.relativePath)),
214 [props.cwd, props.relativePath],
215 );
216 const renderImage = useCallback<MarkdownImageRenderer>(
217 (image) => {
218 const media = resolveMediaSource(image.href, {
219 threadId: props.threadId,
220 workspaceRoot: markdownDirectory,
221 imageEmbed: true,
222 });
223 if (media?.access === "direct") {
224 return null;
225 }
226 if (media === null || media.kind !== "image" || media.access === "unavailable") {
227 return <ThreadMarkdownImageUnavailable alt={image.alt} />;
228 }
229 return (
230 <ThreadMarkdownImage
231 environmentId={props.environmentId}
232 resource={media.resource}
233 alt={image.alt}
234 srcFragment={media.srcFragment}
235 onPressPreview={() => undefined}
236 />
237 );
238 },
239 [markdownDirectory, props.environmentId, props.threadId],
240 );
241 const styles = useMarkdownPreviewStyles(renderImage);
242 const onLinkPress = useCallback((href: string) => {
243 void tryOpenExternalUrl(href, "markdown-link");
244 }, []);
245​
246 return (
247 <ScrollView
248 className="flex-1 bg-sheet"
249 contentContainerStyle={{ padding: 18 }}
250 refreshControl={
251 props.onRefresh ? (
252 <RefreshControl
253 refreshing={isPullRefreshing}
254 onRefresh={() => void handlePullToRefresh()}
255 />
256 ) : undefined
257 }
258 >
259 <View className="mx-auto w-full max-w-[760px]">
260 {hasNativeSelectableMarkdownText() ? (
261 <SelectableMarkdownText
262 markdown={props.markdown}
263 onLinkPress={onLinkPress}
264 renderImage={renderImage}
265 textStyle={styles.nativeTextStyle}
266 />
267 ) : (
268 <Markdown
269 options={{ gfm: true }}
270 renderers={styles.renderers}
271 styles={styles.styles}
272 theme={styles.theme}
273 >
274 {props.markdown}
275 </Markdown>
276 )}
277 </View>
278 </ScrollView>
279 );