@@ -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" ;
4245import { requestConfirmDialog } from "~/confirmDialog" ;
4346import { toastManager } from "../ui/toast" ;
4447import { type RefObject , useRef } from "react" ;
4548import { 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 ) => {
0 commit comments