Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
fix(mobile): preserve markdown image dimensions (#7940)
Dara Adedeji5a7a7cf2925c
diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsxindex 145737523..280ab4eca 100644--- a/apps/mobile/src/features/threads/ThreadFeed.tsx+++ b/apps/mobile/src/features/threads/ThreadFeed.tsx@@ -40,6 +40,7 @@ import {type ColorValue,useWindowDimensions,View,+ type ViewStyle,} from "react-native";import { TouchableOpacity } from "react-native-gesture-handler";import ImageViewing from "react-native-image-viewing";@@ -105,6 +106,7 @@ import {import { useMarkdownCodeHighlight } from "./markdownCodeHighlightState";import { useAssetUrl, useAssetUrlState } from "../../state/assets";import { resolveWorkspaceRelativeFilePath } from "../files/filePath";+import { MARKDOWN_IMAGE_MAX_WIDTH, resolveMarkdownImageDisplaySize } from "./markdownImageSize";const WIDE_MARKDOWN_BLOCK_OPTIONS = {includeOrderedLists: Platform.OS === "android",@@ -196,31 +198,50 @@ function MessageAttachmentImage(props: {);}-/** Markdown image whose src is a workspace file — loads through a signed asset URL. */-function ThreadMarkdownImage(props: {- readonly environmentId: EnvironmentId;- readonly threadId: ThreadId;- readonly path: string;+function ThreadMarkdownImageView(props: {+ readonly uri: string | null;+ readonly sourceKey: string;+ readonly unavailable: boolean;readonly alt: string | null;readonly onPressImage: (uri: string) => void;}) {const codeBackground = useThemeColor("--color-md-code-bg");+ const [availableWidth, setAvailableWidth] = useState(0);+ const [sourceSize, setSourceSize] = useState<{ width: number; height: number } | null>(null);const [failedUri, setFailedUri] = useState<string | null>(null);- const assetUrl = useAssetUrlState(props.environmentId, {- _tag: "workspace-file",- threadId: props.threadId,- path: props.path,- });- const uri = assetUrl._tag === "Success" ? assetUrl.url : null;- const failed = assetUrl._tag === "Failure" || (uri !== null && failedUri === uri);+ const activeUriRef = useRef(props.uri);+ activeUriRef.current = props.uri;++ useEffect(() => {+ setSourceSize(null);+ }, [props.sourceKey]);++ useEffect(() => {+ setFailedUri(null);+ }, [props.uri]);++ const displaySize =+ sourceSize === null+ ? null+ : resolveMarkdownImageDisplaySize({+ sourceWidth: sourceSize.width,+ sourceHeight: sourceSize.height,+ availableWidth,+ });+ const failed = props.unavailable || (props.uri !== null && failedUri === props.uri);+ const placeholderWidth: ViewStyle["width"] =+ availableWidth > 0 ? Math.min(availableWidth, MARKDOWN_IMAGE_MAX_WIDTH) : "100%";+ const frameStyle: ViewStyle = displaySize ?? { width: placeholderWidth, aspectRatio: 16 / 9 };return (- <View style={{ gap: 6 }}>- {uri === null || failed ? (+ <View+ onLayout={(event) => setAvailableWidth(event.nativeEvent.layout.width)}+ style={{ alignSelf: "stretch", gap: 6 }}+ >+ {props.uri === null || failed ? (<Viewstyle={{- width: "100%",- aspectRatio: 16 / 9,+ ...frameStyle,borderRadius: 10,backgroundColor: codeBackground,alignItems: "center",@@ -238,20 +259,37 @@ function ThreadMarkdownImage(props: {accessibilityRole="imagebutton"accessibilityLabel={props.alt ?? "Markdown image"}activeOpacity={0.7}- onPress={() => props.onPressImage(uri)}+ onPress={() => props.onPressImage(props.uri!)}+ style={{ alignSelf: "flex-start" }}>- <Image- source={{ uri }}- resizeMode="contain"- accessible={false}- onError={() => setFailedUri(uri)}+ <Viewstyle={{- width: "100%",- aspectRatio: 16 / 9,+ ...frameStyle,borderRadius: 10,backgroundColor: codeBackground,+ alignItems: "center",+ justifyContent: "center",+ overflow: "hidden",}}- />+ >+ <Image+ source={{ uri: props.uri }}+ resizeMode="contain"+ accessible={false}+ onLoad={(event) => {+ if (activeUriRef.current !== props.uri) return;+ const { width, height } = event.nativeEvent.source;+ setSourceSize({ width, height });+ }}+ onError={() => setFailedUri(props.uri)}+ style={{+ width: "100%",+ height: "100%",+ opacity: displaySize === null ? 0 : 1,+ }}+ />+ {displaySize === null ? <ActivityIndicator style={StyleSheet.absoluteFill} /> : null}+ </View></TouchableOpacity>)}{props.alt ? (@@ -263,28 +301,40 @@ function ThreadMarkdownImage(props: {);}+/** Markdown image whose src is a workspace file — loads through a signed asset URL. */+function ThreadMarkdownImage(props: {+ readonly environmentId: EnvironmentId;+ readonly threadId: ThreadId;+ readonly path: string;+ readonly alt: string | null;+ readonly onPressImage: (uri: string) => void;+}) {+ const assetUrl = useAssetUrlState(props.environmentId, {+ _tag: "workspace-file",+ threadId: props.threadId,+ path: props.path,+ });++ return (+ <ThreadMarkdownImageView+ uri={assetUrl._tag === "Success" ? assetUrl.url : null}+ sourceKey={props.path}+ unavailable={assetUrl._tag === "Failure"}+ alt={props.alt}+ onPressImage={props.onPressImage}+ />+ );+}+function ThreadMarkdownImageUnavailable(props: { readonly alt: string | null }) {- const codeBackground = useThemeColor("--color-md-code-bg");return (- <View style={{ gap: 6 }}>- <View- style={{- width: "100%",- aspectRatio: 16 / 9,- borderRadius: 10,- backgroundColor: codeBackground,- alignItems: "center",- justifyContent: "center",- }}- >- <Text className="text-xs text-foreground-muted">Image unavailable</Text>- </View>- {props.alt ? (- <Text selectable className="text-xs text-foreground-muted">- {props.alt}- </Text>- ) : null}- </View>+ <ThreadMarkdownImageView+ uri={null}+ sourceKey="unavailable"+ unavailable+ alt={props.alt}+ onPressImage={() => undefined}+ />);}@@ -1530,7 +1580,15 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {(image) => {const imageSource = classifyMarkdownImageSource(image.href, props.workspaceRoot ?? null);if (imageSource._tag === "Direct") {- return null;+ return (+ <ThreadMarkdownImageView+ uri={imageSource.uri}+ sourceKey={imageSource.uri}+ unavailable={false}+ alt={image.alt}+ onPressImage={(uri) => setExpandedImage({ uri })}+ />+ );}if (imageSource._tag === "Blocked") {return <ThreadMarkdownImageUnavailable alt={image.alt} />;diff --git a/apps/mobile/src/features/threads/markdownImageSize.test.ts b/apps/mobile/src/features/threads/markdownImageSize.test.tsnew file mode 100644index 000000000..76170890d--- /dev/null+++ b/apps/mobile/src/features/threads/markdownImageSize.test.ts@@ -0,0 +1,62 @@+import { describe, expect, it } from "vite-plus/test";++import {+ MARKDOWN_IMAGE_MAX_HEIGHT,+ MARKDOWN_IMAGE_MAX_WIDTH,+ resolveMarkdownImageDisplaySize,+} from "./markdownImageSize";++describe("resolveMarkdownImageDisplaySize", () => {+ it("keeps small images at their intrinsic size", () => {+ expect(+ resolveMarkdownImageDisplaySize({+ sourceWidth: 96,+ sourceHeight: 96,+ availableWidth: 332,+ }),+ ).toEqual({ width: 96, height: 96 });+ });++ it("fits wide images to the available chat width", () => {+ expect(+ resolveMarkdownImageDisplaySize({+ sourceWidth: 960,+ sourceHeight: 540,+ availableWidth: 332,+ }),+ ).toEqual({ width: 332, height: 186.75 });+ });++ it("caps wide images at 480 points on larger screens", () => {+ expect(+ resolveMarkdownImageDisplaySize({+ sourceWidth: 960,+ sourceHeight: 540,+ availableWidth: 900,+ }),+ ).toEqual({ width: MARKDOWN_IMAGE_MAX_WIDTH, height: 270 });+ });++ it("caps tall images by height without changing their aspect ratio", () => {+ expect(+ resolveMarkdownImageDisplaySize({+ sourceWidth: 400,+ sourceHeight: 800,+ availableWidth: 332,+ }),+ ).toEqual({ width: 240, height: MARKDOWN_IMAGE_MAX_HEIGHT });+ });++ it("rejects dimensions that cannot produce a stable layout", () => {+ expect(+ resolveMarkdownImageDisplaySize({ sourceWidth: 0, sourceHeight: 100, availableWidth: 332 }),+ ).toBeNull();+ expect(+ resolveMarkdownImageDisplaySize({+ sourceWidth: 100,+ sourceHeight: Number.NaN,+ availableWidth: 332,+ }),+ ).toBeNull();+ });+});diff --git a/apps/mobile/src/features/threads/markdownImageSize.ts b/apps/mobile/src/features/threads/markdownImageSize.tsnew file mode 100644index 000000000..0fb6f8fbc--- /dev/null+++ b/apps/mobile/src/features/threads/markdownImageSize.ts@@ -0,0 +1,37 @@+export const MARKDOWN_IMAGE_MAX_WIDTH = 480;+export const MARKDOWN_IMAGE_MAX_HEIGHT = 480;++export interface MarkdownImageDisplaySize {+ readonly width: number;+ readonly height: number;+}++/** Keeps small images intrinsic while fitting larger images inside the chat viewport. */+export function resolveMarkdownImageDisplaySize(input: {+ readonly sourceWidth: number;+ readonly sourceHeight: number;+ readonly availableWidth: number;+}): MarkdownImageDisplaySize | null {+ if (+ !Number.isFinite(input.sourceWidth) ||+ !Number.isFinite(input.sourceHeight) ||+ !Number.isFinite(input.availableWidth) ||+ input.sourceWidth <= 0 ||+ input.sourceHeight <= 0 ||+ input.availableWidth <= 0+ ) {+ return null;+ }++ const scale = Math.min(+ 1,+ input.availableWidth / input.sourceWidth,+ MARKDOWN_IMAGE_MAX_WIDTH / input.sourceWidth,+ MARKDOWN_IMAGE_MAX_HEIGHT / input.sourceHeight,+ );++ return {+ width: input.sourceWidth * scale,+ height: input.sourceHeight * scale,+ };+}