@@ -846,6 +846,53 @@ function readPersistedAttachmentIdsFromStorage(threadId: ThreadId): string[] {
846846 }
847847}
848848
849+ function verifyPersistedAttachments (
850+ threadId : ThreadId ,
851+ attachments : PersistedComposerImageAttachment [ ] ,
852+ set : (
853+ partial :
854+ | ComposerDraftStoreState
855+ | Partial < ComposerDraftStoreState >
856+ | ( (
857+ state : ComposerDraftStoreState ,
858+ ) => ComposerDraftStoreState | Partial < ComposerDraftStoreState > ) ,
859+ replace ?: false ,
860+ ) => void ,
861+ ) : void {
862+ let persistedIdSet = new Set < string > ( ) ;
863+ try {
864+ composerDebouncedStorage . flush ( ) ;
865+ persistedIdSet = new Set ( readPersistedAttachmentIdsFromStorage ( threadId ) ) ;
866+ } catch {
867+ persistedIdSet = new Set ( ) ;
868+ }
869+ set ( ( state ) => {
870+ const current = state . draftsByThreadId [ threadId ] ;
871+ if ( ! current ) {
872+ return state ;
873+ }
874+ const imageIdSet = new Set ( current . images . map ( ( image ) => image . id ) ) ;
875+ const persistedAttachments = attachments . filter (
876+ ( attachment ) => imageIdSet . has ( attachment . id ) && persistedIdSet . has ( attachment . id ) ,
877+ ) ;
878+ const nonPersistedImageIds = current . images
879+ . map ( ( image ) => image . id )
880+ . filter ( ( imageId ) => ! persistedIdSet . has ( imageId ) ) ;
881+ const nextDraft : ComposerThreadDraftState = {
882+ ...current ,
883+ persistedAttachments,
884+ nonPersistedImageIds,
885+ } ;
886+ const nextDraftsByThreadId = { ...state . draftsByThreadId } ;
887+ if ( shouldRemoveDraft ( nextDraft ) ) {
888+ delete nextDraftsByThreadId [ threadId ] ;
889+ } else {
890+ nextDraftsByThreadId [ threadId ] = nextDraft ;
891+ }
892+ return { draftsByThreadId : nextDraftsByThreadId } ;
893+ } ) ;
894+ }
895+
849896function hydreatePersistedComposerImageAttachment (
850897 attachment : PersistedComposerImageAttachment ,
851898) : File | null {
@@ -1662,32 +1709,7 @@ export const useComposerDraftStore = create<ComposerDraftStoreState>()(
16621709 return { draftsByThreadId : nextDraftsByThreadId } ;
16631710 } ) ;
16641711 Promise . resolve ( ) . then ( ( ) => {
1665- const persistedIdSet = new Set ( readPersistedAttachmentIdsFromStorage ( threadId ) ) ;
1666- set ( ( state ) => {
1667- const current = state . draftsByThreadId [ threadId ] ;
1668- if ( ! current ) {
1669- return state ;
1670- }
1671- const imageIdSet = new Set ( current . images . map ( ( image ) => image . id ) ) ;
1672- const persistedAttachments = attachments . filter (
1673- ( attachment ) => imageIdSet . has ( attachment . id ) && persistedIdSet . has ( attachment . id ) ,
1674- ) ;
1675- const nonPersistedImageIds = current . images
1676- . map ( ( image ) => image . id )
1677- . filter ( ( imageId ) => ! persistedIdSet . has ( imageId ) ) ;
1678- const nextDraft : ComposerThreadDraftState = {
1679- ...current ,
1680- persistedAttachments,
1681- nonPersistedImageIds,
1682- } ;
1683- const nextDraftsByThreadId = { ...state . draftsByThreadId } ;
1684- if ( shouldRemoveDraft ( nextDraft ) ) {
1685- delete nextDraftsByThreadId [ threadId ] ;
1686- } else {
1687- nextDraftsByThreadId [ threadId ] = nextDraft ;
1688- }
1689- return { draftsByThreadId : nextDraftsByThreadId } ;
1690- } ) ;
1712+ verifyPersistedAttachments ( threadId , attachments , set ) ;
16911713 } ) ;
16921714 } ,
16931715 clearComposerContent : ( threadId ) => {
0 commit comments