@@ -207,35 +207,55 @@ export class AttachmentManager {
207
207
return this . attachments . indexOf ( attachmentsById [ localId ] ) ;
208
208
} ;
209
209
210
- upsertAttachments = ( attachmentsToUpsert : LocalAttachment [ ] ) => {
211
- if ( ! attachmentsToUpsert . length ) return ;
210
+ private prepareAttachmentUpdate = ( attachmentToUpdate : LocalAttachment ) => {
211
+ const stateAttachments = this . attachments ;
212
+ const attachments = [ ...this . attachments ] ;
213
+ const attachmentIndex = this . getAttachmentIndex ( attachmentToUpdate . localMetadata . id ) ;
214
+ if ( attachmentIndex === - 1 ) return null ;
215
+ // do not re-organize newAttachments array otherwise indexing would no longer work
216
+ // replace in place only with the attachments with the same id's
217
+ const merged = mergeWithDiff < LocalAttachment > (
218
+ stateAttachments [ attachmentIndex ] ,
219
+ attachmentToUpdate ,
220
+ ) ;
221
+ const updatesOnMerge = merged . diff && Object . keys ( merged . diff . children ) . length ;
222
+ if ( updatesOnMerge ) {
223
+ const localAttachment = ensureIsLocalAttachment ( merged . result ) ;
224
+ if ( localAttachment ) {
225
+ attachments . splice ( attachmentIndex , 1 , localAttachment ) ;
226
+ return attachments ;
227
+ }
228
+ }
229
+ return null ;
230
+ } ;
212
231
213
- const currentAttachments = this . attachments ;
214
- const newAttachments = [ ...currentAttachments ] ;
232
+ updateAttachment = ( attachmentToUpdate : LocalAttachment ) => {
233
+ const updatedAttachments = this . prepareAttachmentUpdate ( attachmentToUpdate ) ;
234
+ if ( updatedAttachments ) {
235
+ this . state . partialNext ( { attachments : updatedAttachments } ) ;
236
+ }
237
+ } ;
215
238
239
+ upsertAttachments = ( attachmentsToUpsert : LocalAttachment [ ] ) => {
240
+ if ( ! attachmentsToUpsert . length ) return ;
241
+ let attachments = [ ...this . attachments ] ;
242
+ let hasUpdates = false ;
216
243
attachmentsToUpsert . forEach ( ( attachment ) => {
217
- const targetAttachmentIndex = this . getAttachmentIndex ( attachment . localMetadata ?. id ) ;
218
-
219
- if ( targetAttachmentIndex < 0 ) {
220
- const localAttachment = ensureIsLocalAttachment ( attachment ) ;
221
- if ( localAttachment ) newAttachments . push ( localAttachment ) ;
244
+ const updatedAttachments = this . prepareAttachmentUpdate ( attachment ) ;
245
+ if ( updatedAttachments ) {
246
+ attachments = updatedAttachments ;
247
+ hasUpdates = true ;
222
248
} else {
223
- // do not re-organize newAttachments array otherwise indexing would no longer work
224
- // replace in place only with the attachments with the same id's
225
- const merged = mergeWithDiff < LocalAttachment > (
226
- currentAttachments [ targetAttachmentIndex ] ,
227
- attachment ,
228
- ) ;
229
- const updatesOnMerge = merged . diff && Object . keys ( merged . diff . children ) . length ;
230
- if ( updatesOnMerge ) {
231
- const localAttachment = ensureIsLocalAttachment ( merged . result ) ;
232
- if ( localAttachment )
233
- newAttachments . splice ( targetAttachmentIndex , 1 , localAttachment ) ;
249
+ const localAttachment = ensureIsLocalAttachment ( attachment ) ;
250
+ if ( localAttachment ) {
251
+ attachments . push ( localAttachment ) ;
252
+ hasUpdates = true ;
234
253
}
235
254
}
236
255
} ) ;
237
-
238
- this . state . partialNext ( { attachments : newAttachments } ) ;
256
+ if ( hasUpdates ) {
257
+ this . state . partialNext ( { attachments } ) ;
258
+ }
239
259
} ;
240
260
241
261
removeAttachments = ( localAttachmentIds : string [ ] ) => {
@@ -476,7 +496,7 @@ export class AttachmentManager {
476
496
} ,
477
497
} ;
478
498
479
- this . upsertAttachments ( [ failedAttachment ] ) ;
499
+ this . updateAttachment ( failedAttachment ) ;
480
500
return failedAttachment ;
481
501
}
482
502
@@ -510,7 +530,7 @@ export class AttachmentManager {
510
530
( uploadedAttachment as LocalNotImageAttachment ) . thumb_url = response . thumb_url ;
511
531
}
512
532
513
- this . upsertAttachments ( [ uploadedAttachment ] ) ;
533
+ this . updateAttachment ( uploadedAttachment ) ;
514
534
515
535
return uploadedAttachment ;
516
536
} ;
0 commit comments