Skip to content

Commit 09b81a3

Browse files
authored
fix(mobile): render workspace images in markdown file previews (pingdotgg#8769)
1 parent 2675e3c commit 09b81a3

4 files changed

Lines changed: 265 additions & 186 deletions

File tree

‎apps/mobile/src/features/files/FileMarkdownPreview.tsx‎

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
2+
import { resolveMediaSource } from "@t3tools/client-runtime/media-source";
3+
import { getBrowseDirectoryPath } from "@t3tools/client-runtime/state/projects";
14
import { useCallback, useMemo, useState } from "react";
25
import {
36
Markdown,
@@ -14,12 +17,18 @@ import {
1417
resolveNativeMarkdownTypography,
1518
} from "../../lib/appearancePreferences";
1619
import { useUniwindTheme } from "../../lib/useUniwindTheme";
20+
import {
21+
ThreadMarkdownImage,
22+
ThreadMarkdownImageUnavailable,
23+
} from "../threads/ThreadMarkdownImage";
1724
import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
1825
import {
1926
hasNativeSelectableMarkdownText,
2027
SelectableMarkdownText,
28+
type MarkdownImageRenderer,
2129
type NativeMarkdownTextStyle,
2230
} from "../../native/SelectableMarkdownText";
31+
import { resolveWorkspaceFilePath } from "./filePath";
2332

2433
interface MarkdownPreviewStyles {
2534
readonly theme: PartialMarkdownTheme;
@@ -28,7 +37,7 @@ interface MarkdownPreviewStyles {
2837
readonly nativeTextStyle: NativeMarkdownTextStyle;
2938
}
3039

31-
function useMarkdownPreviewStyles(): MarkdownPreviewStyles {
40+
function useMarkdownPreviewStyles(renderImage?: MarkdownImageRenderer): MarkdownPreviewStyles {
3241
const { appearance } = useAppearancePreferences();
3342
const markdownFontSizes = useMemo(
3443
() => resolveMarkdownFontSizes(appearance.baseFontSize),
@@ -69,6 +78,14 @@ function useMarkdownPreviewStyles(): MarkdownPreviewStyles {
6978
{children}
7079
</NativeText>
7180
),
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,
7289
};
7390

7491
return {
@@ -166,13 +183,18 @@ function useMarkdownPreviewStyles(): MarkdownPreviewStyles {
166183
mediumFontFamily,
167184
nativeMarkdownTypography,
168185
regularFontFamily,
186+
renderImage,
169187
strong,
170188
boldFontFamily,
171189
]);
172190
}
173191

174192
export function FileMarkdownPreview(props: {
193+
readonly cwd: string;
194+
readonly environmentId: EnvironmentId;
175195
readonly markdown: string;
196+
readonly relativePath: string;
197+
readonly threadId: ThreadId;
176198
readonly onRefresh?: () => Promise<void> | void;
177199
}) {
178200
const [isPullRefreshing, setIsPullRefreshing] = useState(false);
@@ -187,7 +209,36 @@ export function FileMarkdownPreview(props: {
187209
setIsPullRefreshing(false);
188210
}
189211
}, [props.onRefresh]);
190-
const styles = useMarkdownPreviewStyles();
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);
191242
const onLinkPress = useCallback((href: string) => {
192243
void tryOpenExternalUrl(href, "markdown-link");
193244
}, []);
@@ -210,6 +261,7 @@ export function FileMarkdownPreview(props: {
210261
<SelectableMarkdownText
211262
markdown={props.markdown}
212263
onLinkPress={onLinkPress}
264+
renderImage={renderImage}
213265
textStyle={styles.nativeTextStyle}
214266
/>
215267
) : (

‎apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ function defaultViewMode(path: string | null): FileViewMode {
106106

107107
function FileContent(props: {
108108
readonly activeMode: FileViewMode;
109-
readonly environmentId: EnvironmentId | null;
109+
readonly cwd: string;
110+
readonly environmentId: EnvironmentId;
110111
readonly previewUri: string | null;
111112
readonly previewFailure: AssetUrlFailureReason | null;
112113
readonly onRetryPreview: () => void;
@@ -116,6 +117,7 @@ function FileContent(props: {
116117
readonly fileContents: string | null;
117118
readonly fileError: string | null;
118119
readonly relativePath: string;
120+
readonly threadId: ThreadId;
119121
readonly initialLine: number | null;
120122
readonly truncated: boolean;
121123
readonly onRefresh?: () => Promise<void> | void;
@@ -199,7 +201,14 @@ function FileContent(props: {
199201
</View>
200202
) : null}
201203
{props.activeMode === "preview" && isMarkdown ? (
202-
<FileMarkdownPreview markdown={props.fileContents} onRefresh={props.onRefresh} />
204+
<FileMarkdownPreview
205+
cwd={props.cwd}
206+
environmentId={props.environmentId}
207+
markdown={props.fileContents}
208+
relativePath={props.relativePath}
209+
threadId={props.threadId}
210+
onRefresh={props.onRefresh}
211+
/>
203212
) : (
204213
<SourceFileSurface
205214
contents={props.fileContents}
@@ -910,6 +919,7 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
910919
<FileContent
911920
key={previewKey}
912921
activeMode={resolvedActiveMode}
922+
cwd={cwd}
913923
environmentId={environmentId}
914924
previewUri={previewUri}
915925
previewFailure={assetPreview._tag === "Failure" ? assetPreview.reason : null}
@@ -921,6 +931,7 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
921931
fileError={fileQuery.error}
922932
initialLine={targetLine}
923933
relativePath={relativePath}
934+
threadId={threadId}
924935
truncated={fileData?.truncated ?? false}
925936
onRefresh={() => fileQuery.refresh()}
926937
/>

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 5 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as Haptics from "expo-haptics";
22
import { KeyboardAwareLegendList } from "@legendapp/list/keyboard";
33
import { useViewabilityAmount, type LegendListRef } from "@legendapp/list/react-native";
44
import type {
5-
AssetResource,
65
ChatAttachment,
76
ChatFileAttachment,
87
ChatImageAttachment,
@@ -68,7 +67,6 @@ import {
6867
type ColorValue,
6968
useWindowDimensions,
7069
View,
71-
type ViewStyle,
7270
} from "react-native";
7371
import { FilePreviewModal, type FilePreviewSource } from "../../components/FilePreviewModal";
7472
import { isPdfFile } from "../../lib/filePreview";
@@ -102,8 +100,6 @@ import { VideoPreviewModal, type VideoPreviewSource } from "../../components/Vid
102100
import { VideoAttachmentTile } from "../../components/VideoAttachmentTile";
103101
import { MediaVideoPlayer } from "../../components/MediaVideoPlayer";
104102
import { resolveMarkdownMediaPreview } from "../../lib/markdownMedia";
105-
import { useMediaActions, type MediaActionsSource } from "../../lib/mediaActions";
106-
import { MediaActionsMenu } from "../../components/MediaActionsMenu";
107103
import {
108104
attachmentVideoPreviewSource,
109105
mediaVideoPreviewUri,
@@ -178,8 +174,12 @@ import {
178174
isAbsolutePath,
179175
resolveWorkspaceRelativeFilePath,
180176
} from "../files/filePath";
181-
import { MARKDOWN_IMAGE_MAX_WIDTH, resolveMarkdownImageDisplaySize } from "./markdownImageSize";
182177
import { fileChipMenu, resolveFileChipTarget, type FileChipAction } from "./fileChipMenu";
178+
import {
179+
ThreadMarkdownImage,
180+
ThreadMarkdownImageUnavailable,
181+
ThreadMarkdownImageView,
182+
} from "./ThreadMarkdownImage";
183183

184184
const WIDE_MARKDOWN_BLOCK_OPTIONS = {
185185
// Native iOS blockquotes and adjacent selectable text are separate layout
@@ -505,171 +505,6 @@ function MessageAttachmentUnknown(props: { readonly name: string }) {
505505
);
506506
}
507507

508-
function ThreadMarkdownImageView(props: {
509-
readonly uri: string | null;
510-
readonly sourceKey: string;
511-
readonly unavailable: boolean;
512-
readonly alt: string | null;
513-
readonly actionsSource?: MediaActionsSource;
514-
readonly onPressPreview: (source: FilePreviewSource) => void;
515-
}) {
516-
const sourceIdentifier = useId();
517-
const mediaActions = useMediaActions(props.actionsSource);
518-
const [availableWidth, setAvailableWidth] = useState(0);
519-
const [sourceSize, setSourceSize] = useState<{ width: number; height: number } | null>(null);
520-
const [failedUri, setFailedUri] = useState<string | null>(null);
521-
522-
useEffect(() => {
523-
setSourceSize(null);
524-
}, [props.sourceKey]);
525-
526-
useEffect(() => {
527-
setFailedUri(null);
528-
}, [props.uri]);
529-
530-
const displaySize =
531-
sourceSize === null
532-
? null
533-
: resolveMarkdownImageDisplaySize({
534-
sourceWidth: sourceSize.width,
535-
sourceHeight: sourceSize.height,
536-
availableWidth,
537-
});
538-
const failed = props.unavailable || (props.uri !== null && failedUri === props.uri);
539-
const placeholderWidth: ViewStyle["width"] =
540-
availableWidth > 0 ? Math.min(availableWidth, MARKDOWN_IMAGE_MAX_WIDTH) : "100%";
541-
const frameStyle: ViewStyle = displaySize ?? { width: placeholderWidth, aspectRatio: 16 / 9 };
542-
543-
return (
544-
<View
545-
onLayout={(event) => setAvailableWidth(event.nativeEvent.layout.width)}
546-
style={{ alignSelf: "stretch", gap: 6 }}
547-
>
548-
{props.uri === null || failed ? (
549-
<MediaActionsMenu media={mediaActions}>
550-
<Pressable
551-
accessibilityRole="imagebutton"
552-
accessibilityLabel={props.alt ?? "Markdown image"}
553-
accessibilityHint={
554-
mediaActions.actions.length > 0 ? "Touch and hold for media actions" : undefined
555-
}
556-
className="items-center justify-center rounded-[10px] bg-md-code-bg"
557-
style={frameStyle}
558-
>
559-
{failed ? (
560-
<Text className="text-xs text-foreground-muted">Image unavailable</Text>
561-
) : (
562-
<ActivityIndicator />
563-
)}
564-
</Pressable>
565-
</MediaActionsMenu>
566-
) : (
567-
<PresentationSource identifier={sourceIdentifier} style={{ alignSelf: "flex-start" }}>
568-
<MediaActionsMenu media={mediaActions}>
569-
<Pressable
570-
accessibilityRole="imagebutton"
571-
accessibilityLabel={props.alt ?? "Markdown image"}
572-
accessibilityHint={
573-
mediaActions.actions.length > 0 ? "Touch and hold for media actions" : undefined
574-
}
575-
onPress={() =>
576-
// Quick Look picks the viewer from the name's extension, so it needs the
577-
// file name rather than the alt text.
578-
props.onPressPreview({
579-
kind: "image",
580-
uri: props.uri!,
581-
name: props.actionsSource?.name ?? props.alt ?? "Image",
582-
sourceIdentifier,
583-
actionsSource: props.actionsSource,
584-
})
585-
}
586-
style={{ alignSelf: "flex-start" }}
587-
>
588-
<View
589-
className="items-center justify-center overflow-hidden rounded-[10px] bg-md-code-bg"
590-
style={{
591-
...frameStyle,
592-
}}
593-
>
594-
<ThreadMarkdownImageRequest
595-
key={props.uri}
596-
uri={props.uri}
597-
onLoad={setSourceSize}
598-
onError={() => setFailedUri(props.uri)}
599-
/>
600-
</View>
601-
</Pressable>
602-
</MediaActionsMenu>
603-
</PresentationSource>
604-
)}
605-
{props.alt ? (
606-
<Text selectable className="text-xs text-foreground-muted">
607-
{props.alt}
608-
</Text>
609-
) : null}
610-
</View>
611-
);
612-
}
613-
614-
function ThreadMarkdownImageRequest(props: {
615-
readonly uri: string;
616-
readonly onLoad: (sourceSize: { width: number; height: number }) => void;
617-
readonly onError: () => void;
618-
}) {
619-
const [loaded, setLoaded] = useState(false);
620-
621-
return (
622-
<>
623-
<Image
624-
source={{ uri: props.uri }}
625-
resizeMode="contain"
626-
accessible={false}
627-
onLoad={(event) => {
628-
setLoaded(true);
629-
props.onLoad(event.nativeEvent.source);
630-
}}
631-
onError={props.onError}
632-
style={{ width: "100%", height: "100%", opacity: loaded ? 1 : 0 }}
633-
/>
634-
{loaded ? null : (
635-
<View
636-
pointerEvents="none"
637-
style={[StyleSheet.absoluteFill, { alignItems: "center", justifyContent: "center" }]}
638-
>
639-
<Text className="text-xs text-foreground-muted">Loading image…</Text>
640-
</View>
641-
)}
642-
</>
643-
);
644-
}
645-
646-
/** Environment-hosted image that loads through a signed asset URL. */
647-
function ThreadMarkdownImage(props: {
648-
readonly environmentId: EnvironmentId;
649-
readonly resource: Extract<AssetResource, { readonly _tag: "attachment" | "media-file" }>;
650-
readonly alt: string | null;
651-
readonly srcFragment?: string;
652-
readonly actionsSource?: MediaActionsSource;
653-
readonly onPressPreview: (source: FilePreviewSource) => void;
654-
}) {
655-
const assetUrl = useAssetUrlState(props.environmentId, props.resource);
656-
657-
return (
658-
<ThreadMarkdownImageView
659-
uri={assetUrl._tag === "Success" ? assetUrl.url + (props.srcFragment ?? "") : null}
660-
sourceKey={
661-
props.resource._tag === "attachment"
662-
? `attachment:${props.resource.attachmentId}`
663-
: `workspace:${props.resource.path}`
664-
}
665-
unavailable={assetUrl._tag === "Failure"}
666-
alt={props.alt}
667-
actionsSource={props.actionsSource}
668-
onPressPreview={props.onPressPreview}
669-
/>
670-
);
671-
}
672-
673508
const ThreadMediaVisibleContext = createContext(false);
674509
// LegendList only computes hook visibility when the list has a viewability config.
675510
const THREAD_MEDIA_VIEWABILITY_CONFIG = { itemVisiblePercentThreshold: 0 };
@@ -713,18 +548,6 @@ function ThreadMarkdownVideo(props: { readonly source: MediaVideoPreviewSource }
713548
);
714549
}
715550

716-
function ThreadMarkdownImageUnavailable(props: { readonly alt: string | null }) {
717-
return (
718-
<ThreadMarkdownImageView
719-
uri={null}
720-
sourceKey="unavailable"
721-
unavailable
722-
alt={props.alt}
723-
onPressPreview={() => undefined}
724-
/>
725-
);
726-
}
727-
728551
const MARKDOWN_MONO_FONT = Platform.select({
729552
ios: "ui-monospace",
730553
android: "monospace",

0 commit comments

Comments
 (0)