@@ -628,6 +628,53 @@ function readPersistedAttachmentIdsFromStorage(threadId: ThreadId): string[] {
628628 }
629629}
630630
631+ function verifyPersistedAttachments (
632+ threadId : ThreadId ,
633+ attachments : PersistedComposerImageAttachment [ ] ,
634+ set : (
635+ partial :
636+ | ComposerDraftStoreState
637+ | Partial < ComposerDraftStoreState >
638+ | ( (
639+ state : ComposerDraftStoreState ,
640+ ) => ComposerDraftStoreState | Partial < ComposerDraftStoreState > ) ,
641+ replace ?: false ,
642+ ) => void ,
643+ ) : void {
644+ let persistedIdSet = new Set < string > ( ) ;
645+ try {
646+ composerDebouncedStorage . flush ( ) ;
647+ persistedIdSet = new Set ( readPersistedAttachmentIdsFromStorage ( threadId ) ) ;
648+ } catch {
649+ persistedIdSet = new Set ( ) ;
650+ }
651+ set ( ( state ) => {
652+ const current = state . draftsByThreadId [ threadId ] ;
653+ if ( ! current ) {
654+ return state ;
655+ }
656+ const imageIdSet = new Set ( current . images . map ( ( image ) => image . id ) ) ;
657+ const persistedAttachments = attachments . filter (
658+ ( attachment ) => imageIdSet . has ( attachment . id ) && persistedIdSet . has ( attachment . id ) ,
659+ ) ;
660+ const nonPersistedImageIds = current . images
661+ . map ( ( image ) => image . id )
662+ . filter ( ( imageId ) => ! persistedIdSet . has ( imageId ) ) ;
663+ const nextDraft : ComposerThreadDraftState = {
664+ ...current ,
665+ persistedAttachments,
666+ nonPersistedImageIds,
667+ } ;
668+ const nextDraftsByThreadId = { ...state . draftsByThreadId } ;
669+ if ( shouldRemoveDraft ( nextDraft ) ) {
670+ delete nextDraftsByThreadId [ threadId ] ;
671+ } else {
672+ nextDraftsByThreadId [ threadId ] = nextDraft ;
673+ }
674+ return { draftsByThreadId : nextDraftsByThreadId } ;
675+ } ) ;
676+ }
677+
631678function hydreatePersistedComposerImageAttachment (
632679 attachment : PersistedComposerImageAttachment ,
633680) : File | null {
@@ -1404,32 +1451,7 @@ export const useComposerDraftStore = create<ComposerDraftStoreState>()(
14041451 return { draftsByThreadId : nextDraftsByThreadId } ;
14051452 } ) ;
14061453 Promise . resolve ( ) . then ( ( ) => {
1407- const persistedIdSet = new Set ( readPersistedAttachmentIdsFromStorage ( threadId ) ) ;
1408- set ( ( state ) => {
1409- const current = state . draftsByThreadId [ threadId ] ;
1410- if ( ! current ) {
1411- return state ;
1412- }
1413- const imageIdSet = new Set ( current . images . map ( ( image ) => image . id ) ) ;
1414- const persistedAttachments = attachments . filter (
1415- ( attachment ) => imageIdSet . has ( attachment . id ) && persistedIdSet . has ( attachment . id ) ,
1416- ) ;
1417- const nonPersistedImageIds = current . images
1418- . map ( ( image ) => image . id )
1419- . filter ( ( imageId ) => ! persistedIdSet . has ( imageId ) ) ;
1420- const nextDraft : ComposerThreadDraftState = {
1421- ...current ,
1422- persistedAttachments,
1423- nonPersistedImageIds,
1424- } ;
1425- const nextDraftsByThreadId = { ...state . draftsByThreadId } ;
1426- if ( shouldRemoveDraft ( nextDraft ) ) {
1427- delete nextDraftsByThreadId [ threadId ] ;
1428- } else {
1429- nextDraftsByThreadId [ threadId ] = nextDraft ;
1430- }
1431- return { draftsByThreadId : nextDraftsByThreadId } ;
1432- } ) ;
1454+ verifyPersistedAttachments ( threadId , attachments , set ) ;
14331455 } ) ;
14341456 } ,
14351457 clearComposerContent : ( threadId ) => {
0 commit comments