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 22, 00:45 UTC
fix(web): preserve previews during attachment preparation (#33949)
Yash Singh committed
Nov 10, 2026 at 15:54 UTC
8fc6fd01d70a8da940781d04802c633ea782a9a7
9 files changed
+350
-79
apps/web/src/components/chat/ComposerAttachmentStrip.tsx
+51
-20
@@ -6,7 +6,7 @@ import type {
6
SnapShotSource,
7
} from "@t3tools/contracts";
8
import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments";
9
-import { CircleAlertIcon, PaperclipIcon, PlayIcon, XIcon } from "lucide-react";
9
+import { CircleAlertIcon, ImageIcon, PaperclipIcon, PlayIcon, XIcon } from "lucide-react";
10
import { type ComponentProps, useLayoutEffect, useRef } from "react";
11
12
import {
@@ -16,7 +16,8 @@ import {
16
composerFileNeedsReattach,
17
} from "../../composerDraftStore";
18
import { retryAttachmentUpload, useAttachmentUploadStore } from "../../lib/attachmentUploadQueue";
19
-import { formatAttachmentUploadProgress } from "../../lib/attachmentUploadState";
19
+import type { AttachmentUploadState } from "../../lib/attachmentUploadState";
20
+import { useAttachmentUploadProgress } from "../../hooks/useAttachmentUploadProgress";
21
import { isHeicImageFile } from "../../lib/imageCompression";
22
import {
23
type PendingSnapShotAnimation,
@@ -47,6 +48,30 @@ import {
48
} from "./SnapShotAttachmentDetails";
49
import { prepareVideoFirstFrame } from "../../lib/videoFirstFrame";
50
51
+function ComposerAttachmentUploadProgress({
52
+ id,
53
+ upload,
54
+ fallback,
55
+}: {
56
+ id: string;
57
+ upload: AttachmentUploadState | undefined;
58
+ fallback?: string;
59
+}) {
60
+ return useAttachmentUploadProgress(id, upload) ?? fallback ?? null;
61
+}
62
+
63
+function ComposerImageFallback() {
64
+ return (
65
+ <span
66
+ role="img"
67
+ aria-label="Image attachment"
68
+ className="flex size-full items-center justify-center p-3 text-secondary-label"
69
+ >
70
+ <ImageIcon aria-hidden="true" className="size-4" />
71
+ </span>
72
+ );
73
+}
74
+
75
function ComposerVideoThumbnail({ file }: { file: File }) {
76
const setVideo = (video: HTMLVideoElement | null) => {
77
if (!video) return;
@@ -214,21 +239,17 @@ export function ComposerAttachmentMediaRow({
239
name={ready.name}
240
mimeType={ready.mimeType}
241
src={ready.previewUrl}
217
- alt={image.name}
242
+ alt=""
243
className="h-full w-full object-cover"
244
/>
245
) : (
221
- <span className="flex h-full items-center justify-center px-1 text-3xs text-secondary-label">
222
- {image.name}
223
- </span>
246
+ <ComposerImageFallback />
247
)
248
}
249
/>
250
</button>
251
) : (
229
- <div className="flex h-full w-full items-center justify-center px-1 text-center text-3xs text-secondary-label">
230
- {image.name}
231
- </div>
252
+ <ComposerImageFallback />
253
)}
254
{source?.kind === "snap-shot" ? (
255
<SnapShotAttachmentDetails
@@ -257,10 +278,12 @@ export function ComposerAttachmentMediaRow({
278
</TooltipPopup>
279
</Tooltip>
280
)}
260
- {(ready ? upload?.status === "uploading" : supportsAttachmentUploads) && (
281
+ {(!ready || upload?.status === "uploading") && (
282
<span className="pointer-events-none absolute inset-x-0 bottom-0 bg-background/85 px-1 text-center text-3xs text-foreground">
262
- {formatAttachmentUploadProgress(
263
- upload?.status === "uploading" ? upload.progress : 0,
283
+ {upload?.status === "uploading" ? (
284
+ <ComposerAttachmentUploadProgress id={image.id} upload={upload} />
285
+ ) : (
286
+ "Preparing"
287
)}
288
</span>
289
)}
@@ -359,7 +382,7 @@ export function ComposerAttachmentMediaRow({
382
</button>
383
{upload?.status === "uploading" && (
384
<span className="pointer-events-none absolute inset-x-0 bottom-0 bg-background/85 px-1 text-center text-3xs text-foreground">
362
- {formatAttachmentUploadProgress(upload.progress)}
385
+ <ComposerAttachmentUploadProgress id={file.id} upload={upload} />
386
</span>
387
)}
388
{upload?.status === "failed" && (
@@ -453,13 +476,21 @@ export function ComposerAttachmentFileList({
476
{file.name}
477
</button>
478
<span className="shrink-0 text-xs text-secondary-label">
456
- {needsReattach
457
- ? canReattachFile
458
- ? "Attach again"
459
- : "Remove to send"
460
- : upload?.status === "uploading"
461
- ? formatAttachmentUploadProgress(upload.progress)
462
- : formatAttachmentSize(file.sizeBytes)}
479
+ {needsReattach ? (
480
+ canReattachFile ? (
481
+ "Attach again"
482
+ ) : (
483
+ "Remove to send"
484
+ )
485
+ ) : upload?.status === "uploading" ? (
486
+ <ComposerAttachmentUploadProgress
487
+ id={file.id}
488
+ upload={upload}
489
+ fallback={formatAttachmentSize(file.sizeBytes)}
490
+ />
491
+ ) : (
492
+ formatAttachmentSize(file.sizeBytes)
493
+ )}
494
</span>
495
{!needsReattach && upload?.status === "failed" ? (
496
<Tooltip>
apps/web/src/components/chat/ComposerImageThumbnail.browser.test.tsx
new
+69
@@ -0,0 +1,69 @@
1
+import { expect, it, vi } from "vite-plus/test";
2
+
3
+import { act, render } from "../../../test/render";
4
+import { createComposerImageThumbnail } from "../../lib/imageCompression";
5
+import { ComposerImageThumbnail } from "./ComposerImageThumbnail";
6
+
7
+it("keeps a native preview available after thumbnail generation has failed", async () => {
8
+ const canvas = document.createElement("canvas");
9
+ canvas.width = 2;
10
+ canvas.height = 2;
11
+ const blob = await new Promise<Blob>((resolve) => canvas.toBlob((blob) => resolve(blob!)));
12
+ const file = new File([blob], "screenshot.png", { type: "image/png" });
13
+ using _bitmap = vi
14
+ .spyOn(window, "createImageBitmap")
15
+ .mockRejectedValue(new Error("Thumbnail decoder unavailable"));
16
+ expect(await createComposerImageThumbnail(file)).toBeNull();
17
+
18
+ const container = document.createElement("div");
19
+ const holdLoad = (event: Event) => event.stopPropagation();
20
+ container.addEventListener("load", holdLoad, true);
21
+ using complete = vi.spyOn(HTMLImageElement.prototype, "complete", "get").mockReturnValue(false);
22
+ await using screen = await render(
23
+ <ComposerImageThumbnail
24
+ file={file}
25
+ alt={file.name}
26
+ className="size-16 object-cover"
27
+ fallback={<span>Preview unavailable</span>}
28
+ />,
29
+ { container },
30
+ );
31
+ using revoke = vi.spyOn(URL, "revokeObjectURL");
32
+ await expect.element(screen.getByText("Preview unavailable")).toBeVisible();
33
+ const loadingImage = screen.container.querySelector("img")!;
34
+ await loadingImage.decode();
35
+ complete.mockRestore();
36
+ container.removeEventListener("load", holdLoad, true);
37
+ await act(() => loadingImage.dispatchEvent(new Event("load")));
38
+ const image = screen.getByRole("img", { name: file.name });
39
+ await expect.element(image).toBeVisible();
40
+ await expect.element(screen.getByText("Preview unavailable")).not.toBeInTheDocument();
41
+ const src = image.element().getAttribute("src");
42
+ expect(src).toMatch(/^blob:/);
43
+ await screen.unmount();
44
+ expect(revoke).toHaveBeenCalledWith(src);
45
+});
46
+
47
+it("shows the fallback while HEIC preparation is pending", async () => {
48
+ let finishReading!: (bytes: ArrayBuffer) => void;
49
+ const metadata = new Promise<ArrayBuffer>((resolve) => {
50
+ finishReading = resolve;
51
+ });
52
+ using _read = vi.spyOn(Blob.prototype, "arrayBuffer").mockReturnValue(metadata);
53
+ const file = new File(["pending"], "photo.heic", { type: "image/heic" });
54
+
55
+ await using screen = await render(
56
+ <ComposerImageThumbnail
57
+ file={file}
58
+ alt={file.name}
59
+ className="size-16 object-cover"
60
+ fallback={<span role="img" aria-label="Preparing image" />}
61
+ />,
62
+ );
63
+ try {
64
+ expect(screen.container.querySelector('[aria-label="Preparing image"]')).not.toBeNull();
65
+ } finally {
66
+ finishReading(new ArrayBuffer(0));
67
+ await createComposerImageThumbnail(file);
68
+ }
69
+});
apps/web/src/components/chat/ComposerImageThumbnail.tsx
+15
-16
@@ -33,11 +33,7 @@ export const ComposerImageThumbnail = memo(function ComposerImageThumbnail({
33
let disposed = false;
34
let localUrl: string | undefined;
35
const cached = readComposerImageThumbnail(file);
36
- if (
37
- file.size <= MAX_COMPRESSIBLE_SOURCE_BYTES &&
38
- !isHeicImageFile(file) &&
39
- cached === undefined
40
- ) {
36
+ if (file.size <= MAX_COMPRESSIBLE_SOURCE_BYTES && !isHeicImageFile(file) && !cached) {
37
localUrl = URL.createObjectURL(file);
38
// oxlint-disable-next-line react/set-state-in-effect -- the blob URL is created and revoked by this effect
39
setPreview({ file, src: localUrl });
@@ -66,17 +62,20 @@ export const ComposerImageThumbnail = memo(function ComposerImageThumbnail({
62
}, [file]);
63
64
return src && failedSrc !== src ? (
69
- <img
70
- ref={(image) => {
71
- if (image?.complete && image.naturalWidth > 0) setLoadedSrc(src);
72
- }}
73
- src={src}
74
- alt={alt}
75
- onLoad={() => setLoadedSrc(src)}
76
- onError={() => setFailedSrc(src)}
77
- className={cn(className, loadedSrc !== src && "invisible")}
78
- />
79
- ) : src === undefined ? null : (
65
+ <span className="relative block size-full">
66
+ {loadedSrc !== src && fallback}
67
+ <img
68
+ ref={(image) => {
69
+ if (image?.complete && image.naturalWidth > 0) setLoadedSrc(src);
70
+ }}
71
+ src={src}
72
+ alt={alt}
73
+ onLoad={() => setLoadedSrc(src)}
74
+ onError={() => setFailedSrc(src)}
75
+ className={cn(className, loadedSrc !== src && "invisible absolute inset-0")}
76
+ />
77
+ </span>
78
+ ) : (
79
fallback
80
);
81
});
apps/web/src/components/composerContextPresentation.tsx
+12
-9
@@ -15,10 +15,8 @@ import { ComposerContextActionsContext } from "./composerContextActions";
15
import type { ComposerFileAttachment, ComposerImageAttachment } from "~/composerDraftStore";
16
import { composerFileNeedsReattach } from "~/composerDraftStore";
17
import { useTheme } from "~/hooks/useTheme";
18
-import {
19
- formatAttachmentUploadProgress,
20
- type AttachmentUploadState,
21
-} from "~/lib/attachmentUploadState";
18
+import type { AttachmentUploadState } from "~/lib/attachmentUploadState";
19
+import { useAttachmentUploadProgress } from "~/hooks/useAttachmentUploadProgress";
20
import { PullRequestGlyph } from "~/components/pullRequest/pullRequestIcons";
21
import {
22
fileContextReference,
@@ -167,10 +165,13 @@ function ContextChip(props: {
165
);
166
}
167
170
-function uploadStatusSuffix(upload: AttachmentUploadState | undefined): string | null {
171
- if (upload?.status === "uploading") return formatAttachmentUploadProgress(upload.progress);
168
+function useUploadStatusSuffix(
169
+ key: string,
170
+ upload: AttachmentUploadState | undefined,
171
+): string | null {
172
+ const progress = useAttachmentUploadProgress(key, upload);
173
if (upload?.status === "failed") return "upload failed";
173
- return null;
174
+ return progress;
175
}
176
177
function attachmentTooltip(
@@ -187,6 +188,7 @@ function ImageContextChip(props: {
188
upload: AttachmentUploadState | undefined;
189
}) {
190
const actions = use(ComposerContextActionsContext);
191
+ const suffix = useUploadStatusSuffix(props.record.id, props.upload);
192
return (
193
<Tooltip>
194
<TooltipTrigger
@@ -195,7 +197,7 @@ function ImageContextChip(props: {
197
name={props.record.name}
198
previewUrl={props.record.previewUrl}
199
size={formatAttachmentSize(props.record.sizeBytes)}
198
- suffix={uploadStatusSuffix(props.upload)}
200
+ suffix={suffix}
201
onClick={() => actions.expandImage(props.record.id)}
202
/>
203
}
@@ -214,7 +216,8 @@ function FileContextChip(props: {
216
const actions = use(ComposerContextActionsContext);
217
const { resolvedTheme } = useTheme();
218
const needsReattach = composerFileNeedsReattach(props.record);
217
- const suffix = needsReattach ? "attach again" : uploadStatusSuffix(props.upload);
219
+ const uploadSuffix = useUploadStatusSuffix(props.record.id, props.upload);
220
+ const suffix = needsReattach ? "attach again" : uploadSuffix;
221
const size = formatAttachmentSize(props.record.sizeBytes);
222
const isVideo = videoMimeType(props.record) !== null;
223
return (
apps/web/src/hooks/useAttachmentUploadProgress.browser.test.tsx
new
+47
@@ -0,0 +1,47 @@
1
+import { EnvironmentId } from "@t3tools/contracts";
2
+import { expect, it, vi } from "vite-plus/test";
3
+
4
+import { act, fakeTimers, render } from "../../test/render";
5
+import type { AttachmentUploadState } from "../lib/attachmentUploadState";
6
+import { useAttachmentUploadProgress } from "./useAttachmentUploadProgress";
7
+
8
+function Probe({ id = "image", upload }: { id?: string; upload: AttachmentUploadState }) {
9
+ const progress = useAttachmentUploadProgress(id, upload);
10
+ return <output>{progress ?? "hidden"}</output>;
11
+}
12
+
13
+it("skips fast uploads and delays each attempt without delaying later progress or completion", async () => {
14
+ const environmentId = EnvironmentId.make("environment");
15
+ const uploading = (progress: number) => ({
16
+ status: "uploading" as const,
17
+ environmentId,
18
+ progress,
19
+ });
20
+ const ready = { status: "ready" as const, environmentId, attachmentId: "attachment" };
21
+ // The first render reads the clock to schedule the badge.
22
+ using _timers = fakeTimers({ now: 0 });
23
+ await using screen = await render(<Probe upload={uploading(0)} />);
24
+ await expect.element(screen.getByText("hidden")).toBeInTheDocument();
25
+ await act(() => vi.advanceTimersByTime(75));
26
+ await screen.rerender(<Probe upload={ready} />);
27
+ await act(() => vi.advanceTimersByTime(75));
28
+ await expect.element(screen.getByText("hidden")).toBeInTheDocument();
29
+
30
+ await screen.rerender(<Probe upload={uploading(0)} />);
31
+ await act(() => vi.advanceTimersByTime(149));
32
+ await screen.rerender(<Probe upload={uploading(0.45)} />);
33
+ await expect.element(screen.getByText("hidden")).toBeInTheDocument();
34
+ await act(() => vi.advanceTimersByTime(1));
35
+ await expect.element(screen.getByText("45%")).toBeInTheDocument();
36
+ await screen.rerender(<Probe upload={uploading(0.7)} />);
37
+ await expect.element(screen.getByText("70%")).toBeInTheDocument();
38
+ await screen.rerender(<Probe upload={ready} />);
39
+ await expect.element(screen.getByText("hidden")).toBeInTheDocument();
40
+
41
+ await screen.rerender(<Probe upload={uploading(0)} />);
42
+ await expect.element(screen.getByText("hidden")).toBeInTheDocument();
43
+ await act(() => vi.advanceTimersByTime(150));
44
+ await expect.element(screen.getByText("0%")).toBeInTheDocument();
45
+ await screen.rerender(<Probe id="another-image" upload={uploading(0)} />);
46
+ await expect.element(screen.getByText("hidden")).toBeInTheDocument();
47
+});
apps/web/src/hooks/useAttachmentUploadProgress.ts
new
+18
@@ -0,0 +1,18 @@
1
+import {
2
+ type AttachmentUploadState,
3
+ formatAttachmentUploadProgress,
4
+} from "../lib/attachmentUploadState";
5
+import { useDelayedStatus } from "./useDelayedStatus";
6
+
7
+export function useAttachmentUploadProgress(
8
+ key: string,
9
+ upload: AttachmentUploadState | undefined,
10
+) {
11
+ const uploading = upload?.status === "uploading";
12
+ const shown = useDelayedStatus(
13
+ `${key}:${upload?.environmentId ?? ""}:${uploading ? "uploading" : "idle"}`,
14
+ uploading ? true : null,
15
+ 150,
16
+ );
17
+ return uploading && shown ? formatAttachmentUploadProgress(upload.progress) : null;
18
+}
apps/web/src/hooks/useDelayedStatus.ts
+3
-3
@@ -7,12 +7,12 @@ import { useState } from "react";
7
8
import { useDeadlinePassed } from "./useDeadline";
9
10
-export function useDelayedStatus<A>(key: string, value: A | null): A | null {
10
+export function useDelayedStatus<A>(key: string, value: A | null, showDelayMs?: number): A | null {
11
const [state, setState] = useState(() =>
12
- stepDelayedStatus<A>(null, { key, value, deadlinePassed: false }, Date.now),
12
+ stepDelayedStatus<A>(null, { key, value, deadlinePassed: false, showDelayMs }, Date.now),
13
);
14
const deadlinePassed = useDeadlinePassed(delayedStatusDeadline(state));
15
- const next = stepDelayedStatus(state, { key, value, deadlinePassed }, Date.now);
15
+ const next = stepDelayedStatus(state, { key, value, deadlinePassed, showDelayMs }, Date.now);
16
if (next !== state) setState(next);
17
return shownDelayedStatus(next, key);
18
}
apps/web/src/lib/imageCompression.test.ts
+55
-15
@@ -96,6 +96,7 @@ function stubCanvasPipeline(
96
const supportsWebp = options?.supportsWebp ?? true;
97
const close = vi.fn();
98
const fillRect = vi.fn();
99
+ const canvasSizes: Array<{ width: number; height: number }> = [];
100
vi.stubGlobal(
101
"createImageBitmap",
102
vi.fn(async () => ({ width: 4000, height: 3000, close })),
@@ -106,7 +107,9 @@ function stubCanvasPipeline(
107
constructor(
108
public width: number,
109
public height: number,
109
- ) {}
110
+ ) {
111
+ canvasSizes.push({ width, height });
112
+ }
113
getContext() {
114
return {
115
fillStyle: "",
@@ -131,7 +134,7 @@ function stubCanvasPipeline(
134
vi.stubGlobal("HTMLCanvasElement", HtmlCanvas);
135
vi.stubGlobal("document", { createElement: () => new HtmlCanvas(0, 0) });
136
}
134
- return { close, fillRect };
137
+ return { close, fillRect, canvasSizes };
138
}
139
140
afterEach(() => {
@@ -263,22 +266,57 @@ describe("composer image thumbnails", () => {
266
});
267
268
describe("HEIC attachment preparation", () => {
269
+ it("shares the native decode and shows its thumbnail before JPEG encoding finishes", async () => {
270
+ const { close } = stubCanvasPipeline(() => 3);
271
+ let finishEncoding!: (blob: Blob) => void;
272
+ const jpeg = new Promise<Blob>((resolve) => {
273
+ finishEncoding = resolve;
274
+ });
275
+ const Canvas = globalThis.OffscreenCanvas;
276
+ vi.stubGlobal(
277
+ "OffscreenCanvas",
278
+ class extends Canvas {
279
+ override convertToBlob(options?: ImageEncodeOptions) {
280
+ return options?.type === "image/jpeg" ? jpeg : super.convertToBlob(options);
281
+ }
282
+ },
283
+ );
284
+ const original = makeHeicFile();
285
+ const preparation = prepareImageForAttachment(original, 1024);
286
+ const queued = prepareImageForAttachment(makeHeicFile({ name: "next.heic" }), 1024);
287
+ const results = Promise.all([preparation, queued]);
288
+ try {
289
+ const thumbnail = await createComposerImageThumbnail(original);
290
+ expect(thumbnail).toMatch(/^data:image\/png;base64,/);
291
+ expect(mocks.heicTo).not.toHaveBeenCalled();
292
+ expect(createImageBitmap).toHaveBeenCalledTimes(1);
293
+ expect(close).not.toHaveBeenCalled();
294
+ } finally {
295
+ finishEncoding(new Blob([new Uint8Array(4)], { type: "image/jpeg" }));
296
+ await results;
297
+ }
298
+ const [result, next] = await results;
299
+ expect(result.ok && result.file.type).toBe("image/jpeg");
300
+ expect(result.ok && result.file.size).toBe(4);
301
+ expect(next.ok).toBe(true);
302
+ expect(close).toHaveBeenCalledTimes(2);
303
+ });
304
+
305
it("converts a HEIC photo with a missing MIME type into a named JPEG", async () => {
306
const original = makeHeicFile({
307
name: "IMG_1234.HEIC",
308
type: "",
309
lastModified: 123,
310
});
272
- mocks.heicTo.mockResolvedValueOnce(
273
- new Blob([new Uint8Array([4, 5, 6, 7])], { type: "image/jpeg" }),
274
- );
311
+ const { close } = stubCanvasPipeline(() => 4);
312
+ vi.mocked(createImageBitmap).mockRejectedValue(new Error("HEIC is not supported natively"));
313
+ mocks.heicTo.mockResolvedValueOnce({ width: 4000, height: 3000, close });
314
315
const result = await prepareImageForAttachment(original, 1024);
316
317
expect(mocks.heicTo).toHaveBeenCalledWith({
318
blob: original,
280
- type: "image/jpeg",
281
- quality: 0.92,
319
+ type: "bitmap",
320
});
321
expect(result.ok && result.file.name).toBe("IMG_1234.jpg");
322
expect(result.ok && result.file.type).toBe("image/jpeg");
@@ -287,24 +325,26 @@ describe("HEIC attachment preparation", () => {
325
expect(result.ok && result.recompressed).toBe(true);
326
});
327
290
- it("compresses JPEG intermediates above the source safety ceiling", async () => {
328
+ it("resizes decoded HEIC pixels before encoding the JPEG", async () => {
329
const original = makeHeicFile({
330
name: "large.heic",
331
type: "image/heic",
332
});
295
- mocks.heicTo.mockResolvedValueOnce(
296
- new Blob([new Uint8Array(MAX_COMPRESSIBLE_SOURCE_BYTES + 1)], {
297
- type: "image/jpeg",
298
- }),
299
- );
300
- const { close } = stubCanvasPipeline(() => 200_000);
333
+ const { close, canvasSizes } = stubCanvasPipeline(() => 200_000);
334
+ vi.mocked(createImageBitmap).mockRejectedValue(new Error("HEIC is not supported natively"));
335
+ mocks.heicTo.mockResolvedValueOnce({ width: 4000, height: 3000, close });
336
337
const result = await prepareImageForAttachment(original, 1_000_000);
338
339
expect(result.ok && result.file.name).toBe("large.jpg");
340
expect(result.ok && result.file.type).toBe("image/jpeg");
341
expect(result.ok && result.file.size).toBeLessThanOrEqual(1_000_000);
307
- expect(close).toHaveBeenCalled();
342
+ expect(close).toHaveBeenCalledTimes(1);
343
+ expect(createImageBitmap).toHaveBeenCalledTimes(1);
344
+ expect(canvasSizes).toEqual([
345
+ { width: 2000, height: 1500 },
346
+ { width: 256, height: 256 },
347
+ ]);
348
});
349
350
it("rejects oversized HEIC dimensions before loading the decoder", async () => {
apps/web/src/lib/imageCompression.ts
+80
-16
@@ -240,11 +240,22 @@ export function inheritComposerImageThumbnail(source: File, target: File): void
240
}
241
242
async function renderComposerImageThumbnail(file: File): Promise<string | null> {
243
+ if (isHeicImageFile(file)) return prepareHeicImage(file).thumbnail;
244
// Placeholders thumbnail the uncompressed source, which can be big enough to OOM the decode.
245
if (!canRecompress() || file.size > MAX_COMPRESSIBLE_SOURCE_BYTES) return null;
246
let bitmap: ImageBitmap | undefined;
247
try {
248
bitmap = await createImageBitmap(file);
249
+ return await renderComposerImageThumbnailFromBitmap(bitmap);
250
+ } catch {
251
+ return null;
252
+ } finally {
253
+ bitmap?.close();
254
+ }
255
+}
256
+
257
+async function renderComposerImageThumbnailFromBitmap(bitmap: ImageBitmap): Promise<string | null> {
258
+ try {
259
const side = Math.min(bitmap.width, bitmap.height);
260
if (side <= 0) return null;
261
const dimension = Math.min(256, side);
@@ -265,8 +276,6 @@ async function renderComposerImageThumbnail(file: File): Promise<string | null>
276
return blob ? await readFileAsDataUrl(blob) : null;
277
} catch {
278
return null;
268
- } finally {
269
- bitmap?.close();
279
}
280
}
281
@@ -454,35 +463,90 @@ export async function compressImageToByteLimit(
463
};
464
}
465
457
-export async function prepareImageForAttachment(
466
+const heicPreparations = new WeakMap<
467
+ File,
468
+ { image: Promise<CompressImageFileResult>; thumbnail: Promise<string | null> }
469
+>();
470
+// Preparing tiles share full-size decodes, so keep only one HEIC bitmap alive at a time.
471
+let heicPreparationQueue = Promise.resolve();
472
+
473
+function prepareHeicImage(file: File) {
474
+ const cached = heicPreparations.get(file);
475
+ if (cached) return cached;
476
+ let resolveThumbnail!: (src: string | null) => void;
477
+ const thumbnail = new Promise<string | null>((resolve) => {
478
+ resolveThumbnail = resolve;
479
+ });
480
+ const image = heicPreparationQueue
481
+ .then(() => convertHeicImage(file, resolveThumbnail))
482
+ .finally(() => resolveThumbnail(null));
483
+ heicPreparationQueue = image.then(
484
+ () => undefined,
485
+ () => undefined,
486
+ );
487
+ const preparation = { image, thumbnail };
488
+ heicPreparations.set(file, preparation);
489
+ return preparation;
490
+}
491
+
492
+async function convertHeicImage(
493
file: File,
459
- maxBytes: number,
494
+ onThumbnail: (src: string | null) => void,
495
): Promise<CompressImageFileResult> {
461
- if (!isHeicImageFile(file)) {
462
- return compressImageToByteLimit(file, maxBytes);
463
- }
464
-
496
if (file.size > MAX_COMPRESSIBLE_SOURCE_BYTES) {
497
return { ok: false, reason: "too-large" };
498
}
499
469
- let converted: Blob;
500
+ let bitmap: ImageBitmap | undefined;
501
try {
502
const dimensionError = await validateHeicImageDimensions(file);
503
if (dimensionError) {
504
return { ok: false, reason: dimensionError };
505
}
475
- const { heicTo } = await import("heic-to/csp");
476
- converted = await heicTo({ blob: file, type: "image/jpeg", quality: QUALITY_STEPS[0] });
506
+ bitmap =
507
+ typeof createImageBitmap === "function"
508
+ ? await createImageBitmap(file).catch(() => undefined)
509
+ : undefined;
510
+ if (!bitmap) {
511
+ const { heicTo } = await import("heic-to/csp");
512
+ bitmap = await heicTo({ blob: file, type: "bitmap" });
513
+ }
514
+ const imageSize = fitImageAttachmentSize(bitmap) ?? {
515
+ width: bitmap.width,
516
+ height: bitmap.height,
517
+ };
518
+ const surface = createCanvas(imageSize.width, imageSize.height);
519
+ if (!surface) return { ok: false, reason: "unreadable" };
520
+ surface.context.drawImage(bitmap, 0, 0, imageSize.width, imageSize.height);
521
+ const [converted] = await Promise.all([
522
+ encodeCanvas(surface.canvas, QUALITY_STEPS[0], "image/jpeg"),
523
+ renderComposerImageThumbnailFromBitmap(bitmap).then(onThumbnail),
524
+ ]);
525
+ if (!converted) return { ok: false, reason: "unreadable" };
526
+ return {
527
+ ok: true,
528
+ file: new File([converted], fileNameForMimeType(file.name || "image", "image/jpeg"), {
529
+ type: "image/jpeg",
530
+ lastModified: file.lastModified,
531
+ }),
532
+ recompressed: true,
533
+ imageSize,
534
+ };
535
} catch {
536
return { ok: false, reason: "unreadable" };
537
+ } finally {
538
+ bitmap?.close();
539
}
540
+}
541
481
- const jpeg = new File([converted], fileNameForMimeType(file.name || "image", "image/jpeg"), {
482
- type: "image/jpeg",
483
- lastModified: file.lastModified,
484
- });
485
- const result = await compressImageToByteLimit(jpeg, maxBytes, {
542
+export async function prepareImageForAttachment(
543
+ file: File,
544
+ maxBytes: number,
545
+): Promise<CompressImageFileResult> {
546
+ if (!isHeicImageFile(file)) return compressImageToByteLimit(file, maxBytes);
547
+ const prepared = await prepareHeicImage(file).image;
548
+ if (!prepared.ok || prepared.file.size <= maxBytes) return prepared;
549
+ const result = await compressImageToByteLimit(prepared.file, maxBytes, {
550
preferredMimeType: "image/jpeg",
551
sourceSizeBytes: file.size,
552
});