From ae9f2607a1535822669f34d1045e206332ac042c Mon Sep 17 00:00:00 2001 From: Gigi Date: Thu, 2 Oct 2025 08:53:39 +0200 Subject: [PATCH] fix: implement proper applesauce relay pool query pattern - Replace addressLoader with direct relayPool usage - Use relayPool.query() with proper filter for kind:10003 events - Add RelayPool import to Bookmarks component - Update TypeScript interfaces to use RelayPool instead of addressLoader - Follow applesauce documentation pattern for querying events --- src/App.tsx | 24 ++-------------------- src/components/Bookmarks.tsx | 40 +++++++++++++++--------------------- 2 files changed, 18 insertions(+), 46 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 855fcc8d..b5e788da 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -12,13 +12,6 @@ function App() { const [eventStore, setEventStore] = useState(null) const [accountManager, setAccountManager] = useState(null) const [relayPool, setRelayPool] = useState(null) - const [addressLoader, setAddressLoader] = useState<((params: { kind: number; pubkey: string; relays?: string[] }) => { - subscribe: (observer: { - next: (event: NostrEvent) => void; - error: (error: unknown) => void; - complete: () => void; - }) => { unsubscribe: () => void }; - }) | null>(null) const [isAuthenticated, setIsAuthenticated] = useState(false) useEffect(() => { @@ -42,25 +35,12 @@ function App() { console.log('Created relay group with', relayUrls.length, 'relays') console.log('Relay URLs:', relayUrls) - // Create address loader using the pool directly - // The pool will handle relay connections and the address loader will use the group - const loader = Loaders.createAddressLoader(pool, { - eventStore: store, - bufferTime: 1000, - followRelayHints: true, - extraRelays: relayUrls - }) - - console.log('Created address loader:', loader) - console.log('Address loader type:', typeof loader) - setEventStore(store) setAccountManager(accounts) setRelayPool(pool) - setAddressLoader(loader) }, []) - if (!eventStore || !accountManager || !relayPool || !addressLoader) { + if (!eventStore || !accountManager || !relayPool) { return
Loading...
} @@ -77,7 +57,7 @@ function App() { setIsAuthenticated(true)} /> ) : ( setIsAuthenticated(false)} /> )} diff --git a/src/components/Bookmarks.tsx b/src/components/Bookmarks.tsx index 07f0fd6e..8eaf1a9f 100644 --- a/src/components/Bookmarks.tsx +++ b/src/components/Bookmarks.tsx @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react' import { Hooks } from 'applesauce-react' import { useEventModel } from 'applesauce-react/hooks' import { Models } from 'applesauce-core' +import { RelayPool } from 'applesauce-relay' import { getParsedContent } from 'applesauce-content/text' import { NostrEvent } from 'nostr-tools' @@ -33,17 +34,11 @@ interface Bookmark { } interface BookmarksProps { - addressLoader: ((params: { kind: number; pubkey: string; relays?: string[] }) => { - subscribe: (observer: { - next: (event: NostrEvent) => void; - error: (error: unknown) => void; - complete: () => void; - }) => { unsubscribe: () => void }; - }) | null + relayPool: RelayPool | null onLogout: () => void } -const Bookmarks: React.FC = ({ addressLoader, onLogout }) => { +const Bookmarks: React.FC = ({ relayPool, onLogout }) => { const [bookmarks, setBookmarks] = useState([]) const [loading, setLoading] = useState(true) const activeAccount = Hooks.useActiveAccount() @@ -53,39 +48,36 @@ const Bookmarks: React.FC = ({ addressLoader, onLogout }) => { useEffect(() => { console.log('Bookmarks useEffect triggered') - console.log('addressLoader:', !!addressLoader) - console.log('addressLoader type:', typeof addressLoader) - console.log('addressLoader value:', addressLoader) + console.log('relayPool:', !!relayPool) console.log('activeAccount:', !!activeAccount) - if (addressLoader && activeAccount) { + if (relayPool && activeAccount) { console.log('Starting to fetch bookmarks...') fetchBookmarks() } else { console.log('Not fetching bookmarks - missing dependencies') } - }, [addressLoader, activeAccount]) + }, [relayPool, activeAccount]) const fetchBookmarks = async () => { - if (!addressLoader || !activeAccount) return + if (!relayPool || !activeAccount) return try { setLoading(true) console.log('Fetching bookmarks for pubkey:', activeAccount.pubkey) console.log('Starting bookmark fetch for:', activeAccount.pubkey.slice(0, 8) + '...') - // Use applesauce address loader to fetch bookmark lists (kind 10003) - // This is the proper way according to NIP-51 and applesauce documentation + // Use applesauce relay pool to query for bookmark events (kind 10003) + // This follows the proper applesauce pattern from the documentation const bookmarkList: Bookmark[] = [] - // Configure addressLoader with specific query parameters - console.log('Configuring addressLoader with kind: 10003, pubkey:', activeAccount.pubkey) - const queryObservable = addressLoader({ - kind: 10003, - pubkey: activeAccount.pubkey - }) + // Create a filter for bookmark events (kind 10003) for the specific pubkey + const filter = { + kinds: [10003], + authors: [activeAccount.pubkey] + } - console.log('Subscribing to configured query Observable') - const subscription = queryObservable.subscribe({ + console.log('Querying relay pool with filter:', filter) + const subscription = relayPool.query(filter).subscribe({ next: (event: NostrEvent) => { console.log('Received bookmark event:', event) const bookmarkData = parseBookmarkEvent(event)