snort/packages/system-react/README.md

84 lines
2.3 KiB
Markdown
Raw Normal View History

2023-06-16 19:31:33 +00:00
## @snort/system-react
React hooks for @snort/system
### Available hooks
#### `useRequestBuilder(RequestBuilder)`
The main hook which allows you to subscribe to nostr relays and returns a reactive store.
#### `useUserProfile(pubkey: string | undefined)`
Profile hook, profile loading is automated, this hook will return the profile from cache and also refresh the cache in the background (`stale-while-revalidate`)
#### `useEventFeed(NostrLink)` / `useEventsFeed(Array<NostrLink>)`
2023-12-28 17:40:53 +00:00
A simple hook which can load events using the `NostrLink` class, this class contains one NIP-19 entity `nevent/naddr` etc.
#### `useReactions(id, Array<NostrLink>)`
2023-12-28 17:40:53 +00:00
Loads reactions for a set of events, this can be a set of posts on a profile or an arbitary list of events.
#### `useEventReactions(NostrLink, Array<NostrEvent>)`
2023-12-28 17:40:53 +00:00
Process a set of related events (usually results from `useReactions`) and return likes/dislikes/reposts/zaps
#### `useUserSearch()`
2023-12-28 17:40:53 +00:00
Search for profiles in the profile cache, this also returns exact links if they match
#### `useSystemState(System)`
2023-12-28 17:40:53 +00:00
Hook state of the nostr system
## Example:
2023-07-22 18:37:46 +00:00
2023-06-16 19:31:33 +00:00
```js
2023-07-22 18:37:46 +00:00
import { useMemo } from "react";
2023-09-15 11:48:55 +00:00
import { SnortContext, useRequestBuilder, useUserProfile } from "@snort/system-react";
import { NostrSystem, NoteCollection, RequestBuilder, TaggedNostrEvent } from "@snort/system";
2023-06-16 19:31:33 +00:00
const System = new NostrSystem({});
// some bootstrap relays
2023-07-22 18:37:46 +00:00
["wss://relay.snort.social", "wss://nos.lol"].forEach(r => System.ConnectToRelay(r, { read: true, write: false }));
2023-06-16 19:31:33 +00:00
2023-08-17 18:54:14 +00:00
export function Note({ ev }: { ev: TaggedNostrEvent }) {
2023-09-15 11:48:55 +00:00
const profile = useUserProfile(ev.pubkey);
2023-07-22 18:37:46 +00:00
return (
<div>
Post by: {profile.name ?? profile.display_name}
<p>{ev.content}</p>
2023-06-16 19:31:33 +00:00
</div>
2023-07-22 18:37:46 +00:00
);
2023-06-16 19:31:33 +00:00
}
export function UserPosts(props: { pubkey: string }) {
2023-07-22 18:37:46 +00:00
const sub = useMemo(() => {
const rb = new RequestBuilder("get-posts");
rb.withFilter().authors([props.pubkey]).kinds([1]).limit(10);
return rb;
}, [props.pubkey]);
const data = useRequestBuilder(sub);
2023-07-22 18:37:46 +00:00
return (
<>
{data.map(a => (
2023-07-22 18:37:46 +00:00
<Note ev={a} />
))}
</>
);
2023-06-16 19:31:33 +00:00
}
export function MyApp() {
2023-09-15 11:48:55 +00:00
return (
<SnortContext.Provider value={System}>
<UserPosts pubkey="63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed" />
</SnortContext.Provider>
);
2023-06-16 19:31:33 +00:00
}
2023-07-22 18:37:46 +00:00
```