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