diff --git a/src/components/Debug.tsx b/src/components/Debug.tsx index 753a250b..b4829d6a 100644 --- a/src/components/Debug.tsx +++ b/src/components/Debug.tsx @@ -43,13 +43,11 @@ const Debug: React.FC = ({ relayPool }) => { // Bookmark loading state const [bookmarkEvents, setBookmarkEvents] = useState([]) const [isLoadingBookmarks, setIsLoadingBookmarks] = useState(false) - const [isDecryptingBookmarks, setIsDecryptingBookmarks] = useState(false) const [bookmarkStats, setBookmarkStats] = useState<{ public: number; private: number } | null>(null) const [tLoadBookmarks, setTLoadBookmarks] = useState(null) const [tDecryptBookmarks, setTDecryptBookmarks] = useState(null) - // Individual event decryption state - const [decryptingEventIds, setDecryptingEventIds] = useState>(new Set()) + // Individual event decryption results const [decryptedEvents, setDecryptedEvents] = useState>(new Map()) // Live timing state @@ -220,13 +218,17 @@ const Debug: React.FC = ({ relayPool }) => { const start = performance.now() setLiveTiming(prev => ({ ...prev, loadBookmarks: { startTime: start } })) + // Get signer for auto-decryption + const fullAccount = accountManager.getActive() + const signerCandidate = fullAccount || activeAccount + // Use onEvent callback to stream events as they arrive // Trust EOSE - completes when relays finish, no artificial timeouts const rawEvents = await queryEvents( relayPool, { kinds: [KINDS.ListSimple, KINDS.ListReplaceable, KINDS.List, KINDS.WebBookmark], authors: [activeAccount.pubkey] }, { - onEvent: (evt) => { + onEvent: async (evt) => { // Add event immediately with live deduplication setBookmarkEvents(prev => { // Create unique key for deduplication @@ -249,6 +251,28 @@ const Debug: React.FC = ({ relayPool }) => { // Add new event return [...prev, evt] }) + + // Auto-decrypt if event has encrypted content + if (hasEncryptedContent(evt)) { + console.log('[bunker] 🔓 Auto-decrypting event', evt.id.slice(0, 8)) + try { + const { publicItemsAll, privateItemsAll } = await collectBookmarksFromEvents( + [evt], + activeAccount, + signerCandidate + ) + setDecryptedEvents(prev => new Map(prev).set(evt.id, { + public: publicItemsAll.length, + private: privateItemsAll.length + })) + console.log('[bunker] ✅ Auto-decrypted:', evt.id.slice(0, 8), { + public: publicItemsAll.length, + private: privateItemsAll.length + }) + } catch (error) { + console.error('[bunker] ❌ Auto-decrypt failed:', evt.id.slice(0, 8), error) + } + } } } ) @@ -269,123 +293,15 @@ const Debug: React.FC = ({ relayPool }) => { } } - const handleDecryptBookmarks = async () => { - if (!activeAccount || bookmarkEvents.length === 0) { - DebugBus.warn('debug', 'Cannot decrypt: missing activeAccount or no bookmark events loaded') - return - } - - try { - setIsDecryptingBookmarks(true) - DebugBus.info('debug', 'Decrypting bookmark events...') - - // Start timing - const start = performance.now() - setLiveTiming(prev => ({ ...prev, decryptBookmarks: { startTime: start } })) - - const fullAccount = accountManager.getActive() - const signerCandidate = fullAccount || activeAccount - - const { publicItemsAll, privateItemsAll } = await collectBookmarksFromEvents( - bookmarkEvents, - activeAccount, - signerCandidate - ) - - const ms = Math.round(performance.now() - start) - setLiveTiming(prev => ({ ...prev, decryptBookmarks: undefined })) - setTDecryptBookmarks(ms) - - setBookmarkStats({ - public: publicItemsAll.length, - private: privateItemsAll.length - }) - - DebugBus.info('debug', `Decryption complete`, { - public: publicItemsAll.length, - private: privateItemsAll.length, - total: publicItemsAll.length + privateItemsAll.length, - ms - }) - } catch (error) { - setLiveTiming(prev => ({ ...prev, decryptBookmarks: undefined })) - DebugBus.error('debug', 'Failed to decrypt bookmarks', error instanceof Error ? error.message : String(error)) - } finally { - setIsDecryptingBookmarks(false) - } - } - const handleClearBookmarks = () => { setBookmarkEvents([]) setBookmarkStats(null) setTLoadBookmarks(null) setTDecryptBookmarks(null) - setDecryptingEventIds(new Set()) setDecryptedEvents(new Map()) DebugBus.info('debug', 'Cleared bookmark data') } - const handleDecryptSingleEvent = async (evt: NostrEvent) => { - console.log('[bunker] 🔵 Individual decrypt clicked for event:', evt.id.slice(0, 8)) - console.log('[bunker] activeAccount exists?', !!activeAccount) - - if (!activeAccount) { - console.warn('[bunker] ⚠️ No active account - cannot decrypt') - DebugBus.warn('debug', 'Cannot decrypt: missing activeAccount') - return - } - - try { - setDecryptingEventIds(prev => new Set(prev).add(evt.id)) - console.log('[bunker] 🔓 Decrypting event', evt.id.slice(0, 8), { - kind: evt.kind, - contentLength: evt.content?.length || 0, - hasContent: !!evt.content, - isNip04: evt.content?.includes('?iv='), - hasHiddenContent: Helpers.hasHiddenContent(evt), - hasHiddenTags: Helpers.hasHiddenTags(evt) - }) - - const fullAccount = accountManager.getActive() - const signerCandidate = fullAccount || activeAccount - - console.log('[bunker] Signer info:', { - hasFullAccount: !!fullAccount, - hasSigner: !!signerCandidate, - signerType: (signerCandidate as { type?: string })?.type - }) - - const { publicItemsAll, privateItemsAll } = await collectBookmarksFromEvents( - [evt], - activeAccount, - signerCandidate - ) - - setDecryptedEvents(prev => new Map(prev).set(evt.id, { - public: publicItemsAll.length, - private: privateItemsAll.length - })) - - console.log('[bunker] ✅ Event decrypted:', evt.id.slice(0, 8), { - public: publicItemsAll.length, - private: privateItemsAll.length - }) - - if (privateItemsAll.length === 0 && hasEncryptedContent(evt)) { - console.warn('[bunker] ⚠️ Found 0 private items but event has encrypted content - decrypt may have failed') - } - } catch (error) { - console.error('[bunker] ❌ Failed to decrypt event', evt.id.slice(0, 8), error) - DebugBus.error('debug', `Failed to decrypt event ${evt.id.slice(0, 8)}`, error instanceof Error ? error.message : String(error)) - } finally { - setDecryptingEventIds(prev => { - const next = new Set(prev) - next.delete(evt.id) - return next - }) - } - } - const handleBunkerLogin = async () => { if (!bunkerUri.trim()) { setBunkerError('Please enter a bunker URI') @@ -640,7 +556,7 @@ const Debug: React.FC = ({ relayPool }) => { {/* Bookmark Loading Section */}

Bookmark Loading

-
Test bookmark loading and decryption (kinds: 10003, 30003, 30001, 39701)
+
Test bookmark loading with auto-decryption (kinds: 10003, 30003, 30001, 39701)
- - )} +
{getKindName(evt.kind)}
+ {dTag &&
d-tag: {dTag}
} + {titleTag &&
title: {titleTag}
} +
+
Size: {formatBytes(size)}
+
Public: {counts.public}
+ {hasEncrypted &&
🔒 Has encrypted content
}
+ {decryptResult && ( +
+
✓ Decrypted: {decryptResult.public} public, {decryptResult.private} private
+
+ )} +
ID: {evt.id}
) })}