Skip to content
TypeScript

fix(web): size the chat image slot from server-reported dimensions (#10200)

Julius Marminged0f855bfa91b
diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx
index 55de831a52e..4e592c406b1 100644
--- a/apps/web/src/components/ChatMarkdown.tsx
+++ b/apps/web/src/components/ChatMarkdown.tsx
@@ -1235,9 +1235,15 @@ function markdownImageCopy(alt: string, src: string, title: string | undefined):
return `![${escapedAlt}](${src}${titleSuffix})`;
}
+/**
+ * `maxHeightRem` folds a height cap into the width bound: `max-height` alone
+ * would not feed back through `aspect-ratio` once `width` is definite, so a
+ * tall image would keep a box wider than the picture it draws.
+ */
function authoredImageSizeStyle(
width: string | number | undefined,
height: string | number | undefined,
+ maxHeightRem = 30,
): CSSProperties | undefined {
const parsedWidth = Number(width);
const parsedHeight = Number(height);
@@ -1248,7 +1254,7 @@ function authoredImageSizeStyle(
width: parsedWidth,
height: "auto",
aspectRatio: `${parsedWidth} / ${parsedHeight}`,
- maxWidth: `min(100%, 30rem, ${(30 * parsedWidth) / parsedHeight}rem)`,
+ maxWidth: `min(100%, 30rem, ${(maxHeightRem * parsedWidth) / parsedHeight}rem)`,
};
}
if (hasWidth) return { maxWidth: `min(100%, 30rem, ${parsedWidth}px)` };
@@ -1522,6 +1528,8 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
readonly srcFragment?: string;
/** Reserve a slot while loading; off for images that share a line with text. */
readonly standalone?: boolean | undefined;
+ /** Caps the box height in rem while keeping the image's ratio; 30 by default. */
+ readonly maxHeightRem?: number | undefined;
readonly style?: CSSProperties | undefined;
readonly workspaceRoot?: string | undefined;
readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
@@ -1538,6 +1546,18 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
const reference = path ? mediaFileReference(path, props.workspaceRoot) : undefined;
const relativePath = reference?.relativePath;
const src = assetUrl._tag === "Success" ? assetUrl.url + (props.srcFragment ?? "") : null;
+ // The server reads the pixel size from the file header, so the slot can be
+ // the image's final box instead of a 16:9 guess. An authored size wins; a
+ // caller's height cap shrinks the box while keeping the ratio.
+ const knownSize = assetUrl._tag === "Success" ? assetUrl.imageDimensions : undefined;
+ const maxHeightRem = props.maxHeightRem ?? 30;
+ const style =
+ props.style ??
+ (knownSize
+ ? authoredImageSizeStyle(knownSize.width, knownSize.height, maxHeightRem)
+ : maxHeightRem !== 30
+ ? { maxHeight: `${maxHeightRem}rem` }
+ : undefined);
const actionsSource: MediaActionSource = {
kind: props.kind ?? "image",
name: props.alt || (props.kind ?? "image"),
@@ -1581,7 +1601,7 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
copyMarkdown={props.copyMarkdown}
standalone={props.standalone ?? true}
className={CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME}
- style={props.style}
+ style={style}
actionsSource={actionsSource}
onImageExpand={props.onImageExpand}
/>
diff --git a/apps/web/src/components/ChatMarkdown.workspace-images.test.tsx b/apps/web/src/components/ChatMarkdown.workspace-images.test.tsx
index ad3e951d89a..17d9c04b06c 100644
--- a/apps/web/src/components/ChatMarkdown.workspace-images.test.tsx
+++ b/apps/web/src/components/ChatMarkdown.workspace-images.test.tsx
@@ -5,6 +5,7 @@ import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
const testState = vi.hoisted(() => ({
resources: [] as Array<unknown>,
assetState: "success" as "success" | "loading" | "failure",
+ imageDimensions: undefined as { width: number; height: number } | undefined,
}));
vi.mock("@effect/atom-react", () => ({ useAtomValue: () => null }));
@@ -14,7 +15,11 @@ vi.mock("../assets/assetUrls", () => ({
testState.resources.push(resource);
if (testState.assetState === "loading") return { _tag: "Loading" };
if (testState.assetState === "failure") return { _tag: "Failure" };
- return { _tag: "Success", url: "https://signed.test/workspace-image.svg" };
+ return {
+ _tag: "Success",
+ url: "https://signed.test/workspace-image.svg",
+ ...(testState.imageDimensions ? { imageDimensions: testState.imageDimensions } : {}),
+ };
},
}));
vi.mock("../hooks/useTheme", () => ({ useTheme: () => ({ resolvedTheme: "dark" }) }));
@@ -42,7 +47,7 @@ vi.mock("~/lib/openPullRequestLink", () => ({
useOpenChangeRequestLink: () => vi.fn(),
}));
-import ChatMarkdown from "./ChatMarkdown";
+import ChatMarkdown, { ChatMarkdownAssetImage } from "./ChatMarkdown";
import { FileMarkdownPreview } from "./files/FileMarkdownPreview";
const threadRef = {
@@ -92,6 +97,7 @@ describe("ChatMarkdown workspace images", () => {
beforeEach(() => {
testState.resources = [];
testState.assetState = "success";
+ testState.imageDimensions = undefined;
});
it.each([
@@ -245,6 +251,42 @@ describe("ChatMarkdown workspace images", () => {
expect(html).toContain('<span id="user-content-diagram"');
});
+ it("sizes the slot from server-reported dimensions so a portrait image never grows", () => {
+ testState.imageDimensions = { width: 720, height: 1400 };
+
+ const style = firstInlineStyle(render("![shot](.t3/workspace-image.svg)"));
+
+ expect(style).toMatchObject({ width: "720px", "aspect-ratio": "720 / 1400" });
+ });
+
+ it("folds a caller's height cap into the width bound so the ratio holds", () => {
+ testState.imageDimensions = { width: 720, height: 1400 };
+
+ const html = renderToStaticMarkup(
+ <ChatMarkdownAssetImage
+ environmentId={threadRef.environmentId}
+ resource={{ _tag: "media-file", threadId: threadRef.threadId, path: "/shot.png" }}
+ alt="shot"
+ maxHeightRem={16}
+ />,
+ );
+
+ expect(firstInlineStyle(html)).toMatchObject({
+ "aspect-ratio": "720 / 1400",
+ "max-width": `min(100%, 30rem, ${(16 * 720) / 1400}rem)`,
+ });
+ });
+
+ it("lets an authored size override server-reported dimensions", () => {
+ testState.imageDimensions = { width: 720, height: 1400 };
+
+ const style = firstInlineStyle(
+ render('<img src=".t3/workspace-image.svg" alt="sized" width="96" height="128">'),
+ );
+
+ expect(style).toMatchObject({ width: "96px", "aspect-ratio": "96 / 128" });
+ });
+
it("reserves a slot for an image that is alone in a list item", () => {
expect(render("- ![shot](.t3/workspace-image.svg)")).toContain("aspect-video");
});
diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx
index 4b41e98e062..c1b09899b8d 100644
--- a/apps/web/src/components/chat/MessagesTimeline.tsx
+++ b/apps/web/src/components/chat/MessagesTimeline.tsx
@@ -3342,7 +3342,7 @@ const PlainWorkEntryRow = memo(function PlainWorkEntryRow(props: {
alt={viewedImage.alt}
srcFragment={viewedImage.srcFragment}
workspaceRoot={workspaceRoot}
- style={{ maxHeight: "16rem" }}
+ maxHeightRem={16}
onImageExpand={onImageExpand}
/>
</div>