tmp
This commit is contained in:
@ -3,54 +3,49 @@
|
||||
React hooks for @snort/system
|
||||
|
||||
Sample:
|
||||
|
||||
```js
|
||||
import { useMemo } from "react"
|
||||
import { useMemo } from "react";
|
||||
import { useRequestBuilder, useUserProfile } from "@snort/system-react";
|
||||
import { FlatNoteStore, NostrSystem, RequestBuilder, TaggedRawEvent } from "@snort/system"
|
||||
import { FlatNoteStore, NostrSystem, RequestBuilder, TaggedRawEvent } from "@snort/system";
|
||||
|
||||
// singleton nostr system class
|
||||
const System = new NostrSystem({});
|
||||
|
||||
// some bootstrap relays
|
||||
[
|
||||
"wss://relay.snort.social",
|
||||
"wss://nos.lol"
|
||||
].forEach(r => System.ConnectToRelay(r, { read: true, write: false }));
|
||||
["wss://relay.snort.social", "wss://nos.lol"].forEach(r => System.ConnectToRelay(r, { read: true, write: false }));
|
||||
|
||||
export function Note({ ev }: { ev: TaggedRawEvent }) {
|
||||
// get profile from cache or request a profile from relays
|
||||
const profile = useUserProfile(System, ev.pubkey);
|
||||
// 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>
|
||||
return (
|
||||
<div>
|
||||
Post by: {profile.name ?? profile.display_name}
|
||||
<p>{ev.content}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function UserPosts(props: { pubkey: string }) {
|
||||
const sub = useMemo(() => {
|
||||
const rb = new RequestBuilder("get-posts");
|
||||
rb.withFilter()
|
||||
.authors([props.pubkey])
|
||||
.kinds([1])
|
||||
.limit(10);
|
||||
const sub = useMemo(() => {
|
||||
const rb = new RequestBuilder("get-posts");
|
||||
rb.withFilter().authors([props.pubkey]).kinds([1]).limit(10);
|
||||
|
||||
return rb;
|
||||
}, [props.pubkey]);
|
||||
return rb;
|
||||
}, [props.pubkey]);
|
||||
|
||||
const data = useRequestBuilder<FlatNoteStore>(System, FlatNoteStore, sub);
|
||||
return (
|
||||
<>
|
||||
{data.data.map(a => <Note ev={a} />)}
|
||||
</>
|
||||
)
|
||||
const data = useRequestBuilder < FlatNoteStore > (System, FlatNoteStore, sub);
|
||||
return (
|
||||
<>
|
||||
{data.data.map(a => (
|
||||
<Note ev={a} />
|
||||
))}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export function MyApp() {
|
||||
return (
|
||||
<UserPosts pubkey="63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed" />
|
||||
)
|
||||
return <UserPosts pubkey="63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed" />;
|
||||
}
|
||||
```
|
||||
```
|
||||
|
@ -1,48 +1,42 @@
|
||||
import { useMemo } from "react"
|
||||
import { useMemo } from "react";
|
||||
import { useRequestBuilder, useUserProfile } from "../src";
|
||||
|
||||
import { FlatNoteStore, NostrSystem, RequestBuilder, TaggedRawEvent } from "@snort/system"
|
||||
import { FlatNoteStore, NostrSystem, RequestBuilder, TaggedRawEvent } from "@snort/system";
|
||||
|
||||
const System = new NostrSystem({});
|
||||
|
||||
// some bootstrap relays
|
||||
[
|
||||
"wss://relay.snort.social",
|
||||
"wss://nos.lol"
|
||||
].forEach(r => System.ConnectToRelay(r, { read: true, write: false }));
|
||||
["wss://relay.snort.social", "wss://nos.lol"].forEach(r => System.ConnectToRelay(r, { read: true, write: false }));
|
||||
|
||||
export function Note({ ev }: { ev: TaggedRawEvent }) {
|
||||
const profile = useUserProfile(System, ev.pubkey);
|
||||
const profile = useUserProfile(System, ev.pubkey);
|
||||
|
||||
return <div>
|
||||
Post by: {profile.name ?? profile.display_name}
|
||||
<p>
|
||||
{ev.content}
|
||||
</p>
|
||||
return (
|
||||
<div>
|
||||
Post by: {profile.name ?? profile.display_name}
|
||||
<p>{ev.content}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function UserPosts(props: { pubkey: string }) {
|
||||
const sub = useMemo(() => {
|
||||
const rb = new RequestBuilder("get-posts");
|
||||
rb.withFilter()
|
||||
.authors([props.pubkey])
|
||||
.kinds([1])
|
||||
.limit(10);
|
||||
const sub = useMemo(() => {
|
||||
const rb = new RequestBuilder("get-posts");
|
||||
rb.withFilter().authors([props.pubkey]).kinds([1]).limit(10);
|
||||
|
||||
return rb;
|
||||
}, [props.pubkey]);
|
||||
return rb;
|
||||
}, [props.pubkey]);
|
||||
|
||||
const data = useRequestBuilder<FlatNoteStore>(System, FlatNoteStore, sub);
|
||||
return (
|
||||
<>
|
||||
{data.data.map(a => <Note ev={a} />)}
|
||||
</>
|
||||
)
|
||||
const data = useRequestBuilder<FlatNoteStore>(System, FlatNoteStore, sub);
|
||||
return (
|
||||
<>
|
||||
{data.data.map(a => (
|
||||
<Note ev={a} />
|
||||
))}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export function MyApp() {
|
||||
return (
|
||||
<UserPosts pubkey="63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed" />
|
||||
)
|
||||
}
|
||||
return <UserPosts pubkey="63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed" />;
|
||||
}
|
||||
|
@ -20,4 +20,4 @@
|
||||
"@snort/system": "^1.0.16",
|
||||
"@snort/shared": "^1.0.4"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,3 @@
|
||||
export * from "./useRequestBuilder";
|
||||
export * from "./useSystemState";
|
||||
export * from "./useUserProfile";
|
||||
export * from "./useUserProfile";
|
||||
|
@ -7,7 +7,7 @@ import { unwrap } from "@snort/shared";
|
||||
*/
|
||||
const useRequestBuilder = <TStore extends NoteStore, TSnapshot = ReturnType<TStore["getSnapshotData"]>>(
|
||||
system: SystemInterface,
|
||||
type: { new(): TStore },
|
||||
type: { new (): TStore },
|
||||
rb: RequestBuilder | null
|
||||
) => {
|
||||
const subscribe = (onChanged: () => void) => {
|
||||
@ -37,4 +37,4 @@ const useRequestBuilder = <TStore extends NoteStore, TSnapshot = ReturnType<TSto
|
||||
);
|
||||
};
|
||||
|
||||
export { useRequestBuilder };
|
||||
export { useRequestBuilder };
|
||||
|
@ -16,7 +16,7 @@ export function useUserProfile(system: NostrSystem, pubKey?: HexKey): MetadataCa
|
||||
if (pubKey) {
|
||||
system.ProfileLoader.UntrackMetadata(pubKey);
|
||||
}
|
||||
}
|
||||
};
|
||||
},
|
||||
() => system.ProfileLoader.Cache.getFromCache(pubKey)
|
||||
);
|
||||
|
Reference in New Issue
Block a user