@@ -375,6 +375,8 @@ export async function pickComposerImages(input: { readonly existingCount: number
375375export async function pickComposerMedia ( input : {
376376 readonly existingCount : number ;
377377 readonly maxVideoBytes ?: number ;
378+ /** Receives placeholders over the picker's local copies before processing; each keeps its attachment's id. */
379+ readonly onPicked ?: ( placeholders : ReadonlyArray < DraftComposerAttachment > ) => void ;
378380} ) : Promise < {
379381 readonly attachments : ReadonlyArray < DraftComposerAttachment > ;
380382 readonly error : string | null ;
@@ -425,11 +427,28 @@ export async function pickComposerMedia(input: {
425427 error : null ,
426428 } ;
427429 }
430+ const ids = result . assets . map ( ( ) => uuidv4 ( ) ) ;
431+ input . onPicked ?.(
432+ result . assets . flatMap ( ( asset , index ) : DraftComposerAttachment [ ] => {
433+ const common = {
434+ id : ids [ index ] ! ,
435+ name : asset . fileName ?? "" ,
436+ mimeType : asset . mimeType ?? "" ,
437+ sizeBytes : asset . fileSize ?? 0 ,
438+ } ;
439+ if ( asset . type === "image" || asset . mimeType ?. startsWith ( "image/" ) ) {
440+ return [ { ...common , type : "image" , previewUri : asset . uri } ] ;
441+ }
442+ return input . maxVideoBytes === undefined
443+ ? [ ]
444+ : [ { ...common , type : "file" , fileUri : asset . uri } ] ;
445+ } ) ,
446+ ) ;
428447
429448 const attachments : DraftComposerAttachment [ ] = [ ] ;
430449 let error : string | null = null ;
431450
432- for ( const asset of result . assets ) {
451+ for ( const [ index , asset ] of result . assets . entries ( ) ) {
433452 if ( attachments . length >= remainingSlots ) {
434453 error = `You can attach up to ${ PROVIDER_SEND_TURN_MAX_ATTACHMENTS } attachments per message.` ;
435454 break ;
@@ -443,15 +462,16 @@ export async function pickComposerMedia(input: {
443462 try {
444463 const { File } = await import ( "expo-file-system" ) ;
445464 const file = new File ( asset . uri ) ;
446- attachments . push (
447- await createComposerFileAttachment ( {
465+ attachments . push ( {
466+ ... ( await createComposerFileAttachment ( {
448467 uri : asset . uri ,
449468 name : asset . fileName ?. trim ( ) || file . name || "video" ,
450469 mimeType : mimeType || file . type || "application/octet-stream" ,
451470 sizeBytes : asset . fileSize ?? null ,
452471 maxBytes : clampFileAttachmentUploadBytes ( input . maxVideoBytes ) ,
453- } ) ,
454- ) ;
472+ } ) ) ,
473+ id : ids [ index ] ! ,
474+ } ) ;
455475 } catch ( cause ) {
456476 error =
457477 cause instanceof Error ? cause . message : `Could not read '${ asset . fileName ?? "video" } '.` ;
@@ -482,7 +502,8 @@ export async function pickComposerMedia(input: {
482502 ? mimeType
483503 : null ;
484504
485- let image : { base64 : string ; mimeType : string ; name : string ; previewUri : string } ;
505+ let image : { base64 : string ; mimeType : string ; name : string } ;
506+ let renderedUri : string | undefined ;
486507 try {
487508 if ( originalMimeType !== null ) {
488509 const { File } = await import ( "expo-file-system" ) ;
@@ -492,25 +513,34 @@ export async function pickComposerMedia(input: {
492513 mimeType : originalMimeType ,
493514 base64,
494515 } ) ;
516+ renderedUri = fitted ?. uri ;
495517 image = {
496518 base64 : fitted ?. base64 ?? base64 ,
497519 mimeType : originalMimeType ,
498520 name,
499- previewUri : fitted ?. uri ?? asset . uri ,
500521 } ;
501522 } else {
502523 const rendered = await renderPhoto ( asset . uri ) ;
524+ renderedUri = rendered . uri ;
503525 image = {
504526 base64 : rendered . base64 ,
505527 mimeType : "image/jpeg" ,
506528 name : / \. j p e ? g $ / i. test ( name ) ? name : `${ name . replace ( / \. [ ^ . ] + $ / , "" ) } .jpg` ,
507- previewUri : rendered . uri ,
508529 } ;
509530 }
510531 } catch {
511532 error = `Failed to read '${ name } '.` ;
512533 continue ;
513534 }
535+ // The bytes now live in the data URL, and the preview uses the picker copy.
536+ if ( renderedUri !== undefined ) {
537+ try {
538+ const { File } = await import ( "expo-file-system" ) ;
539+ new File ( renderedUri ) . delete ( ) ;
540+ } catch ( cause ) {
541+ console . warn ( "[composer-images] could not remove rendered photo" , cause ) ;
542+ }
543+ }
514544
515545 const sizeBytes = estimateBase64ByteSize ( image . base64 ) ;
516546 if ( sizeBytes <= 0 || sizeBytes > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES ) {
@@ -519,13 +549,14 @@ export async function pickComposerMedia(input: {
519549 }
520550
521551 attachments . push ( {
522- id : uuidv4 ( ) ,
552+ id : ids [ index ] ! ,
523553 type : "image" ,
524554 name : image . name ,
525555 mimeType : image . mimeType ,
526556 sizeBytes,
527557 dataUrl : `data:${ image . mimeType } ;base64,${ image . base64 } ` ,
528- previewUri : image . previewUri ,
558+ // The picker copy is already on screen as the placeholder; reusing it avoids a reload flash.
559+ previewUri : asset . uri ,
529560 } ) ;
530561 }
531562
0 commit comments