@devries-cameron20 / t3code-1 / commits / 4dcc244fe1
Fork 0

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

fix(web): pasted images show their upload tile instantly (#23832)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Ben Davis committed Oct 23, 2026 at 10:54 UTC 4dcc244fe17ce448d193e95be43e78658dd78bc9
8 files changed +307 -138
apps/web/src/components/chat/ChatComposer.tsx
+12 -7
@@ -290,6 +290,10 @@ import { useComposerImageGroups } from "./useComposerImageGroups";
290 import { useComposerStash } from "./useComposerStash";
291 import { useComposerContextImport } from "./useComposerContextImport";
292 import { useComposerAttachmentIntake } from "./useComposerAttachmentIntake";
293 +import {
294 + countPreparingComposerImages,
295 + usePreparingComposerImages,
296 +} from "./preparingComposerImages";
297 import { ComposerControlsBar } from "./ComposerControlsBar";
298 import { useComposerCommandKey } from "./useComposerCommandKey";
299
@@ -746,6 +750,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
750 uncommittedSnapShotIds,
751 standaloneComposerImages,
752 } = useComposerImageGroups(attachmentDraftTarget, composerImages, composerPreviewAnnotations);
753 + const preparingComposerImages = usePreparingComposerImages(attachmentTargetKey);
754 const nonPersistedComposerImageIds = attachmentDraft.nonPersistedImageIds;
755 const uploadsByImageId = useAttachmentUploadStore((state) => state.uploadsByImageId);
756 const openPrLink = useOpenPrLink(routeThreadRef);
@@ -1124,7 +1129,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
1129 const composerScrollCollapseEligibleRef = useRef(false);
1130 const windowRefocusInFlightRef = useRef(false);
1131 const composerScrollGestureRef = useRef(createComposerScrollGestureState());
1127 - const pendingImageCompressionsRef = useRef<Map<string, number>>(new Map());
1132 const pendingThreadImportsRef = useRef<Map<string, number>>(new Map());
1133 const isRevertingCheckpointRef = useRef(isRevertingCheckpoint);
1134 useLayoutEffect(() => {
@@ -1434,7 +1438,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
1438 composerContextRecords,
1439 composerImages,
1440 composerFilesRef,
1437 - pendingImageCompressionsRef,
1441 pendingThreadImportsRef,
1442 addComposerFilesToDraft,
1443 });
@@ -2123,7 +2126,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
2126 // A send while a pasted image is still compressing would strand that image:
2127 // the turn snapshot wouldn't include it, and it would surface in the *next*
2128 // draft instead.
2126 - if (activeThreadId && (pendingImageCompressionsRef.current.get(attachmentTargetKey) ?? 0) > 0) {
2129 + if (activeThreadId && countPreparingComposerImages(attachmentTargetKey) > 0) {
2130 event?.preventDefault();
2131 toastManager.add({
2132 type: "info",
@@ -2361,7 +2364,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
2364 environmentUnavailable !== null ||
2365 composerSubmissionError !== null ||
2366 providerInputSubmissionError !== null ||
2364 - hasImageAttachmentAttention;
2367 + hasImageAttachmentAttention ||
2368 + preparingComposerImages.length > 0;
2369 const isComposerResting = shouldUseRestingComposerLayout({
2370 isExistingThread: routeKind === "server" && activeThreadId !== null,
2371 isMobileViewport,
@@ -2734,7 +2738,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
2738 composerFilesRef,
2739 composerEditorRef,
2740 isRevertingCheckpointRef,
2737 - pendingImageCompressionsRef,
2741 setThreadError,
2742 countReservedAttachments,
2743 addComposerImage,
@@ -3103,7 +3106,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
3106 },
3107 addDroppedFolders: addComposerFoldersStable,
3108 hasPendingAttachments: () =>
3106 - (pendingImageCompressionsRef.current.get(attachmentTargetKey) ?? 0) > 0 ||
3109 + countPreparingComposerImages(attachmentTargetKey) > 0 ||
3110 pendingDraftWork.has(attachmentTargetKey),
3111 insertTextAtEnd: insertComposerTextAtEndStable,
3112 pasteTextAtEnd: (text: string, options) => {
@@ -3367,10 +3370,12 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
3370 !isComposerCollapsedMobile &&
3371 (uncommittedSnapShotIds.length > 0 ||
3372 composerVideos.length > 0 ||
3370 - expandedComposerImages.length > 0) ? (
3373 + expandedComposerImages.length > 0 ||
3374 + preparingComposerImages.length > 0) ? (
3375 <ComposerAttachmentMediaRow
3376 images={composerImages}
3377 expandedImages={expandedComposerImages}
3378 + preparingImages={preparingComposerImages}
3379 videos={composerVideos}
3380 pendingSnapShotAnimations={pendingSnapShotAnimations}
3381 pendingSnapShotIds={pendingSnapShotIds}
apps/web/src/components/chat/ComposerAttachmentStrip.tsx
+33 -21
@@ -34,6 +34,7 @@ import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog";
34 import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
35 import { ComposerImageThumbnail } from "./ComposerImageThumbnail";
36 import { composerFloatingLayerProps } from "./composerEventScope";
37 +import type { PreparingComposerImage } from "./preparingComposerImages";
38 import {
39 attachVideoThumbnail,
40 buildExpandedImagePreview,
@@ -109,6 +110,7 @@ function SnapShotAttachmentFrame({
110 export function ComposerAttachmentMediaRow({
111 images,
112 expandedImages,
113 + preparingImages,
114 videos,
115 pendingSnapShotAnimations,
116 pendingSnapShotIds,
@@ -127,6 +129,7 @@ export function ComposerAttachmentMediaRow({
129 }: {
130 readonly images: ReadonlyArray<ComposerImageAttachment>;
131 readonly expandedImages: ReadonlyArray<ComposerImageAttachment>;
132 + readonly preparingImages: ReadonlyArray<PreparingComposerImage>;
133 readonly videos: ReadonlyArray<ComposerFileAttachment>;
134 readonly pendingSnapShotAnimations: ReadonlyArray<PendingSnapShotAnimation>;
135 readonly pendingSnapShotIds: ReadonlyArray<string>;
@@ -156,14 +159,20 @@ export function ComposerAttachmentMediaRow({
159 : "flex-wrap",
160 )}
161 >
159 - {expandedImages
160 - .map((image) => {
161 - const upload = supportsAttachmentUploads ? uploadsByImageId[image.id] : undefined;
162 + {/* A preparing image renders through the same tile, keyed by the id its compressed image
163 + takes, so the thumbnail element survives the swap. */}
164 + {[
165 + ...expandedImages.map((image) => ({ image, ready: image })),
166 + ...preparingImages.map((image) => ({ image, ready: null })),
167 + ]
168 + .map(({ image, ready }) => {
169 + const upload =
170 + ready && supportsAttachmentUploads ? uploadsByImageId[image.id] : undefined;
171 + const source = ready?.source;
172 const snapShotAnimationPending =
163 - image.source?.kind === "snap-shot" && pendingSnapShotIdSet.has(image.id);
173 + source?.kind === "snap-shot" && pendingSnapShotIdSet.has(image.id);
174 const snapShotArrival =
165 - image.source?.kind === "snap-shot" &&
166 - shouldAnimateSnapShotArrival(image.source.capturedAt);
175 + source?.kind === "snap-shot" && shouldAnimateSnapShotArrival(source.capturedAt);
176 return (
177 <SnapShotAttachmentFrame
178 key={image.id}
@@ -173,20 +182,21 @@ export function ComposerAttachmentMediaRow({
182 arrival={snapShotArrival}
183 animateArrival={snapShotAnimations && !snapShotAnimationPending && snapShotArrival}
184 animationId={snapShotAnimationPending ? image.id : undefined}
176 - animationSource={snapShotAnimationPending ? image.source : undefined}
185 + animationSource={snapShotAnimationPending ? source : undefined}
186 className={cn(
187 "group/attachment shrink-0 snap-start bg-background",
179 - image.source?.kind === "snap-shot"
188 + source?.kind === "snap-shot"
189 ? SNAP_SHOT_ATTACHMENT_FRAME_CLASS
190 : "relative h-16 w-16 overflow-hidden rounded-lg border border-border/80",
191 snapShotAnimationPending && "invisible",
192 )}
193 >
185 - {image.previewUrl ? (
194 + {!ready || ready.previewUrl ? (
195 <button
196 type="button"
188 - className="h-full w-full cursor-zoom-in"
197 + className={cn("h-full w-full", ready && "cursor-zoom-in")}
198 aria-label={`Preview ${image.name}`}
199 + disabled={!ready}
200 onClick={() => {
201 const preview = buildExpandedImagePreview(images, image.id);
202 if (!preview) return;
@@ -198,11 +208,11 @@ export function ComposerAttachmentMediaRow({
208 alt={image.name}
209 className="h-full w-full object-cover"
210 fallback={
201 - isHeicImageFile({ name: image.name, type: image.mimeType }) ? (
211 + ready && isHeicImageFile({ name: ready.name, type: ready.mimeType }) ? (
212 <AttachmentImage
203 - name={image.name}
204 - mimeType={image.mimeType}
205 - src={image.previewUrl}
213 + name={ready.name}
214 + mimeType={ready.mimeType}
215 + src={ready.previewUrl}
216 alt={image.name}
217 className="h-full w-full object-cover"
218 />
@@ -219,9 +229,9 @@ export function ComposerAttachmentMediaRow({
229 {image.name}
230 </div>
231 )}
222 - {image.source?.kind === "snap-shot" ? (
232 + {source?.kind === "snap-shot" ? (
233 <SnapShotAttachmentDetails
224 - source={image.source}
234 + source={source}
235 className={cn(
236 upload?.status === "uploading" && "bottom-4",
237 upload?.status === "failed" && "bottom-8",
@@ -246,12 +256,14 @@ export function ComposerAttachmentMediaRow({
256 </TooltipPopup>
257 </Tooltip>
258 )}
249 - {upload?.status === "uploading" && (
259 + {(ready ? upload?.status === "uploading" : supportsAttachmentUploads) && (
260 <span className="pointer-events-none absolute inset-x-0 bottom-0 bg-background/85 px-1 text-center text-3xs text-foreground">
251 - {formatAttachmentUploadProgress(upload.progress)}
261 + {formatAttachmentUploadProgress(
262 + upload?.status === "uploading" ? upload.progress : 0,
263 + )}
264 </span>
265 )}
254 - {upload?.status === "failed" && (
266 + {ready && upload?.status === "failed" && (
267 <Tooltip>
268 <TooltipTrigger
269 render={
@@ -262,7 +274,7 @@ export function ComposerAttachmentMediaRow({
274 onClick={() =>
275 retryAttachmentUpload({
276 environmentId,
265 - image,
277 + image: ready,
278 draftTarget: attachmentDraftTarget,
279 })
280 }
@@ -279,7 +291,7 @@ export function ComposerAttachmentMediaRow({
291 <span
292 className={cn(
293 "absolute right-1 top-1 flex",
282 - image.source?.kind === "snap-shot" &&
294 + source?.kind === "snap-shot" &&
295 "opacity-0 transition-opacity pointer-coarse:opacity-100 group-hover/attachment:opacity-100 group-focus-within/attachment:opacity-100",
296 )}
297 >
apps/web/src/components/chat/ComposerImageThumbnail.tsx
+5 -2
@@ -1,6 +1,9 @@
1 import { memo, useEffect, useState, type ReactNode } from "react";
2
3 -import { createComposerImageThumbnail } from "../../lib/imageCompression";
3 +import {
4 + createComposerImageThumbnail,
5 + readComposerImageThumbnail,
6 +} from "../../lib/imageCompression";
7
8 /** Keep full-resolution image decoding out of composer rerenders. */
9 export const ComposerImageThumbnail = memo(function ComposerImageThumbnail({
@@ -24,6 +27,6 @@ export const ComposerImageThumbnail = memo(function ComposerImageThumbnail({
27 active = false;
28 };
29 }, [file]);
27 - const src = preview?.file === file ? preview.src : null;
30 + const src = preview?.file === file ? preview.src : (readComposerImageThumbnail(file) ?? null);
31 return src ? <img src={src} alt={alt} className={className} /> : fallback;
32 });
apps/web/src/components/chat/preparingComposerImages.ts new
+75
@@ -0,0 +1,75 @@
1 +import { create } from "zustand";
2 +
3 +export interface PreparingComposerImage {
4 + readonly id: string;
5 + readonly name: string;
6 + /** The uncompressed source, thumbnailed so the placeholder shows the image itself. */
7 + readonly file: File;
8 +}
9 +
10 +const NO_PREPARING_IMAGES: ReadonlyArray<PreparingComposerImage> = [];
11 +
12 +/**
13 + * Images accepted into a draft but still being compressed, keyed by attachment target. Large
14 + * screenshots take a noticeable moment to re-encode, so the strip shows these right away and
15 + * they hold their attachment slots until the real image joins the draft.
16 + */
17 +const usePreparingComposerImageStore = create<{
18 + readonly byTargetKey: Readonly<Record<string, ReadonlyArray<PreparingComposerImage>>>;
19 +}>(() => ({ byTargetKey: {} }));
20 +
21 +export function usePreparingComposerImages(targetKey: string) {
22 + return usePreparingComposerImageStore(
23 + (state) => state.byTargetKey[targetKey] ?? NO_PREPARING_IMAGES,
24 + );
25 +}
26 +
27 +export function countPreparingComposerImages(targetKey: string): number {
28 + return usePreparingComposerImageStore.getState().byTargetKey[targetKey]?.length ?? 0;
29 +}
30 +
31 +export function addPreparingComposerImages(
32 + targetKey: string,
33 + images: ReadonlyArray<PreparingComposerImage>,
34 +): void {
35 + usePreparingComposerImageStore.setState((state) => ({
36 + byTargetKey: {
37 + ...state.byTargetKey,
38 + [targetKey]: [...(state.byTargetKey[targetKey] ?? NO_PREPARING_IMAGES), ...images],
39 + },
40 + }));
41 +}
42 +
43 +export function isPreparingComposerImage(targetKey: string, imageId: string): boolean {
44 + return (
45 + usePreparingComposerImageStore
46 + .getState()
47 + .byTargetKey[targetKey]?.some((image) => image.id === imageId) ?? false
48 + );
49 +}
50 +
51 +/** Returns the ids that were still preparing; any other id was cancelled from the strip. */
52 +export function removePreparingComposerImages(
53 + targetKey: string,
54 + imageIds: ReadonlySet<string>,
55 +): ReadonlySet<string> {
56 + const current =
57 + usePreparingComposerImageStore.getState().byTargetKey[targetKey] ?? NO_PREPARING_IMAGES;
58 + const removed = new Set(
59 + current.filter((image) => imageIds.has(image.id)).map((image) => image.id),
60 + );
61 + if (removed.size === 0) return removed;
62 + usePreparingComposerImageStore.setState((state) => {
63 + const remaining = (state.byTargetKey[targetKey] ?? NO_PREPARING_IMAGES).filter(
64 + (image) => !removed.has(image.id),
65 + );
66 + const byTargetKey = { ...state.byTargetKey };
67 + if (remaining.length > 0) {
68 + byTargetKey[targetKey] = remaining;
69 + } else {
70 + delete byTargetKey[targetKey];
71 + }
72 + return { byTargetKey };
73 + });
74 + return removed;
75 +}
apps/web/src/components/chat/useComposerAttachmentIntake.ts
+97 -73
@@ -38,11 +38,19 @@ import {
38 pastedTextDisposition,
39 wouldTextPasteExceedLimit,
40 } from "@t3tools/client-runtime/text-paste";
41 -import { prepareImageForAttachment } from "../../lib/imageCompression";
41 +import {
42 + inheritComposerImageThumbnail,
43 + prepareImageForAttachment,
44 +} from "../../lib/imageCompression";
45 import { requestConfirmDialog } from "~/confirmDialog";
46 import { toastManager } from "../ui/toast";
47 import { type RefObject, useRef } from "react";
48 import { videoMimeType } from "../../types";
49 +import {
50 + addPreparingComposerImages,
51 + isPreparingComposerImage,
52 + removePreparingComposerImages,
53 +} from "./preparingComposerImages";
54
55 /**
56 * Takes files into the draft: validates and reserves slots, compresses images, inserts
@@ -65,7 +73,6 @@ export function useComposerAttachmentIntake(input: {
73 readonly composerFilesRef: ChatComposerProps["composerFilesRef"];
74 readonly composerEditorRef: RefObject<ComposerPromptEditorHandle | null>;
75 readonly isRevertingCheckpointRef: RefObject<boolean>;
68 - readonly pendingImageCompressionsRef: RefObject<Map<string, number>>;
76 readonly setThreadError: ChatComposerProps["setThreadError"];
77 readonly countReservedAttachments: () => number;
78 readonly addComposerImage: (image: ComposerImageAttachment) => string[];
@@ -94,7 +101,6 @@ export function useComposerAttachmentIntake(input: {
101 composerFilesRef,
102 composerEditorRef,
103 isRevertingCheckpointRef,
97 - pendingImageCompressionsRef,
104 setThreadError,
105 countReservedAttachments,
106 addComposerImage,
@@ -142,9 +148,8 @@ export function useComposerAttachmentIntake(input: {
148 stripInlineContextReferences(promptRef.current).trim().length > 0);
149
150 // Validation happens synchronously so concurrent pastes see each other:
145 - // accepted files reserve their attachment slots (via the pending counter)
151 + // accepted files reserve their attachment slots (as preparing images)
152 // before the first await, keeping the total under the limit.
147 - const pendingCount = pendingImageCompressionsRef.current.get(attachmentTargetKey) ?? 0;
153 let reservedCount = countReservedAttachments();
154 // A pick that matches a needs-reattach marker replaces it in the draft, so
155 // it must not consume a slot; a draft full of markers would otherwise hit
@@ -242,81 +247,100 @@ export function useComposerAttachmentIntake(input: {
247 }
248 if (acceptedImages.length === 0) return insertedAny;
249
245 - pendingImageCompressionsRef.current.set(
246 - attachmentTargetKey,
247 - pendingCount + acceptedImages.length,
248 - );
250 + // The draft image reuses its placeholder's id and thumbnail so the strip tile stays exactly
251 + // as it is when the compressed file replaces it.
252 + const preparingImages = acceptedImages.map((file) => ({
253 + id: randomUUID(),
254 + name: file.name || "image",
255 + file,
256 + }));
257 + addPreparingComposerImages(attachmentTargetKey, preparingImages);
258 if (questionAttachmentTarget)
259 changeQuestionAttachmentPreparation(questionAttachmentTarget, acceptedImages.length);
251 - const compressAndStoreImages = async (): Promise<boolean> => {
252 - const nextImages: ComposerImageAttachment[] = [];
253 - let compressionError: string | null = null;
254 - for (const file of acceptedImages) {
255 - const compressed = await prepareImageForAttachment(
256 - file,
257 - PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
260 + const compressed: { id: string; file: File }[] = [];
261 + // Keyed by image so a failure whose tile was removed (even mid-compression) is not reported.
262 + const compressionErrors = new Map<string, string>();
263 + for (const { id, file } of preparingImages) {
264 + // Removed from the strip while an earlier image was compressing.
265 + if (!isPreparingComposerImage(attachmentTargetKey, id)) continue;
266 + // A rejection must still reach the cleanup below: a leaked
267 + // placeholder would block sending from this draft for good.
268 + const result = await prepareImageForAttachment(
269 + file,
270 + PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
271 + ).catch(() => ({ ok: false, reason: "unreadable" }) as const);
272 + if (!result.ok) {
273 + compressionErrors.set(
274 + id,
275 + result.reason === "unreadable"
276 + ? `'${file.name}' could not be read as an image.`
277 + : `'${file.name}' is too large to attach, even after compression.`,
278 );
259 - if (!compressed.ok) {
260 - compressionError =
261 - compressed.reason === "unreadable"
262 - ? `'${file.name}' could not be read as an image.`
263 - : `'${file.name}' is too large to attach, even after compression.`;
264 - continue;
265 - }
266 - const attachmentFile = compressed.file;
267 - const previewUrl = URL.createObjectURL(attachmentFile);
268 - nextImages.push({
269 - type: "image",
270 - id: randomUUID(),
271 - name: attachmentFile.name || "image",
272 - mimeType: attachmentFile.type,
273 - sizeBytes: attachmentFile.size,
274 - previewUrl,
275 - file: attachmentFile,
276 - });
277 - }
278 - if (
279 - questionAttachmentTarget &&
280 - !useQuestionAttachmentPreparation.getState().counts[questionAttachmentTarget]
281 - ) {
282 - for (const image of nextImages) URL.revokeObjectURL(image.previewUrl);
283 - return false;
284 - }
285 - const storedImageIds = new Set(
286 - nextImages.length === 1 && nextImages[0]
287 - ? addComposerImage(nextImages[0])
288 - : nextImages.length > 1
289 - ? addComposerImagesToDraft(nextImages)
290 - : [],
291 - );
292 - const storedImages = nextImages.filter((image) => storedImageIds.has(image.id));
293 - if (storedImages.length > 0 && imageAttachmentsGetChips) {
294 - insertedAny =
295 - insertAttachmentReferences(storedImages.map(imageContextReference)) || insertedAny;
296 - }
297 - // Only failures are reported here. Success must not pass `null`: by
298 - // now other work (a failed send, an overlapping paste) may have set a
299 - // thread error this call knows nothing about, and clearing it would
300 - // swallow that message.
301 - if (compressionError !== null) {
302 - setThreadError(threadId, compressionError);
303 - }
304 - return insertedAny;
305 - };
306 - return await compressAndStoreImages().finally(() => {
307 - if (questionAttachmentTarget)
308 - changeQuestionAttachmentPreparation(questionAttachmentTarget, -acceptedImages.length);
309 - const remaining =
310 - (pendingImageCompressionsRef.current.get(attachmentTargetKey) ?? 0) - acceptedImages.length;
311 - if (remaining > 0) {
312 - pendingImageCompressionsRef.current.set(attachmentTargetKey, remaining);
313 - } else {
314 - pendingImageCompressionsRef.current.delete(attachmentTargetKey);
279 + continue;
280 }
316 - });
281 + inheritComposerImageThumbnail(file, result.file);
282 + compressed.push({ id, file: result.file });
283 + }
284 + const questionDiscarded =
285 + questionAttachmentTarget !== null &&
286 + !useQuestionAttachmentPreparation.getState().counts[questionAttachmentTarget];
287 + // Same tick as the draft insert below, so the strip swaps placeholders for
288 + // real tiles in one render. A placeholder removed meanwhile stays removed.
289 + const stillPreparingIds = removePreparingComposerImages(
290 + attachmentTargetKey,
291 + new Set(preparingImages.map((image) => image.id)),
292 + );
293 + if (questionAttachmentTarget)
294 + changeQuestionAttachmentPreparation(questionAttachmentTarget, -stillPreparingIds.size);
295 + if (questionDiscarded) return false;
296 + const nextImages: ComposerImageAttachment[] = compressed
297 + .filter(({ id }) => stillPreparingIds.has(id))
298 + .map(({ id, file }) => ({
299 + type: "image",
300 + id,
301 + name: file.name || "image",
302 + mimeType: file.type,
303 + sizeBytes: file.size,
304 + previewUrl: URL.createObjectURL(file),
305 + file,
306 + }));
307 + const storedImageIds = new Set(
308 + nextImages.length === 1 && nextImages[0]
309 + ? addComposerImage(nextImages[0])
310 + : nextImages.length > 1
311 + ? addComposerImagesToDraft(nextImages)
312 + : [],
313 + );
314 + const storedImages = nextImages.filter((image) => storedImageIds.has(image.id));
315 + // The draft may be gone by now; revoking a URL the store already revoked is harmless.
316 + for (const image of nextImages) {
317 + if (!storedImageIds.has(image.id)) URL.revokeObjectURL(image.previewUrl);
318 + }
319 + if (storedImages.length > 0 && imageAttachmentsGetChips) {
320 + insertedAny =
321 + insertAttachmentReferences(storedImages.map(imageContextReference)) || insertedAny;
322 + }
323 + // Only failures are reported here. Success must not pass `null`: by
324 + // now other work (a failed send, an overlapping paste) may have set a
325 + // thread error this call knows nothing about, and clearing it would
326 + // swallow that message.
327 + const compressionError = [...compressionErrors]
328 + .filter(([id]) => stillPreparingIds.has(id))
329 + .map(([, message]) => message)
330 + .at(-1);
331 + if (compressionError !== undefined) {
332 + setThreadError(threadId, compressionError);
333 + }
334 + return insertedAny;
335 };
336
337 const removeComposerImage = (imageId: string) => {
338 + if (removePreparingComposerImages(attachmentTargetKey, new Set([imageId])).size > 0) {
339 + // Its compression finishes in the background and is dropped.
340 + if (questionAttachmentTarget)
341 + changeQuestionAttachmentPreparation(questionAttachmentTarget, -1);
342 + return;
343 + }
344 const image = composerImagesRef.current.find((candidate) => candidate.id === imageId);
345 const referenced = collectInlineContextIds(promptRef.current).includes(
346 image ? imageContextReference(image).contextId : "",
apps/web/src/components/chat/useComposerContextImport.ts
+2 -3
@@ -47,6 +47,7 @@ import {
47 import { useAtomCommand } from "../../state/use-atom-command";
48 import { useAtomQueryRunner } from "~/state/use-atom-query-runner";
49 import { useStableCallback } from "../../hooks/useStableCallback";
50 +import { countPreparingComposerImages } from "./preparingComposerImages";
51
52 async function downloadAttachmentBlob(url: string): Promise<Blob | null> {
53 try {
@@ -74,7 +75,6 @@ export function useComposerContextImport(input: {
75 readonly composerContextRecords: ComposerDraftContextRecords;
76 readonly composerImages: ReadonlyArray<ComposerImageAttachment>;
77 readonly composerFilesRef: ChatComposerProps["composerFilesRef"];
77 - readonly pendingImageCompressionsRef: RefObject<Map<string, number>>;
78 readonly pendingThreadImportsRef: RefObject<Map<string, number>>;
79 readonly addComposerFilesToDraft: (files: ComposerFileAttachment[]) => string[];
80 }) {
@@ -91,7 +91,6 @@ export function useComposerContextImport(input: {
91 composerContextRecords,
92 composerImages,
93 composerFilesRef,
94 - pendingImageCompressionsRef,
94 pendingThreadImportsRef,
95 addComposerFilesToDraft,
96 } = input;
@@ -128,7 +127,7 @@ export function useComposerContextImport(input: {
127 return (
128 (draft?.images.length ?? 0) +
129 (draft?.files.length ?? 0) +
131 - (pendingImageCompressionsRef.current.get(attachmentTargetKey) ?? 0) +
130 + countPreparingComposerImages(attachmentTargetKey) +
131 (pendingThreadImportsRef.current.get(attachmentTargetKey) ?? 0) +
132 countQuestionAttachments(otherQuestionKeys)
133 );
apps/web/src/lib/imageCompression.test.ts
+19
@@ -3,9 +3,12 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"
3 import {
4 compressImageForStash,
5 compressImageToByteLimit,
6 + createComposerImageThumbnail,
7 + inheritComposerImageThumbnail,
8 MAX_COMPRESSIBLE_SOURCE_BYTES,
9 MAX_STASH_IMAGE_DATA_URL_CHARS,
10 prepareImageForAttachment,
11 + readComposerImageThumbnail,
12 } from "./imageCompression";
13
14 import type { SnapShotSource } from "@t3tools/contracts";
@@ -243,6 +246,22 @@ describe("compressImageForStash", () => {
246 });
247 });
248
249 +describe("composer image thumbnails", () => {
250 + it("gives the compressed file its source's thumbnail without decoding it again", async () => {
251 + stubCanvasPipeline(() => 3);
252 + const source = makeFile(3);
253 + const compressed = makeFile(2, "image/webp");
254 + const sourceThumbnail = await createComposerImageThumbnail(source);
255 + expect(sourceThumbnail).toMatch(/^data:image\/png;base64,/);
256 +
257 + inheritComposerImageThumbnail(source, compressed);
258 +
259 + expect(readComposerImageThumbnail(compressed)).toBe(sourceThumbnail);
260 + expect(await createComposerImageThumbnail(compressed)).toBe(sourceThumbnail);
261 + expect(createImageBitmap).toHaveBeenCalledTimes(1);
262 + });
263 +});
264 +
265 describe("HEIC attachment preparation", () => {
266 it("converts a HEIC photo with a missing MIME type into a named JPEG", async () => {
267 const original = makeHeicFile({
apps/web/src/lib/imageCompression.ts
+64 -32
@@ -205,43 +205,75 @@ async function encodeCanvas(
205 }
206
207 const composerThumbnails = new WeakMap<File, Promise<string | null>>();
208 +const settledComposerThumbnails = new WeakMap<File, string | null>();
209
209 -export function createComposerImageThumbnail(file: File): Promise<string | null> {
210 - const cached = composerThumbnails.get(file);
211 - if (cached) return cached;
212 - const thumbnail = (async () => {
213 - if (!canRecompress()) return null;
214 - let bitmap: ImageBitmap | undefined;
215 - try {
216 - bitmap = await createImageBitmap(file);
217 - const side = Math.min(bitmap.width, bitmap.height);
218 - if (side <= 0) return null;
219 - const dimension = Math.min(256, side);
220 - const surface = createCanvas(dimension, dimension);
221 - if (!surface) return null;
222 - surface.context.drawImage(
223 - bitmap,
224 - (bitmap.width - side) / 2,
225 - (bitmap.height - side) / 2,
226 - side,
227 - side,
228 - 0,
229 - 0,
230 - dimension,
231 - dimension,
232 - );
233 - const blob = await encodeCanvas(surface.canvas, 1, "image/png");
234 - return blob ? await readFileAsDataUrl(blob) : null;
235 - } catch {
236 - return null;
237 - } finally {
238 - bitmap?.close();
239 - }
240 - })();
210 +function cacheComposerImageThumbnail(
211 + file: File,
212 + thumbnail: Promise<string | null>,
213 +): Promise<string | null> {
214 composerThumbnails.set(file, thumbnail);
215 + void thumbnail.then((src) => settledComposerThumbnails.set(file, src));
216 return thumbnail;
217 }
218
219 +/** `undefined` until the thumbnail settles, so a tile can paint a known thumbnail immediately. */
220 +export function readComposerImageThumbnail(file: File): string | null | undefined {
221 + return settledComposerThumbnails.get(file);
222 +}
223 +
224 +export function createComposerImageThumbnail(file: File): Promise<string | null> {
225 + return (
226 + composerThumbnails.get(file) ??
227 + cacheComposerImageThumbnail(file, renderComposerImageThumbnail(file))
228 + );
229 +}
230 +
231 +/**
232 + * Pasted images are re-encoded after their placeholder already shows the source's thumbnail.
233 + * Reusing it keeps the tile from flashing back to its fallback while the new file is decoded.
234 + */
235 +export function inheritComposerImageThumbnail(source: File, target: File): void {
236 + const sourceThumbnail = composerThumbnails.get(source);
237 + if (source === target || !sourceThumbnail || composerThumbnails.has(target)) return;
238 + const settled = settledComposerThumbnails.get(source);
239 + if (settled) settledComposerThumbnails.set(target, settled);
240 + cacheComposerImageThumbnail(
241 + target,
242 + sourceThumbnail.then((src) => src ?? renderComposerImageThumbnail(target)),
243 + );
244 +}
245 +
246 +async function renderComposerImageThumbnail(file: File): Promise<string | null> {
247 + // Placeholders thumbnail the uncompressed source, which can be big enough to OOM the decode.
248 + if (!canRecompress() || file.size > MAX_COMPRESSIBLE_SOURCE_BYTES) return null;
249 + let bitmap: ImageBitmap | undefined;
250 + try {
251 + bitmap = await createImageBitmap(file);
252 + const side = Math.min(bitmap.width, bitmap.height);
253 + if (side <= 0) return null;
254 + const dimension = Math.min(256, side);
255 + const surface = createCanvas(dimension, dimension);
256 + if (!surface) return null;
257 + surface.context.drawImage(
258 + bitmap,
259 + (bitmap.width - side) / 2,
260 + (bitmap.height - side) / 2,
261 + side,
262 + side,
263 + 0,
264 + 0,
265 + dimension,
266 + dimension,
267 + );
268 + const blob = await encodeCanvas(surface.canvas, 1, "image/png");
269 + return blob ? await readFileAsDataUrl(blob) : null;
270 + } catch {
271 + return null;
272 + } finally {
273 + bitmap?.close();
274 + }
275 +}
276 +
277 async function encodeWithinBudget(
278 bitmap: ImageBitmap,
279 maxDimension: number,