|
1 | 1 | <script lang="ts"> |
2 | | - import { Download, Upload } from '@lucide/svelte'; |
| 2 | + import { Download, Upload, Trash2 } from '@lucide/svelte'; |
3 | 3 | import { Button } from '$lib/components/ui/button'; |
4 | 4 | import { DialogConversationSelection } from '$lib/components/app'; |
| 5 | + import DialogConfirmation from '$lib/components/app/dialogs/DialogConfirmation.svelte'; |
5 | 6 | import { DatabaseStore } from '$lib/stores/database'; |
6 | 7 | import type { ExportedConversations } from '$lib/types/database'; |
7 | 8 | import { createMessageCountMap } from '$lib/utils/conversation-utils'; |
|
20 | 21 | [] |
21 | 22 | ); |
22 | 23 |
|
| 24 | + // Delete functionality state |
| 25 | + let showDeleteDialog = $state(false); |
| 26 | + let deletedCount = $state(0); |
| 27 | + let showDeleteSummary = $state(false); |
| 28 | +
|
23 | 29 | async function handleExportClick() { |
24 | 30 | try { |
25 | 31 | const allConversations = await DatabaseStore.getAllConversations(); |
|
149 | 155 | alert('Failed to import conversations. Please check the file format.'); |
150 | 156 | } |
151 | 157 | } |
| 158 | +
|
| 159 | + async function handleDeleteAllClick() { |
| 160 | + try { |
| 161 | + const allConversations = await DatabaseStore.getAllConversations(); |
| 162 | + if (allConversations.length === 0) { |
| 163 | + alert('No conversations to delete'); |
| 164 | + return; |
| 165 | + } |
| 166 | + showDeleteDialog = true; |
| 167 | + } catch (err) { |
| 168 | + console.error('Failed to load conversations for deletion:', err); |
| 169 | + alert('Failed to load conversations'); |
| 170 | + } |
| 171 | + } |
| 172 | +
|
| 173 | + async function handleDeleteAllConfirm() { |
| 174 | + try { |
| 175 | + const allConversations = await DatabaseStore.getAllConversations(); |
| 176 | + let deleted = 0; |
| 177 | +
|
| 178 | + // Delete each conversation and its messages |
| 179 | + for (const conv of allConversations) { |
| 180 | + await DatabaseStore.deleteConversation(conv.id); |
| 181 | + deleted++; |
| 182 | + } |
| 183 | +
|
| 184 | + // Update chat store to reflect the changes |
| 185 | + await chatStore.loadConversations(); |
| 186 | + chatStore.clearActiveConversation(); |
| 187 | +
|
| 188 | + deletedCount = deleted; |
| 189 | + showDeleteSummary = true; |
| 190 | + showDeleteDialog = false; |
| 191 | + showExportSummary = false; |
| 192 | + showImportSummary = false; |
| 193 | + } catch (err) { |
| 194 | + console.error('Failed to delete conversations:', err); |
| 195 | + alert('Failed to delete conversations'); |
| 196 | + } |
| 197 | + } |
| 198 | +
|
| 199 | + function handleDeleteAllCancel() { |
| 200 | + showDeleteDialog = false; |
| 201 | + } |
152 | 202 | </script> |
153 | 203 |
|
154 | 204 | <div class="space-y-6"> |
|
233 | 283 | </div> |
234 | 284 | {/if} |
235 | 285 | </div> |
| 286 | + |
| 287 | + <div class="grid border-t border-border/30 pt-4"> |
| 288 | + <h4 class="mb-2 text-sm font-medium text-destructive">Delete All Conversations</h4> |
| 289 | + |
| 290 | + <p class="mb-4 text-sm text-muted-foreground"> |
| 291 | + Permanently delete all conversations and their messages. This action cannot be undone. |
| 292 | + Consider exporting your conversations first if you want to keep a backup. |
| 293 | + </p> |
| 294 | + |
| 295 | + <Button |
| 296 | + class="text-destructive-foreground w-full justify-start justify-self-start bg-destructive hover:bg-destructive/80 md:w-auto" |
| 297 | + onclick={handleDeleteAllClick} |
| 298 | + variant="destructive" |
| 299 | + > |
| 300 | + <Trash2 class="mr-2 h-4 w-4" /> |
| 301 | + |
| 302 | + Delete all conversations |
| 303 | + </Button> |
| 304 | + |
| 305 | + {#if showDeleteSummary && deletedCount > 0} |
| 306 | + <div |
| 307 | + class="mt-4 grid overflow-x-auto rounded-lg border border-destructive/50 bg-destructive/10 p-4" |
| 308 | + > |
| 309 | + <h5 class="mb-2 text-sm font-medium text-destructive"> |
| 310 | + Deleted {deletedCount} conversation{deletedCount === 1 ? '' : 's'} |
| 311 | + </h5> |
| 312 | + <p class="text-sm text-muted-foreground"> |
| 313 | + All conversations have been permanently removed from your device. |
| 314 | + </p> |
| 315 | + </div> |
| 316 | + {/if} |
| 317 | + </div> |
236 | 318 | </div> |
237 | 319 | </div> |
238 | 320 |
|
|
253 | 335 | onCancel={() => (showImportDialog = false)} |
254 | 336 | onConfirm={handleImportConfirm} |
255 | 337 | /> |
| 338 | + |
| 339 | +<DialogConfirmation |
| 340 | + bind:open={showDeleteDialog} |
| 341 | + title="Delete all conversations" |
| 342 | + description="Are you sure you want to delete all conversations? This action cannot be undone and will permanently remove all your conversations and messages." |
| 343 | + confirmText="Delete All" |
| 344 | + cancelText="Cancel" |
| 345 | + variant="destructive" |
| 346 | + icon={Trash2} |
| 347 | + onConfirm={handleDeleteAllConfirm} |
| 348 | + onCancel={handleDeleteAllCancel} |
| 349 | +/> |
0 commit comments