fix: handle addressLoader as both function and Observable

- Add type checking to handle addressLoader as either function or Observable
- Fix TypeError: addressLoader is not a function error
- Add debugging logs to understand addressLoader type at runtime
- Support both function and Observable patterns for addressLoader usage
This commit is contained in:
Gigi
2025-10-02 08:45:55 +02:00
parent ff7da4c6a2
commit d801af81a4
2 changed files with 67 additions and 27 deletions

View File

@@ -0,0 +1,5 @@
---
alwaysApply: true
---
If you can use an applesauce-module for something, use applesauce. https://hzrd149.github.io/applesauce/typedoc/modules.html

View File

@@ -73,41 +73,76 @@ const Bookmarks: React.FC<BookmarksProps> = ({ addressLoader, onLogout }) => {
console.log('Fetching bookmarks for pubkey:', activeAccount.pubkey)
console.log('Starting bookmark fetch for:', activeAccount.pubkey.slice(0, 8) + '...')
// Debug the addressLoader
console.log('addressLoader in fetchBookmarks:', addressLoader)
console.log('addressLoader type in fetchBookmarks:', typeof addressLoader)
console.log('addressLoader is function:', typeof addressLoader === 'function')
// Use applesauce address loader to fetch bookmark lists (kind 10003)
// This is the proper way according to NIP-51 and applesauce documentation
const bookmarkList: Bookmark[] = []
// Use address loader with relay group for optimal performance
// This follows applesauce-relay documentation for relay groups
const subscription = addressLoader({
kind: 10003, // Bookmark list according to NIP-51
pubkey: activeAccount.pubkey,
// Relay group automatically handles multiple relays and deduplication
// No need to specify individual relays - the group manages this
}).subscribe({
next: (event: NostrEvent) => {
console.log('Received bookmark event:', event)
const bookmarkData = parseBookmarkEvent(event)
if (bookmarkData) {
bookmarkList.push(bookmarkData)
console.log('Parsed bookmark:', bookmarkData)
// Set timeout to prevent hanging
let subscription: { unsubscribe: () => void } | null = null
// Check if addressLoader is a function or an Observable
if (typeof addressLoader === 'function') {
// Use address loader with relay group for optimal performance
// This follows applesauce-relay documentation for relay groups
subscription = addressLoader({
kind: 10003, // Bookmark list according to NIP-51
pubkey: activeAccount.pubkey,
// Relay group automatically handles multiple relays and deduplication
// No need to specify individual relays - the group manages this
}).subscribe({
next: (event: NostrEvent) => {
console.log('Received bookmark event:', event)
const bookmarkData = parseBookmarkEvent(event)
if (bookmarkData) {
bookmarkList.push(bookmarkData)
console.log('Parsed bookmark:', bookmarkData)
}
},
error: (error: unknown) => {
console.error('Error fetching bookmarks:', error)
setLoading(false)
},
complete: () => {
console.log('Bookmark fetch complete. Found:', bookmarkList.length, 'bookmarks')
setBookmarks(bookmarkList)
setLoading(false)
}
},
error: (error: unknown) => {
console.error('Error fetching bookmarks:', error)
setLoading(false)
},
complete: () => {
console.log('Bookmark fetch complete. Found:', bookmarkList.length, 'bookmarks')
setBookmarks(bookmarkList)
setLoading(false)
}
})
})
} else {
// If addressLoader is an Observable, subscribe to it directly
console.log('addressLoader is an Observable, subscribing directly')
subscription = addressLoader.subscribe({
next: (event: NostrEvent) => {
console.log('Received bookmark event:', event)
const bookmarkData = parseBookmarkEvent(event)
if (bookmarkData) {
bookmarkList.push(bookmarkData)
console.log('Parsed bookmark:', bookmarkData)
}
},
error: (error: unknown) => {
console.error('Error fetching bookmarks:', error)
setLoading(false)
},
complete: () => {
console.log('Bookmark fetch complete. Found:', bookmarkList.length, 'bookmarks')
setBookmarks(bookmarkList)
setLoading(false)
}
})
}
// Set timeout to prevent hanging
setTimeout(() => {
console.log('Bookmark fetch timeout. Found:', bookmarkList.length, 'bookmarks')
subscription.unsubscribe()
if (subscription) {
subscription.unsubscribe()
}
if (bookmarkList.length === 0) {
setBookmarks([])
setLoading(false)