From 244119bb05e78c7871765969a3381eab51cd807b Mon Sep 17 00:00:00 2001 From: Danish Arora Date: Wed, 10 Sep 2025 17:29:50 +0530 Subject: [PATCH] chore: add clear database button --- src/components/Header.tsx | 69 +++++++++++++++++++++++++++++++ src/lib/database/LocalDatabase.ts | 38 +++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/src/components/Header.tsx b/src/components/Header.tsx index 1218f6e..d019d1c 100644 --- a/src/components/Header.tsx +++ b/src/components/Header.tsx @@ -24,6 +24,7 @@ import { Menu, X, Clock, + Trash2, } from 'lucide-react'; import { DropdownMenu, @@ -32,6 +33,17 @@ import { DropdownMenuSeparator, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, + AlertDialogTrigger, +} from '@/components/ui/alert-dialog'; import { useToast } from '@/components/ui/use-toast'; import { useAppKitAccount, useDisconnect } from '@reown/appkit/react'; import { WalletWizard } from '@/components/ui/wallet-wizard'; @@ -122,6 +134,23 @@ const Header = () => { }); }; + const handleClearDatabase = async () => { + try { + await localDatabase.clearAll(); + toast({ + title: 'Database Cleared', + description: 'All local data has been cleared successfully.', + }); + } catch (error) { + console.error('Failed to clear database:', error); + toast({ + title: 'Error', + description: 'Failed to clear local database. Please try again.', + variant: 'destructive', + }); + } + }; + const getStatusIcon = () => { if (!isConnected) return ; @@ -261,6 +290,46 @@ const Header = () => { + + + e.preventDefault()} + className="flex items-center space-x-2 text-orange-400 focus:text-orange-400" + > + + Clear Database + + + + + + Clear Local Database + + + This will permanently delete all locally stored data including: +
• Posts and comments +
• User identities and preferences +
• Bookmarks and votes +
• UI state and settings +
+
+ This action cannot be undone. +
+
+ + + Cancel + + + Clear Database + + +
+
+ { + // Clear in-memory cache + this.clear(); + + // Clear all IndexedDB stores + if (!this.db) return; + + const storeNames = [ + STORE.CELLS, + STORE.POSTS, + STORE.COMMENTS, + STORE.VOTES, + STORE.MODERATIONS, + STORE.USER_IDENTITIES, + STORE.USER_AUTH, + STORE.DELEGATION, + STORE.UI_STATE, + STORE.META, + STORE.BOOKMARKS, + ]; + + const tx = this.db.transaction(storeNames, 'readwrite'); + + await Promise.all( + storeNames.map(storeName => { + return new Promise((resolve, reject) => { + const store = tx.objectStore(storeName); + const request = store.clear(); + request.onerror = () => reject(request.error); + request.onsuccess = () => resolve(); + }); + }) + ); + } + private storeMessage(message: OpchanMessage): void { switch (message.type) { case MessageType.CELL: