fix(web): avoid false draft attachment persistence warnings (#1153)
This commit is contained in:
1 parent
d415558915
commit
e11fb6e778
1 file changed
+48
-26
@@ -846,6 +846,53 @@ function readPersistedAttachmentIdsFromStorage(threadId: ThreadId): string[] {
|
||||
}
|
||||
}
|
||||
|
||||
function verifyPersistedAttachments(
|
||||
threadId: ThreadId,
|
||||
attachments: PersistedComposerImageAttachment[],
|
||||
set: (
|
||||
partial:
|
||||
| ComposerDraftStoreState
|
||||
| Partial<ComposerDraftStoreState>
|
||||
| ((
|
||||
state: ComposerDraftStoreState,
|
||||
) => ComposerDraftStoreState | Partial<ComposerDraftStoreState>),
|
||||
replace?: false,
|
||||
) => void,
|
||||
): void {
|
||||
let persistedIdSet = new Set<string>();
|
||||
try {
|
||||
composerDebouncedStorage.flush();
|
||||
persistedIdSet = new Set(readPersistedAttachmentIdsFromStorage(threadId));
|
||||
} catch {
|
||||
persistedIdSet = new Set();
|
||||
}
|
||||
set((state) => {
|
||||
const current = state.draftsByThreadId[threadId];
|
||||
if (!current) {
|
||||
return state;
|
||||
}
|
||||
const imageIdSet = new Set(current.images.map((image) => image.id));
|
||||
const persistedAttachments = attachments.filter(
|
||||
(attachment) => imageIdSet.has(attachment.id) && persistedIdSet.has(attachment.id),
|
||||
);
|
||||
const nonPersistedImageIds = current.images
|
||||
.map((image) => image.id)
|
||||
.filter((imageId) => !persistedIdSet.has(imageId));
|
||||
const nextDraft: ComposerThreadDraftState = {
|
||||
...current,
|
||||
persistedAttachments,
|
||||
nonPersistedImageIds,
|
||||
};
|
||||
const nextDraftsByThreadId = { ...state.draftsByThreadId };
|
||||
if (shouldRemoveDraft(nextDraft)) {
|
||||
delete nextDraftsByThreadId[threadId];
|
||||
} else {
|
||||
nextDraftsByThreadId[threadId] = nextDraft;
|
||||
}
|
||||
return { draftsByThreadId: nextDraftsByThreadId };
|
||||
});
|
||||
}
|
||||
|
||||
function hydreatePersistedComposerImageAttachment(
|
||||
attachment: PersistedComposerImageAttachment,
|
||||
): File | null {
|
||||
@@ -1662,32 +1709,7 @@ export const useComposerDraftStore = create<ComposerDraftStoreState>()(
|
||||
return { draftsByThreadId: nextDraftsByThreadId };
|
||||
});
|
||||
Promise.resolve().then(() => {
|
||||
const persistedIdSet = new Set(readPersistedAttachmentIdsFromStorage(threadId));
|
||||
set((state) => {
|
||||
const current = state.draftsByThreadId[threadId];
|
||||
if (!current) {
|
||||
return state;
|
||||
}
|
||||
const imageIdSet = new Set(current.images.map((image) => image.id));
|
||||
const persistedAttachments = attachments.filter(
|
||||
(attachment) => imageIdSet.has(attachment.id) && persistedIdSet.has(attachment.id),
|
||||
);
|
||||
const nonPersistedImageIds = current.images
|
||||
.map((image) => image.id)
|
||||
.filter((imageId) => !persistedIdSet.has(imageId));
|
||||
const nextDraft: ComposerThreadDraftState = {
|
||||
...current,
|
||||
persistedAttachments,
|
||||
nonPersistedImageIds,
|
||||
};
|
||||
const nextDraftsByThreadId = { ...state.draftsByThreadId };
|
||||
if (shouldRemoveDraft(nextDraft)) {
|
||||
delete nextDraftsByThreadId[threadId];
|
||||
} else {
|
||||
nextDraftsByThreadId[threadId] = nextDraft;
|
||||
}
|
||||
return { draftsByThreadId: nextDraftsByThreadId };
|
||||
});
|
||||
verifyPersistedAttachments(threadId, attachments, set);
|
||||
});
|
||||
},
|
||||
clearComposerContent: (threadId) => {
|
||||
|
||||
Reference in new issue
Block a user