snort/packages/system-react/example/example.tsx

47 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-07-22 18:37:46 +00:00
import { useMemo } from "react";
2023-08-24 14:25:54 +00:00
import { SnortContext, useRequestBuilder, useUserProfile } from "../src";
2023-06-16 19:31:33 +00:00
import { NostrSystem, 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-08-24 14:25:54 +00:00
const profile = useUserProfile(ev.pubkey);
2023-06-16 19:31:33 +00:00
2023-07-22 18:37:46 +00:00
return (
<div>
Post by: {profile?.name ?? profile?.display_name}
2023-07-22 18:37:46 +00:00
<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-08-24 15:55:36 +00:00
return (
<SnortContext.Provider value={System}>
<UserPosts pubkey="63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed" />
</SnortContext.Provider>
);
2023-07-22 18:37:46 +00:00
}