Skip to content

Commit 5f7fe26

Browse files
bmdavis419claude
andcommitted
fix(web): pasted images show their upload tile instantly
Pasted images were compressed before joining the draft, so the strip showed nothing until a large screenshot finished re-encoding. A placeholder tile now shows the source image at 0% right away, and the compressed image takes over the same tile and thumbnail without a flash. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3b95e78 commit 5f7fe26

7 files changed

Lines changed: 204 additions & 65 deletions

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ import { useComposerImageGroups } from "./useComposerImageGroups";
290290
import { useComposerStash } from "./useComposerStash";
291291
import { useComposerContextImport } from "./useComposerContextImport";
292292
import { useComposerAttachmentIntake } from "./useComposerAttachmentIntake";
293+
import {
294+
countPreparingComposerImages,
295+
usePreparingComposerImages,
296+
} from "./preparingComposerImages";
293297
import { ComposerControlsBar } from "./ComposerControlsBar";
294298
import { useComposerCommandKey } from "./useComposerCommandKey";
295299

@@ -746,6 +750,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
746750
uncommittedSnapShotIds,
747751
standaloneComposerImages,
748752
} = useComposerImageGroups(attachmentDraftTarget, composerImages, composerPreviewAnnotations);
753+
const preparingComposerImages = usePreparingComposerImages(attachmentTargetKey);
749754
const nonPersistedComposerImageIds = attachmentDraft.nonPersistedImageIds;
750755
const uploadsByImageId = useAttachmentUploadStore((state) => state.uploadsByImageId);
751756
const openPrLink = useOpenPrLink(routeThreadRef);
@@ -1124,7 +1129,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
11241129
const composerScrollCollapseEligibleRef = useRef(false);
11251130
const windowRefocusInFlightRef = useRef(false);
11261131
const composerScrollGestureRef = useRef(createComposerScrollGestureState());
1127-
const pendingImageCompressionsRef = useRef<Map<string, number>>(new Map());
11281132
const pendingThreadImportsRef = useRef<Map<string, number>>(new Map());
11291133
const isRevertingCheckpointRef = useRef(isRevertingCheckpoint);
11301134
useLayoutEffect(() => {
@@ -1434,7 +1438,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
14341438
composerContextRecords,
14351439
composerImages,
14361440
composerFilesRef,
1437-
pendingImageCompressionsRef,
14381441
pendingThreadImportsRef,
14391442
addComposerFilesToDraft,
14401443
});
@@ -2123,7 +2126,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
21232126
// A send while a pasted image is still compressing would strand that image:
21242127
// the turn snapshot wouldn't include it, and it would surface in the *next*
21252128
// draft instead.
2126-
if (activeThreadId && (pendingImageCompressionsRef.current.get(attachmentTargetKey) ?? 0) > 0) {
2129+
if (activeThreadId && countPreparingComposerImages(attachmentTargetKey) > 0) {
21272130
event?.preventDefault();
21282131
toastManager.add({
21292132
type: "info",
@@ -2373,6 +2376,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
23732376
const expandedComposerImages = isComposerResting
23742377
? standaloneComposerImages.filter((image) => pendingSnapShotIdSet.has(image.id))
23752378
: standaloneComposerImages;
2379+
const expandedPreparingImages = isComposerResting ? [] : preparingComposerImages;
23762380
const composerControlsCollapsed = isComposerResting || isComposerCollapsedMobile;
23772381
const showInlineRestingControls = composerControlsCollapsed && restingControlsHost === null;
23782382
const composerControlsVisibleInStrip =
@@ -2734,7 +2738,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
27342738
composerFilesRef,
27352739
composerEditorRef,
27362740
isRevertingCheckpointRef,
2737-
pendingImageCompressionsRef,
27382741
setThreadError,
27392742
countReservedAttachments,
27402743
addComposerImage,
@@ -3103,7 +3106,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
31033106
},
31043107
addDroppedFolders: addComposerFoldersStable,
31053108
hasPendingAttachments: () =>
3106-
(pendingImageCompressionsRef.current.get(attachmentTargetKey) ?? 0) > 0 ||
3109+
countPreparingComposerImages(attachmentTargetKey) > 0 ||
31073110
pendingDraftWork.has(attachmentTargetKey),
31083111
insertTextAtEnd: insertComposerTextAtEndStable,
31093112
pasteTextAtEnd: (text: string, options) => {
@@ -3367,10 +3370,12 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
33673370
!isComposerCollapsedMobile &&
33683371
(uncommittedSnapShotIds.length > 0 ||
33693372
composerVideos.length > 0 ||
3370-
expandedComposerImages.length > 0) ? (
3373+
expandedComposerImages.length > 0 ||
3374+
expandedPreparingImages.length > 0) ? (
33713375
<ComposerAttachmentMediaRow
33723376
images={composerImages}
33733377
expandedImages={expandedComposerImages}
3378+
preparingImages={expandedPreparingImages}
33743379
videos={composerVideos}
33753380
pendingSnapShotAnimations={pendingSnapShotAnimations}
33763381
pendingSnapShotIds={pendingSnapShotIds}

‎apps/web/src/components/chat/ComposerAttachmentStrip.tsx‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog";
3434
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
3535
import { ComposerImageThumbnail } from "./ComposerImageThumbnail";
3636
import { composerFloatingLayerProps } from "./composerEventScope";
37+
import type { PreparingComposerImage } from "./preparingComposerImages";
3738
import {
3839
attachVideoThumbnail,
3940
buildExpandedImagePreview,
@@ -109,6 +110,7 @@ function SnapShotAttachmentFrame({
109110
export function ComposerAttachmentMediaRow({
110111
images,
111112
expandedImages,
113+
preparingImages,
112114
videos,
113115
pendingSnapShotAnimations,
114116
pendingSnapShotIds,
@@ -127,6 +129,7 @@ export function ComposerAttachmentMediaRow({
127129
}: {
128130
readonly images: ReadonlyArray<ComposerImageAttachment>;
129131
readonly expandedImages: ReadonlyArray<ComposerImageAttachment>;
132+
readonly preparingImages: ReadonlyArray<PreparingComposerImage>;
130133
readonly videos: ReadonlyArray<ComposerFileAttachment>;
131134
readonly pendingSnapShotAnimations: ReadonlyArray<PendingSnapShotAnimation>;
132135
readonly pendingSnapShotIds: ReadonlyArray<string>;
@@ -296,6 +299,37 @@ export function ComposerAttachmentMediaRow({
296299
);
297300
})
298301
.concat(
302+
// Mirrors the image tile above, keyed by the id the compressed image
303+
// takes, so React keeps the same thumbnail element through the swap.
304+
preparingImages.map((image) => (
305+
<SnapShotAttachmentFrame
306+
key={image.id}
307+
className="group/attachment relative h-16 w-16 shrink-0 snap-start overflow-hidden rounded-lg border border-border/80 bg-background"
308+
>
309+
<button
310+
type="button"
311+
className="h-full w-full"
312+
aria-label={`Preview ${image.name}`}
313+
disabled
314+
>
315+
<ComposerImageThumbnail
316+
file={image.file}
317+
alt={image.name}
318+
className="h-full w-full object-cover"
319+
fallback={
320+
<span className="flex h-full items-center justify-center px-1 text-3xs text-secondary-label">
321+
{image.name}
322+
</span>
323+
}
324+
/>
325+
</button>
326+
{supportsAttachmentUploads && (
327+
<span className="pointer-events-none absolute inset-x-0 bottom-0 bg-background/85 px-1 text-center text-3xs text-foreground">
328+
{formatAttachmentUploadProgress(0)}
329+
</span>
330+
)}
331+
</SnapShotAttachmentFrame>
332+
)),
299333
uncommittedSnapShotIds.map((captureId) => (
300334
<SnapShotAttachmentFrame
301335
key={captureId}

‎apps/web/src/components/chat/ComposerImageThumbnail.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { memo, useEffect, useState, type ReactNode } from "react";
22

3-
import { createComposerImageThumbnail } from "../../lib/imageCompression";
3+
import {
4+
createComposerImageThumbnail,
5+
readComposerImageThumbnail,
6+
} from "../../lib/imageCompression";
47

58
/** Keep full-resolution image decoding out of composer rerenders. */
69
export const ComposerImageThumbnail = memo(function ComposerImageThumbnail({
@@ -24,6 +27,6 @@ export const ComposerImageThumbnail = memo(function ComposerImageThumbnail({
2427
active = false;
2528
};
2629
}, [file]);
27-
const src = preview?.file === file ? preview.src : null;
30+
const src = preview?.file === file ? preview.src : (readComposerImageThumbnail(file) ?? null);
2831
return src ? <img src={src} alt={alt} className={className} /> : fallback;
2932
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 removePreparingComposerImages(
44+
targetKey: string,
45+
imageIds: ReadonlySet<string>,
46+
): void {
47+
usePreparingComposerImageStore.setState((state) => {
48+
const remaining = (state.byTargetKey[targetKey] ?? NO_PREPARING_IMAGES).filter(
49+
(image) => !imageIds.has(image.id),
50+
);
51+
const byTargetKey = { ...state.byTargetKey };
52+
if (remaining.length > 0) {
53+
byTargetKey[targetKey] = remaining;
54+
} else {
55+
delete byTargetKey[targetKey];
56+
}
57+
return { byTargetKey };
58+
});
59+
}

‎apps/web/src/components/chat/useComposerAttachmentIntake.ts‎

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,18 @@ import {
3838
pastedTextDisposition,
3939
wouldTextPasteExceedLimit,
4040
} from "@t3tools/client-runtime/text-paste";
41-
import { prepareImageForAttachment } from "../../lib/imageCompression";
41+
import {
42+
inheritComposerImageThumbnail,
43+
prepareImageForAttachment,
44+
} from "../../lib/imageCompression";
4245
import { requestConfirmDialog } from "~/confirmDialog";
4346
import { toastManager } from "../ui/toast";
4447
import { type RefObject, useRef } from "react";
4548
import { videoMimeType } from "../../types";
49+
import {
50+
addPreparingComposerImages,
51+
removePreparingComposerImages,
52+
} from "./preparingComposerImages";
4653

4754
/**
4855
* Takes files into the draft: validates and reserves slots, compresses images, inserts
@@ -65,7 +72,6 @@ export function useComposerAttachmentIntake(input: {
6572
readonly composerFilesRef: ChatComposerProps["composerFilesRef"];
6673
readonly composerEditorRef: RefObject<ComposerPromptEditorHandle | null>;
6774
readonly isRevertingCheckpointRef: RefObject<boolean>;
68-
readonly pendingImageCompressionsRef: RefObject<Map<string, number>>;
6975
readonly setThreadError: ChatComposerProps["setThreadError"];
7076
readonly countReservedAttachments: () => number;
7177
readonly addComposerImage: (image: ComposerImageAttachment) => string[];
@@ -94,7 +100,6 @@ export function useComposerAttachmentIntake(input: {
94100
composerFilesRef,
95101
composerEditorRef,
96102
isRevertingCheckpointRef,
97-
pendingImageCompressionsRef,
98103
setThreadError,
99104
countReservedAttachments,
100105
addComposerImage,
@@ -142,9 +147,8 @@ export function useComposerAttachmentIntake(input: {
142147
stripInlineContextReferences(promptRef.current).trim().length > 0);
143148

144149
// Validation happens synchronously so concurrent pastes see each other:
145-
// accepted files reserve their attachment slots (via the pending counter)
150+
// accepted files reserve their attachment slots (as preparing images)
146151
// before the first await, keeping the total under the limit.
147-
const pendingCount = pendingImageCompressionsRef.current.get(attachmentTargetKey) ?? 0;
148152
let reservedCount = countReservedAttachments();
149153
// A pick that matches a needs-reattach marker replaces it in the draft, so
150154
// it must not consume a slot; a draft full of markers would otherwise hit
@@ -242,16 +246,20 @@ export function useComposerAttachmentIntake(input: {
242246
}
243247
if (acceptedImages.length === 0) return insertedAny;
244248

245-
pendingImageCompressionsRef.current.set(
246-
attachmentTargetKey,
247-
pendingCount + acceptedImages.length,
248-
);
249+
// The draft image reuses its placeholder's id and thumbnail so the strip tile stays exactly
250+
// as it is when the compressed file replaces it.
251+
const preparingImages = acceptedImages.map((file) => ({
252+
id: randomUUID(),
253+
name: file.name || "image",
254+
file,
255+
}));
256+
addPreparingComposerImages(attachmentTargetKey, preparingImages);
249257
if (questionAttachmentTarget)
250258
changeQuestionAttachmentPreparation(questionAttachmentTarget, acceptedImages.length);
251-
const compressAndStoreImages = async (): Promise<boolean> => {
259+
try {
252260
const nextImages: ComposerImageAttachment[] = [];
253261
let compressionError: string | null = null;
254-
for (const file of acceptedImages) {
262+
for (const { id, file } of preparingImages) {
255263
const compressed = await prepareImageForAttachment(
256264
file,
257265
PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
@@ -264,10 +272,11 @@ export function useComposerAttachmentIntake(input: {
264272
continue;
265273
}
266274
const attachmentFile = compressed.file;
275+
inheritComposerImageThumbnail(file, attachmentFile);
267276
const previewUrl = URL.createObjectURL(attachmentFile);
268277
nextImages.push({
269278
type: "image",
270-
id: randomUUID(),
279+
id,
271280
name: attachmentFile.name || "image",
272281
mimeType: attachmentFile.type,
273282
sizeBytes: attachmentFile.size,
@@ -302,18 +311,16 @@ export function useComposerAttachmentIntake(input: {
302311
setThreadError(threadId, compressionError);
303312
}
304313
return insertedAny;
305-
};
306-
return await compressAndStoreImages().finally(() => {
314+
} finally {
315+
// Runs in the same tick as the draft insert above, so the strip swaps
316+
// placeholders for real tiles in one render.
307317
if (questionAttachmentTarget)
308318
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);
315-
}
316-
});
319+
removePreparingComposerImages(
320+
attachmentTargetKey,
321+
new Set(preparingImages.map((image) => image.id)),
322+
);
323+
}
317324
};
318325

319326
const removeComposerImage = (imageId: string) => {

‎apps/web/src/components/chat/useComposerContextImport.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
import { useAtomCommand } from "../../state/use-atom-command";
4848
import { useAtomQueryRunner } from "~/state/use-atom-query-runner";
4949
import { useStableCallback } from "../../hooks/useStableCallback";
50+
import { countPreparingComposerImages } from "./preparingComposerImages";
5051

5152
async function downloadAttachmentBlob(url: string): Promise<Blob | null> {
5253
try {
@@ -74,7 +75,6 @@ export function useComposerContextImport(input: {
7475
readonly composerContextRecords: ComposerDraftContextRecords;
7576
readonly composerImages: ReadonlyArray<ComposerImageAttachment>;
7677
readonly composerFilesRef: ChatComposerProps["composerFilesRef"];
77-
readonly pendingImageCompressionsRef: RefObject<Map<string, number>>;
7878
readonly pendingThreadImportsRef: RefObject<Map<string, number>>;
7979
readonly addComposerFilesToDraft: (files: ComposerFileAttachment[]) => string[];
8080
}) {
@@ -91,7 +91,6 @@ export function useComposerContextImport(input: {
9191
composerContextRecords,
9292
composerImages,
9393
composerFilesRef,
94-
pendingImageCompressionsRef,
9594
pendingThreadImportsRef,
9695
addComposerFilesToDraft,
9796
} = input;
@@ -128,7 +127,7 @@ export function useComposerContextImport(input: {
128127
return (
129128
(draft?.images.length ?? 0) +
130129
(draft?.files.length ?? 0) +
131-
(pendingImageCompressionsRef.current.get(attachmentTargetKey) ?? 0) +
130+
countPreparingComposerImages(attachmentTargetKey) +
132131
(pendingThreadImportsRef.current.get(attachmentTargetKey) ?? 0) +
133132
countQuestionAttachments(otherQuestionKeys)
134133
);

0 commit comments

Comments
 (0)