ui: export conversations from database instead of cached store (#27432)

This commit is contained in:
Tom Tan
2026-09-04 15:13:10 +02:00
committed by GitHub
parent 49c0dc82b8
commit 1863ac0333
3 changed files with 194 additions and 23 deletions
@@ -139,12 +139,8 @@
async function handleExportConfirm(selectedConversations: DatabaseConversation[]) {
try {
const allData: ExportedConversation[] = await Promise.all(
selectedConversations.map(async (conv) => {
const messages = await conversationsStore.getConversationMessages(conv.id);
return { conv: $state.snapshot(conv), messages: $state.snapshot(messages) };
})
const allData = await conversationsStore.getConversationsForExport(
selectedConversations.map((conv) => conv.id)
);
if (allData.length === 1) {
@@ -168,15 +168,7 @@ class ConversationsStore implements ConversationsPreferencesHost {
if (convIds.length === 0) return;
try {
const fetched = await DatabaseService.getConversationsWithMessages(convIds);
const activeId = this.activeConversation?.id;
const overridden = fetched.get(activeId ?? '');
if (overridden && activeId) {
overridden.conv = { ...this.activeConversation! };
}
const exported = [...fetched.values()];
const exported = await this.getConversationsForExport(convIds);
if (exported.length === 0) {
toast.error('No conversations to export');
@@ -365,16 +357,11 @@ class ConversationsStore implements ConversationsPreferencesHost {
* @param convId - The conversation ID to download
*/
async downloadConversation(convId: string): Promise<void> {
const conversation =
this.activeConversation?.id === convId
? this.activeConversation
: await DatabaseService.getConversation(convId);
const [exportedConversation] = await this.getConversationsForExport([convId]);
if (!conversation) return;
if (!exportedConversation) return;
const messages = await DatabaseService.getConversationMessages(convId);
ConversationTransferService.downloadConversationFile({ conv: conversation, messages });
ConversationTransferService.downloadConversationFile(exportedConversation);
}
/**
@@ -453,6 +440,19 @@ class ConversationsStore implements ConversationsPreferencesHost {
return await DatabaseService.getConversationMessages(convId);
}
/**
* Gets conversations and their messages from the database for export.
* @param convIds - Conversation IDs
* @returns List of conversations with messages, ordered by the input IDs
*/
async getConversationsForExport(convIds: string[]): Promise<ExportedConversation[]> {
const fetched = await DatabaseService.getConversationsWithMessages(convIds);
return convIds
.map((id) => fetched.get(id))
.filter((entry): entry is ExportedConversation => entry !== undefined);
}
/**
* Imports conversations from provided data (without file picker)
* @param data - Array of conversation data with messages