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

8155
yarn.lock

File diff suppressed because it is too large Load Diff