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

FileMarkdownPreview.tsx

213 lines7.7 KB

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;
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 skillTextColor: codeText,
61 quoteMarkerColor: blockquoteBorder,
62 dividerColor: horizontalRule,
63 fontSize: nativeMarkdownTypography.fontSize,
64 lineHeight: nativeMarkdownTypography.lineHeight,
65 headingFontSizes: nativeMarkdownTypography.headingFontSizes,
66 fontFamily: regularFontFamily,
67 headingFontFamily: boldFontFamily,
68 boldFontFamily,
69 },
70 };
72​
73export function FileMarkdownPreview(props: {
74 readonly cwd: string;
75 readonly captured?: boolean;
76 readonly environmentId: EnvironmentId;
77 readonly markdown: string;
78 readonly relativePath: string;
79 readonly threadId: ThreadId | null;
80 readonly onRefresh?: () => Promise<void> | void;
81}) {
82 const [isPullRefreshing, setIsPullRefreshing] = useState(false);
83 const [expandedFile, setExpandedFile] = useState<FilePreviewSource | null>(null);
84 const [expandedVideo, setExpandedVideo] = useState<VideoPreviewSource | null>(null);
85 const handlePullToRefresh = async () => {
86 if (!props.onRefresh) {
87 return;
88 }
89 setIsPullRefreshing(true);
90 // RefreshControl only needs the spinner cleared; the caller surfaces its own errors.
91 await settlePromise(async () => props.onRefresh?.());
92 setIsPullRefreshing(false);
93 };
94 const markdownDirectory = getBrowseDirectoryPath(
95 resolveWorkspaceFilePath(props.cwd, props.relativePath),
96 );
97 const renderImage: MarkdownImageRenderer = (image) => {
98 const media = resolveMobileMarkdownMediaSource(image.href, {
99 threadId: props.threadId ?? undefined,
100 workspaceRoot: markdownDirectory,
101 imageEmbed: true,
102 });
103 const preview = resolveMarkdownMediaPreview(image.href, {
104 environmentId: props.environmentId,
105 threadId: props.threadId ?? undefined,
106 workspaceRoot: markdownDirectory,
107 captured: props.captured,
108 imageEmbed: true,
109 });
110 if (media?.access === "direct") {
111 if (media.kind === "video") {
112 return (
113 <ThreadMarkdownVideo
114 source={{
115 type: "media",
116 name: media.name,
117 mimeType: media.mimeType,
118 uri: normalizeNativeMarkdownUrl(media.uri),
119 actionsSource: preview?.source.actionsSource,
120 }}
121 thumbnailVisible
122 />
123 );
124 }
125 return (
126 <ThreadMarkdownImageView
127 uri={normalizeNativeMarkdownUrl(media.uri)}
128 sourceKey={media.uri}
129 unavailable={false}
130 alt={image.alt}
131 format={media.mimeType === "image/svg+xml" ? "svg" : undefined}
132 actionsSource={preview?.source.actionsSource}
133 onPressPreview={setExpandedFile}
134 />
135 );
136 }
137 if (props.captured || media === null || media.access === "unavailable") {
138 return <ThreadMarkdownImageUnavailable alt={image.alt} />;
139 }
140 if (media.kind === "video") {
141 return preview?.kind === "video" ? (
142 <ThreadMarkdownVideo source={preview.source} thumbnailVisible />
143 ) : (
144 <ThreadMarkdownImageUnavailable alt={image.alt} />
145 );
146 }
147 return (
148 <ThreadMarkdownImage
149 environmentId={props.environmentId}
150 resource={media.resource}
151 alt={image.alt}
152 srcFragment={media.srcFragment}
153 actionsSource={preview?.source.actionsSource}
154 onPressPreview={setExpandedFile}
155 />
156 );
157 };
158 const styles = useMarkdownPreviewStyles();
159 const resolveImageSource = useMarkdownImageSource({
160 environmentId: props.environmentId,
161 threadId: props.threadId,
162 workspaceRoot: markdownDirectory,
163 captured: props.captured,
164 });
165 const openMedia = (href: string, imageEmbed = false) => {
166 const media = resolveMarkdownMediaPreview(href, {
167 environmentId: props.environmentId,
168 threadId: props.threadId ?? undefined,
169 workspaceRoot: markdownDirectory,
170 captured: props.captured,
171 imageEmbed,
172 });
173 if (!media) return false;
174 if (media.kind === "video") setExpandedVideo(media.source);
175 else setExpandedFile(media.source);
176 return true;
177 };
178 const onImagePress = (href: string) => {
179 openMedia(href, true);
180 };
181 const onLinkPress = (href: string) => {
182 if (!openMedia(href)) void tryOpenExternalUrl(href, "markdown-link");
183 };
184​
185 return (
186 <ScrollView
187 className="flex-1 bg-sheet"
188 contentInsetAdjustmentBehavior="automatic"
189 contentContainerStyle={{ padding: 18 }}
190 refreshControl={
191 props.onRefresh ? (
192 <RefreshControl
193 refreshing={isPullRefreshing}
194 onRefresh={() => void handlePullToRefresh()}
195 />
196 ) : undefined
197 }
198 >
199 <View className="mx-auto w-full max-w-[760px]">
200 <SelectableMarkdownText
201 markdown={props.markdown}
202 onLinkPress={onLinkPress}
203 renderImage={renderImage}
204 resolveImageSource={resolveImageSource}
205 onImagePress={onImagePress}
206 textStyle={styles.nativeTextStyle}
207 />
208 <FilePreviewModal source={expandedFile} onRequestClose={() => setExpandedFile(null)} />
209 <VideoPreviewModal source={expandedVideo} onRequestClose={() => setExpandedVideo(null)} />
210 </View>
211 </ScrollView>
212 );