snort/packages/system-react/README.md

52 lines
1.3 KiB
Markdown
Raw Normal View History

2023-06-16 19:31:33 +00:00
## @snort/system-react
React hooks for @snort/system
Sample:
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-06-16 19:31:33 +00:00
import { useRequestBuilder, useUserProfile } from "@snort/system-react";
2023-08-17 18:54:14 +00:00
import { FlatNoteStore, NostrSystem, RequestBuilder, TaggedNostrEvent } from "@snort/system";
2023-06-16 19:31:33 +00:00
// singleton nostr system class
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-07-22 18:37:46 +00:00
// get profile from cache or request a profile from relays
const profile = useUserProfile(System, ev.pubkey);
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 < FlatNoteStore > (System, FlatNoteStore, sub);
return (
<>
{data.data.map(a => (
<Note ev={a} />
))}
</>
);
2023-06-16 19:31:33 +00:00
}
export function MyApp() {
2023-07-22 18:37:46 +00:00
return <UserPosts pubkey="63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed" />;
2023-06-16 19:31:33 +00:00
}
2023-07-22 18:37:46 +00:00
```