Use nostr-tools instead of nostr-client

This commit is contained in:
Tristan Edwards 2022-12-25 13:52:01 +01:00
parent 5dc4675605
commit 32465d17c9
5 changed files with 40087 additions and 4139 deletions

View File

@ -5,7 +5,7 @@
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:react/jsx-runtime",
"plugin:@typescript-eslint/recommended",
"prettier"
],

35992
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -68,6 +68,7 @@
"typescript": "^4.9.4"
},
"dependencies": {
"@nostrgg/client": "^0.1.0"
"@nostrgg/client": "^0.1.0",
"nostr-tools": "^1.0.0"
}
}

View File

@ -1,38 +1,25 @@
import React, {
import {
createContext,
ReactNode,
useContext,
useEffect,
useState,
useRef,
} from "react"
import { Relay, Filter, Event as NostrEvent, relayInit } from "nostr-tools"
import { uniqBy } from "./utils"
import {
OnConnectFunc,
initNostr,
OnEventFunc,
SendEventFunc,
Filter,
NostrEvent,
SendMsgType,
SendEvent,
} from "@nostrgg/client"
export * from "@nostrgg/client"
type OnConnectFunc = (relay: Relay) => void
interface NostrContextType {
isLoading: boolean
onConnect: (_onConnectCallback?: OnConnectFunc) => void
onEvent: (_onEventCallback?: OnEventFunc) => void
sendEvent?: (event: SendEvent) => void
}
const NostrContext = createContext<NostrContextType>({
isLoading: true,
onConnect: () => null,
onEvent: () => null,
})
export function NostrProvider({
@ -47,44 +34,31 @@ export function NostrProvider({
const [isLoading, setIsLoading] = useState(true)
let onConnectCallback: null | OnConnectFunc = null
let onEventCallback: null | OnEventFunc = null
const sendEventRef = useRef<SendEventFunc>()
useEffect(() => {
const { sendEvent: _sendEvent } = initNostr({
relayUrls,
onConnect: (url, sendEvent) => {
setIsLoading(false)
relayUrls.forEach(async (relayUrl) => {
const relay = relayInit(relayUrl)
relay.connect()
if (onConnectCallback) {
onConnectCallback(url, sendEvent)
}
},
onEvent: (relayUrl, event) => {
if (onEventCallback) {
onEventCallback(relayUrl, event)
}
},
debug,
relay.on("connect", () => {
setIsLoading(false)
onConnectCallback?.(relay)
})
sendEventRef.current = _sendEvent
}, [onConnectCallback, onEventCallback, relayUrls])
// Wait for this to be merged: https://github.com/fiatjaf/nostr-tools/pull/69
// relay.on("error", () => {
// console.log(`Error connecting to ${relay.url}`)
// })
})
}, [onConnectCallback, relayUrls])
const value: NostrContextType = {
isLoading,
sendEvent: sendEventRef.current,
onConnect: (_onConnectCallback?: OnConnectFunc) => {
if (_onConnectCallback) {
onConnectCallback = _onConnectCallback
}
},
onEvent: (_onEventCallback?: OnEventFunc) => {
if (_onEventCallback) {
onEventCallback = _onEventCallback
}
},
}
return <NostrContext.Provider value={value}>{children}</NostrContext.Provider>
@ -95,18 +69,18 @@ export function useNostr() {
}
export function useNostrEvents({ filter }: { filter: Filter }) {
const { isLoading, sendEvent, onConnect, onEvent } = useNostr()
const { isLoading, onConnect } = useNostr()
const [events, setEvents] = useState<NostrEvent[]>([])
onConnect((url, _sendEvent) => {
_sendEvent([SendMsgType.REQ, filter], url)
})
onConnect((relay: Relay) => {
const sub = relay.sub([filter], {})
onEvent((_relayUrl, event) => {
sub.on("event", (event: NostrEvent) => {
setEvents((_events) => {
return [event, ..._events]
})
})
})
const uniqEvents = events.length > 0 ? uniqBy(events, "id") : []
const sortedEvents = uniqEvents.sort((a, b) => b.created_at - a.created_at)
@ -115,7 +89,5 @@ export function useNostrEvents({ filter }: { filter: Filter }) {
isLoading,
events: sortedEvents,
onConnect,
onEvent,
sendEvent,
}
}

8155
yarn.lock

File diff suppressed because it is too large Load Diff