@devries-cameron20 / t3code Mirror · read-only / commits / d9559ee1aa
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 03:00 UTC

fix(web): render stored HEIC attachments (#10429)

Guillermo Casanova committed Oct 18, 2026 at 17:09 UTC d9559ee1aa24c46c05982764a0908f42b8d08b56
8 files changed +239 -13
apps/web/src/components/chat/ChatComposer.tsx
+35 -10
@@ -161,7 +161,12 @@ import {
161 type ComposerBannerStackContent,
162 type ComposerBannerStackItem,
163 } from "./ComposerBannerStack";
164 -import { compressImageForStash, prepareImageForAttachment } from "../../lib/imageCompression";
164 +import {
165 + compressImageForStash,
166 + isHeicImageFile,
167 + prepareImageForAttachment,
168 +} from "../../lib/imageCompression";
169 +import { AttachmentImage } from "../media/AttachmentImage";
170 import {
171 fileAttachmentTooLargeMessage,
172 formatAttachmentSize,
@@ -4860,12 +4865,22 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
4865 alt=""
4866 className="size-full object-cover"
4867 fallback={
4863 - <PierreEntryIcon
4864 - pathValue={image.name}
4865 - kind="file"
4866 - theme={resolvedTheme}
4867 - className="m-auto size-3.5"
4868 - />
4868 + isHeicImageFile({ name: image.name, type: image.mimeType }) ? (
4869 + <AttachmentImage
4870 + name={image.name}
4871 + mimeType={image.mimeType}
4872 + src={image.previewUrl}
4873 + alt=""
4874 + className="size-full object-cover"
4875 + />
4876 + ) : (
4877 + <PierreEntryIcon
4878 + pathValue={image.name}
4879 + kind="file"
4880 + theme={resolvedTheme}
4881 + className="m-auto size-3.5"
4882 + />
4883 + )
4884 }
4885 />
4886 ) : (
@@ -6729,9 +6744,19 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
6744 alt={image.name}
6745 className="h-full w-full object-cover"
6746 fallback={
6732 - <span className="flex h-full items-center justify-center px-1 text-3xs text-secondary-label">
6733 - {image.name}
6734 - </span>
6747 + isHeicImageFile({ name: image.name, type: image.mimeType }) ? (
6748 + <AttachmentImage
6749 + name={image.name}
6750 + mimeType={image.mimeType}
6751 + src={image.previewUrl}
6752 + alt={image.name}
6753 + className="h-full w-full object-cover"
6754 + />
6755 + ) : (
6756 + <span className="flex h-full items-center justify-center px-1 text-3xs text-secondary-label">
6757 + {image.name}
6758 + </span>
6759 + )
6760 }
6761 />
6762 </button>
apps/web/src/components/chat/ExpandedImageDialog.tsx
+1
@@ -233,6 +233,7 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
233 key={`${index}:${item.src}`}
234 src={item.src}
235 name={item.name}
236 + mimeType={item.mimeType}
237 onError={() => setFailedImageSrc(item.src)}
238 />
239 )}
apps/web/src/components/chat/ExpandedImagePreview.test.ts
+21
@@ -45,6 +45,27 @@ describe("resolveMarkdownMediaPreview", () => {
45 });
46
47 describe("buildExpandedImagePreview", () => {
48 + it("keeps MIME type for stored images without a filename extension", () => {
49 + const preview = buildExpandedImagePreview(
50 + [
51 + {
52 + type: "image",
53 + id: "heic-1",
54 + name: "photo",
55 + mimeType: "image/heic",
56 + sizeBytes: 3,
57 + previewUrl: "blob:heic",
58 + },
59 + ],
60 + "heic-1",
61 + );
62 + expect(preview?.images[0]).toMatchObject({
63 + name: "photo",
64 + mimeType: "image/heic",
65 + src: "blob:heic",
66 + });
67 + });
68 +
69 it("builds a signed-asset preview for a persisted video attachment", () => {
70 const preview = buildAttachmentVideoPreview(EnvironmentId.make("environment-1"), {
71 type: "file",
apps/web/src/components/chat/ExpandedImagePreview.tsx
+11 -1
@@ -22,6 +22,7 @@ import { resolveProtocolRelativeMediaUrl } from "../media/mediaContent";
22 export interface ExpandedImageItem {
23 src: string | null;
24 name: string;
25 + mimeType?: string;
26 type?: "video";
27 /** The preview created this object URL and must revoke it; other blob URLs belong to the timeline. */
28 ownsSrc?: boolean;
@@ -182,7 +183,15 @@ export function buildExpandedImagePreview(
183 }
184 const previewableImages = images.flatMap((image) =>
185 image.type === "image" && image.previewUrl
185 - ? [{ id: image.id, src: image.previewUrl, name: image.name, source: image.source }]
186 + ? [
187 + {
188 + id: image.id,
189 + src: image.previewUrl,
190 + name: image.name,
191 + mimeType: image.mimeType,
192 + source: image.source,
193 + },
194 + ]
195 : [],
196 );
197 if (previewableImages.length === 0) {
@@ -196,6 +205,7 @@ export function buildExpandedImagePreview(
205 images: previewableImages.map((image) => ({
206 src: image.src,
207 name: image.name,
208 + mimeType: image.mimeType,
209 ...(image.source?.kind === "snap-shot" ? { source: image.source } : {}),
210 })),
211 index: selectedIndex,
apps/web/src/components/chat/MessagesTimeline.tsx
+4 -1
@@ -161,6 +161,7 @@ import type {
161 import { Button, InlineButton } from "../ui/button";
162 import { MorphIcon } from "~/components/MorphIcon";
163 import { useAssetUrlRefresh, useAssetUrls, useAssetUrlState } from "../../assets/assetUrls";
164 +import { AttachmentImage } from "../media/AttachmentImage";
165 import { MediaVideoPlayer } from "../media/MediaVideoPlayer";
166 import { getVirtualizedScrollFadeClassName } from "../ui/scroll-area";
167 import {
@@ -2300,7 +2301,9 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
2301 ctx.onImageExpand(preview);
2302 }}
2303 >
2303 - <img
2304 + <AttachmentImage
2305 + name={image.name}
2306 + mimeType={image.mimeType}
2307 src={image.previewUrl}
2308 alt={image.name}
2309 className="block size-full object-cover"
apps/web/src/components/chat/ZoomableImage.tsx
+7 -1
@@ -5,6 +5,8 @@ import {
5 windowControlsOverlayHeight,
6 } from "../../lib/windowControlsOverlay";
7
8 +import { AttachmentImage } from "../media/AttachmentImage";
9 +
10 const MAX_ZOOM = 8;
11
12 // The viewer lays out below the native caption strip so its controls never share a row with
@@ -21,11 +23,13 @@ export interface ZoomableImageHandle {
23 export function ZoomableImage({
24 src,
25 name,
26 + mimeType,
27 onError,
28 ref,
29 }: {
30 src: string;
31 name: string;
32 + mimeType?: string | undefined;
33 onError: () => void;
34 ref?: Ref<ZoomableImageHandle>;
35 }) {
@@ -227,7 +231,9 @@ export function ZoomableImage({
231 setDragging(false);
232 }}
233 >
230 - <img
234 + <AttachmentImage
235 + name={name}
236 + mimeType={mimeType}
237 src={src}
238 alt={name}
239 draggable={false}
apps/web/src/components/media/AttachmentImage.test.tsx new
+98
@@ -0,0 +1,98 @@
1 +import { act, type SyntheticEvent } from "react";
2 +import { create, type ReactTestRenderer } from "react-test-renderer";
3 +import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
4 +import { AttachmentImage } from "./AttachmentImage";
5 +import { prepareImageForAttachment } from "../../lib/imageCompression";
6 +
7 +vi.mock("../../lib/imageCompression", async (importOriginal) => ({
8 + ...(await importOriginal<typeof import("../../lib/imageCompression")>()),
9 + prepareImageForAttachment: vi.fn(),
10 +}));
11 +
12 +let renderer: ReactTestRenderer;
13 +const fetchImage = vi.fn();
14 +const revokeUrl = vi.fn();
15 +const decodeError = {} as SyntheticEvent<HTMLImageElement>;
16 +const jpeg = new File(["jpeg"], "photo.jpg", { type: "image/jpeg" });
17 +
18 +beforeEach(() => {
19 + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
20 + vi.stubGlobal("fetch", fetchImage);
21 + vi.spyOn(URL, "createObjectURL").mockReturnValue("blob:jpeg");
22 + vi.spyOn(URL, "revokeObjectURL").mockImplementation(revokeUrl);
23 + fetchImage.mockResolvedValue(new Response(new Blob(["heic"])));
24 + vi.mocked(prepareImageForAttachment).mockResolvedValue({
25 + ok: true,
26 + file: jpeg,
27 + recompressed: true,
28 + });
29 +});
30 +
31 +afterEach(async () => {
32 + await act(() => renderer?.unmount());
33 + vi.restoreAllMocks();
34 + vi.unstubAllGlobals();
35 + vi.clearAllMocks();
36 +});
37 +
38 +it.each([
39 + { name: "photo.HEIC", mimeType: undefined },
40 + { name: "photo", mimeType: "image/heif" },
41 +])(
42 + "converts $name after native decoding fails and releases its preview",
43 + async ({ name, mimeType }) => {
44 + await act(() => {
45 + renderer = create(
46 + <AttachmentImage src="https://environment.test/photo" name={name} mimeType={mimeType} />,
47 + );
48 + });
49 + expect(fetchImage).not.toHaveBeenCalled();
50 + await act(() => renderer.root.findByType("img").props.onError(decodeError));
51 + expect(renderer.root.findByType("img").props.src).toBe("blob:jpeg");
52 + expect(prepareImageForAttachment).toHaveBeenCalledWith(
53 + expect.objectContaining({ name, type: "image/heic" }),
54 + 50 * 1024 * 1024,
55 + );
56 + await act(() => renderer.unmount());
57 + expect(revokeUrl).toHaveBeenCalledWith("blob:jpeg");
58 + },
59 +);
60 +
61 +it("leaves PNG errors to the caller without fetching or converting", async () => {
62 + const onError = vi.fn();
63 + await act(() => {
64 + renderer = create(<AttachmentImage src="blob:png" name="photo.png" onError={onError} />);
65 + });
66 + await act(() => renderer.root.findByType("img").props.onError(decodeError));
67 + expect(onError).toHaveBeenCalledWith(decodeError);
68 + expect(fetchImage).not.toHaveBeenCalled();
69 +});
70 +
71 +it("reports failed HEIC conversion through the existing error handler", async () => {
72 + const onError = vi.fn();
73 + vi.mocked(prepareImageForAttachment).mockResolvedValue({ ok: false, reason: "unreadable" });
74 + await act(() => {
75 + renderer = create(<AttachmentImage src="blob:heic" name="photo.heif" onError={onError} />);
76 + });
77 + await act(() => renderer.root.findByType("img").props.onError(decodeError));
78 + expect(onError).toHaveBeenCalledWith(decodeError);
79 + expect(URL.createObjectURL).not.toHaveBeenCalled();
80 +});
81 +
82 +it("discards an old decode when navigating to a different source", async () => {
83 + let finishDecode!: (result: Awaited<ReturnType<typeof prepareImageForAttachment>>) => void;
84 + vi.mocked(prepareImageForAttachment).mockReturnValue(
85 + new Promise((resolve) => {
86 + finishDecode = resolve;
87 + }),
88 + );
89 + await act(() => {
90 + renderer = create(<AttachmentImage src="blob:first" name="photo.heic" />);
91 + });
92 + await act(() => renderer.root.findByType("img").props.onError(decodeError));
93 + await act(() => renderer.update(<AttachmentImage src="blob:second" name="photo.heic" />));
94 + await act(() => finishDecode({ ok: true, file: jpeg, recompressed: true }));
95 + expect(renderer.root.findByType("img").props.src).toBe("blob:second");
96 + expect(URL.createObjectURL).not.toHaveBeenCalled();
97 + expect(fetchImage.mock.calls[0]?.[1].signal.aborted).toBe(true);
98 +});
apps/web/src/components/media/AttachmentImage.tsx new
+62
@@ -0,0 +1,62 @@
1 +import {
2 + useEffect,
3 + useEffectEvent,
4 + useState,
5 + type ComponentProps,
6 + type SyntheticEvent,
7 +} from "react";
8 +import {
9 + isHeicImageFile,
10 + MAX_COMPRESSIBLE_SOURCE_BYTES,
11 + prepareImageForAttachment,
12 +} from "../../lib/imageCompression";
13 +
14 +type AttachmentImageProps = ComponentProps<"img"> & { name: string; mimeType?: string | undefined };
15 +
16 +export function AttachmentImage({ name, mimeType, ...props }: AttachmentImageProps) {
17 + return isHeicImageFile({ name, type: mimeType ?? "" }) ? (
18 + <HeicImage key={props.src} {...props} name={name} />
19 + ) : (
20 + <img {...props} />
21 + );
22 +}
23 +
24 +function HeicImage({ name, src, onError, ...props }: Omit<AttachmentImageProps, "mimeType">) {
25 + const [decodeError, setDecodeError] = useState<SyntheticEvent<HTMLImageElement> | null>(null);
26 + const [previewUrl, setPreviewUrl] = useState<string>();
27 +
28 + const reportError = useEffectEvent((event: SyntheticEvent<HTMLImageElement>) => onError?.(event));
29 +
30 + useEffect(() => {
31 + if (!decodeError || !src) return;
32 + const controller = new AbortController();
33 + let objectUrl: string | undefined;
34 + const decode = async () => {
35 + try {
36 + const response = await fetch(src, { signal: controller.signal });
37 + if (!response.ok) return reportError(decodeError);
38 + const blob = await response.blob();
39 + if (controller.signal.aborted) return;
40 + const result = await prepareImageForAttachment(
41 + new File([blob], name, { type: "image/heic" }),
42 + MAX_COMPRESSIBLE_SOURCE_BYTES,
43 + );
44 + if (controller.signal.aborted) return;
45 + if (!result.ok) return reportError(decodeError);
46 + objectUrl = URL.createObjectURL(result.file);
47 + setPreviewUrl(objectUrl);
48 + } catch {
49 + if (!controller.signal.aborted) reportError(decodeError);
50 + }
51 + };
52 + void decode();
53 + return () => {
54 + controller.abort();
55 + if (objectUrl) URL.revokeObjectURL(objectUrl);
56 + };
57 + }, [decodeError, name, src]);
58 +
59 + return (
60 + <img {...props} src={previewUrl ?? src} onError={decodeError ? onError : setDecodeError} />
61 + );
62 +}