Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 48 additions & 26 deletions apps/web/src/composerDraftStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,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 {
Expand Down Expand Up @@ -1116,32 +1163,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) => {
Expand Down