mirror of
https://github.com/t4t5/nostr-react.git
synced 2025-12-03 17:28:30 +01:00
Add debug functionality
This commit is contained in:
@@ -14,6 +14,7 @@ type OnConnectFunc = (relay: Relay) => void
|
|||||||
|
|
||||||
interface NostrContextType {
|
interface NostrContextType {
|
||||||
isLoading: boolean
|
isLoading: boolean
|
||||||
|
debug?: boolean
|
||||||
onConnect: (_onConnectCallback?: OnConnectFunc) => void
|
onConnect: (_onConnectCallback?: OnConnectFunc) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,6 +23,15 @@ const NostrContext = createContext<NostrContextType>({
|
|||||||
onConnect: () => null,
|
onConnect: () => null,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const log = (
|
||||||
|
isOn: boolean | undefined,
|
||||||
|
type: "info" | "error" | "warn",
|
||||||
|
...args: unknown[]
|
||||||
|
) => {
|
||||||
|
if (!isOn) return
|
||||||
|
console[type](...args)
|
||||||
|
}
|
||||||
|
|
||||||
export function NostrProvider({
|
export function NostrProvider({
|
||||||
children,
|
children,
|
||||||
relayUrls,
|
relayUrls,
|
||||||
@@ -41,18 +51,21 @@ export function NostrProvider({
|
|||||||
relay.connect()
|
relay.connect()
|
||||||
|
|
||||||
relay.on("connect", () => {
|
relay.on("connect", () => {
|
||||||
|
log(debug, "info", `✅ nostrgg: Connected to ${relayUrl}`)
|
||||||
setIsLoading(false)
|
setIsLoading(false)
|
||||||
onConnectCallback?.(relay)
|
onConnectCallback?.(relay)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Wait for this to be merged: https://github.com/fiatjaf/nostr-tools/pull/69
|
// Wait for this to be merged: https://github.com/fiatjaf/nostr-tools/pull/69
|
||||||
// relay.on("error", () => {
|
// relay.on("error", () => {
|
||||||
|
// log(debug, "error", `❌ nostrgg: Error connecting to ${relayUrl}!`)
|
||||||
// console.log(`Error connecting to ${relay.url}`)
|
// console.log(`Error connecting to ${relay.url}`)
|
||||||
// })
|
// })
|
||||||
})
|
})
|
||||||
}, [onConnectCallback, relayUrls])
|
}, [onConnectCallback, relayUrls])
|
||||||
|
|
||||||
const value: NostrContextType = {
|
const value: NostrContextType = {
|
||||||
|
debug,
|
||||||
isLoading,
|
isLoading,
|
||||||
onConnect: (_onConnectCallback?: OnConnectFunc) => {
|
onConnect: (_onConnectCallback?: OnConnectFunc) => {
|
||||||
if (_onConnectCallback) {
|
if (_onConnectCallback) {
|
||||||
@@ -69,13 +82,14 @@ export function useNostr() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function useNostrEvents({ filter }: { filter: Filter }) {
|
export function useNostrEvents({ filter }: { filter: Filter }) {
|
||||||
const { isLoading, onConnect } = useNostr()
|
const { isLoading, onConnect, debug } = useNostr()
|
||||||
const [events, setEvents] = useState<NostrEvent[]>([])
|
const [events, setEvents] = useState<NostrEvent[]>([])
|
||||||
|
|
||||||
onConnect((relay: Relay) => {
|
onConnect((relay: Relay) => {
|
||||||
const sub = relay.sub([filter], {})
|
const sub = relay.sub([filter], {})
|
||||||
|
|
||||||
sub.on("event", (event: NostrEvent) => {
|
sub.on("event", (event: NostrEvent) => {
|
||||||
|
log(debug, "info", "⬇️ nostrgg: Received event:", event)
|
||||||
setEvents((_events) => {
|
setEvents((_events) => {
|
||||||
return [event, ..._events]
|
return [event, ..._events]
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user