Skip to content

Commit 4dcc244

Browse files
bmdavis419claudejuliusmarminge
authored
fix(web): pasted images show their upload tile instantly (pingdotgg#23832)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 58a5d1d commit 4dcc244

8 files changed

Lines changed: 307 additions & 138 deletions

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

Lines changed: 12 additions & 7 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",
@@ -2361,7 +2364,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
23612364
environmentUnavailable !== null ||
23622365
composerSubmissionError !== null ||
23632366
providerInputSubmissionError !== null ||
2364-
hasImageAttachmentAttention;
2367+
hasImageAttachmentAttention ||
2368+
preparingComposerImages.length > 0;
23652369
const isComposerResting = shouldUseRestingComposerLayout({
23662370
isExistingThread: routeKind === "server" && activeThreadId !== null,
23672371
isMobileViewport,
@@ -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+
preparingComposerImages.length > 0) ? (
33713375
<ComposerAttachmentMediaRow
33723376
images={composerImages}
33733377
expandedImages={expandedComposerImages}
3378+
preparingImages={preparingComposerImages}
33743379
videos={composerVideos}
33753380
pendingSnapShotAnimations={pendingSnapShotAnimations}
33763381
pendingSnapShotIds={pendingSnapShotIds}

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

Lines changed: 33 additions & 21 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>;
@@ -156,14 +159,20 @@ export function ComposerAttachmentMediaRow({
156159
: "flex-wrap",
157160
)}
158161
>
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;
162172
const snapShotAnimationPending =
163-
image.source?.kind === "snap-shot" && pendingSnapShotIdSet.has(image.id);
173+
source?.kind === "snap-shot" && pendingSnapShotIdSet.has(image.id);
164174
const snapShotArrival =
165-
image.source?.kind === "snap-shot" &&
166-
shouldAnimateSnapShotArrival(image.source.capturedAt);
175+
source?.kind === "snap-shot" && shouldAnimateSnapShotArrival(source.capturedAt);
167176
return (
168177
<SnapShotAttachmentFrame
169178
key={image.id}
@@ -173,20 +182,21 @@ export function ComposerAttachmentMediaRow({
173182
arrival={snapShotArrival}
174183
animateArrival={snapShotAnimations && !snapShotAnimationPending && snapShotArrival}
175184
animationId={snapShotAnimationPending ? image.id : undefined}
176-
animationSource={snapShotAnimationPending ? image.source : undefined}
185+
animationSource={snapShotAnimationPending ? source : undefined}
177186
className={cn(
178187
"group/attachment shrink-0 snap-start bg-background",
179-
image.source?.kind === "snap-shot"
188+
source?.kind === "snap-shot"
180189
? SNAP_SHOT_ATTACHMENT_FRAME_CLASS
181190
: "relative h-16 w-16 overflow-hidden rounded-lg border border-border/80",
182191
snapShotAnimationPending && "invisible",
183192
)}
184193
>
185-
{image.previewUrl ? (
194+
{!ready || ready.previewUrl ? (
186195
<button
187196
type="button"
188-
className="h-full w-full cursor-zoom-in"
197+
className={cn("h-full w-full", ready && "cursor-zoom-in")}
189198
aria-label={`Preview ${image.name}`}
199+
disabled={!ready}
190200
onClick={() => {
191201
const preview = buildExpandedImagePreview(images, image.id);
192202
if (!preview) return;
@@ -198,11 +208,11 @@ export function ComposerAttachmentMediaRow({
198208
alt={image.name}
199209
className="h-full w-full object-cover"
200210
fallback={
201-
isHeicImageFile({ name: image.name, type: image.mimeType }) ? (
211+
ready && isHeicImageFile({ name: ready.name, type: ready.mimeType }) ? (
202212
<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}
206216
alt={image.name}
207217
className="h-full w-full object-cover"
208218
/>
@@ -219,9 +229,9 @@ export function ComposerAttachmentMediaRow({
219229
{image.name}
220230
</div>
221231
)}
222-
{image.source?.kind === "snap-shot" ? (
232+
{source?.kind === "snap-shot" ? (
223233
<SnapShotAttachmentDetails
224-
source={image.source}
234+
source={source}
225235
className={cn(
226236
upload?.status === "uploading" && "bottom-4",
227237
upload?.status === "failed" && "bottom-8",
@@ -246,12 +256,14 @@ export function ComposerAttachmentMediaRow({
246256
</TooltipPopup>
247257
</Tooltip>
248258
)}
249-
{upload?.status === "uploading" && (
259+
{(ready ? upload?.status === "uploading" : supportsAttachmentUploads) && (
250260
<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+
)}
252264
</span>
253265
)}
254-
{upload?.status === "failed" && (
266+
{ready && upload?.status === "failed" && (
255267
<Tooltip>
256268
<TooltipTrigger
257269
render={
@@ -262,7 +274,7 @@ export function ComposerAttachmentMediaRow({
262274
onClick={() =>
263275
retryAttachmentUpload({
264276
environmentId,
265-
image,
277+
image: ready,
266278
draftTarget: attachmentDraftTarget,
267279
})
268280
}
@@ -279,7 +291,7 @@ export function ComposerAttachmentMediaRow({
279291
<span
280292
className={cn(
281293
"absolute right-1 top-1 flex",
282-
image.source?.kind === "snap-shot" &&
294+
source?.kind === "snap-shot" &&
283295
"opacity-0 transition-opacity pointer-coarse:opacity-100 group-hover/attachment:opacity-100 group-focus-within/attachment:opacity-100",
284296
)}
285297
>

‎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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)