|
4 | 4 | import ConversationSelectionDialog from './ConversationSelectionDialog.svelte'; |
5 | 5 | import { DatabaseStore } from '$lib/stores/database'; |
6 | 6 | import type { ExportedConversations } from '$lib/types/database'; |
7 | | - import { createMessageCountMap } from '$lib/utils/conversationUtils'; |
| 7 | + import { createMessageCountMap } from '$lib/utils/conversation-utils'; |
8 | 8 | import { chatStore } from '$lib/stores/chat.svelte'; |
9 | 9 |
|
10 | 10 | let exportedConversations = $state<DatabaseConversation[]>([]); |
11 | 11 | let importedConversations = $state<DatabaseConversation[]>([]); |
12 | 12 | let showExportSummary = $state(false); |
13 | 13 | let showImportSummary = $state(false); |
14 | 14 |
|
15 | | - // Dialog state |
16 | 15 | let showExportDialog = $state(false); |
17 | 16 | let showImportDialog = $state(false); |
18 | 17 | let availableConversations = $state<DatabaseConversation[]>([]); |
|
23 | 22 |
|
24 | 23 | async function handleExportClick() { |
25 | 24 | try { |
26 | | - // Load all conversations for selection |
27 | 25 | const allConversations = await DatabaseStore.getAllConversations(); |
28 | 26 | if (allConversations.length === 0) { |
29 | 27 | alert('No conversations to export'); |
30 | 28 | return; |
31 | 29 | } |
32 | 30 |
|
33 | | - // Load messages for each conversation to get counts |
34 | 31 | const conversationsWithMessages = await Promise.all( |
35 | 32 | allConversations.map(async (conv) => { |
36 | 33 | const messages = await DatabaseStore.getConversationMessages(conv.id); |
37 | 34 | return { conv, messages }; |
38 | 35 | }) |
39 | 36 | ); |
40 | 37 |
|
41 | | - // Create message count map |
42 | 38 | messageCountMap = createMessageCountMap(conversationsWithMessages); |
43 | 39 | availableConversations = allConversations; |
44 | 40 | showExportDialog = true; |
|
50 | 46 |
|
51 | 47 | async function handleExportConfirm(selectedConversations: DatabaseConversation[]) { |
52 | 48 | try { |
53 | | - // Export selected conversations - use snapshot to get plain objects |
54 | 49 | const allData: ExportedConversations = await Promise.all( |
55 | 50 | selectedConversations.map(async (conv) => { |
56 | 51 | const messages = await DatabaseStore.getConversationMessages(conv.id); |
|
63 | 58 | }); |
64 | 59 | const url = URL.createObjectURL(blob); |
65 | 60 | const a = document.createElement('a'); |
| 61 | +
|
66 | 62 | a.href = url; |
67 | 63 | a.download = `conversations_${new Date().toISOString().split('T')[0]}.json`; |
68 | 64 | document.body.appendChild(a); |
|
82 | 78 |
|
83 | 79 | async function handleImportClick() { |
84 | 80 | try { |
85 | | - // Open file picker |
86 | 81 | const input = document.createElement('input'); |
| 82 | +
|
87 | 83 | input.type = 'file'; |
88 | 84 | input.accept = '.json'; |
89 | 85 |
|
|
112 | 108 | ); |
113 | 109 | } |
114 | 110 |
|
115 | | - // Store full import data and extract conversations for selection dialog |
116 | 111 | fullImportData = importedData; |
117 | 112 | availableConversations = importedData.map( |
118 | 113 | (item: { conv: DatabaseConversation; messages: DatabaseMessage[] }) => item.conv |
119 | 114 | ); |
120 | | -
|
121 | | - // Create message count map from imported data |
122 | 115 | messageCountMap = createMessageCountMap(importedData); |
123 | 116 | showImportDialog = true; |
124 | 117 | } catch (err: unknown) { |
125 | 118 | const message = err instanceof Error ? err.message : 'Unknown error'; |
| 119 | +
|
126 | 120 | console.error('Failed to parse file:', err); |
127 | 121 | alert(`Failed to parse file: ${message}`); |
128 | 122 | } |
|
137 | 131 |
|
138 | 132 | async function handleImportConfirm(selectedConversations: DatabaseConversation[]) { |
139 | 133 | try { |
140 | | - // Filter the original import data to only include selected conversations |
141 | 134 | const selectedIds = new Set(selectedConversations.map((c) => c.id)); |
142 | 135 | const selectedData = $state |
143 | 136 | .snapshot(fullImportData) |
144 | 137 | .filter((item) => selectedIds.has(item.conv.id)); |
145 | 138 |
|
146 | | - // Import selected conversations with their messages |
147 | 139 | await DatabaseStore.importConversations(selectedData); |
148 | 140 |
|
149 | | - // Refresh the conversations list in the chat store |
150 | 141 | await chatStore.loadConversations(); |
151 | 142 |
|
152 | 143 | importedConversations = selectedConversations; |
|
245 | 236 | </div> |
246 | 237 | </div> |
247 | 238 |
|
248 | | -<!-- Export Dialog --> |
249 | 239 | <ConversationSelectionDialog |
250 | 240 | conversations={availableConversations} |
251 | 241 | {messageCountMap} |
|
255 | 245 | onConfirm={handleExportConfirm} |
256 | 246 | /> |
257 | 247 |
|
258 | | -<!-- Import Dialog --> |
259 | 248 | <ConversationSelectionDialog |
260 | 249 | conversations={availableConversations} |
261 | 250 | {messageCountMap} |
|
0 commit comments