2022-12-20 06:40:26 +00:00
|
|
|
# @nostrgg/react
|
2022-12-20 05:21:07 +00:00
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
React Hooks for Nostr
|
2022-12-20 05:21:07 +00:00
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
## Installation
|
2022-12-20 05:21:07 +00:00
|
|
|
|
|
|
|
```
|
2022-12-20 06:40:26 +00:00
|
|
|
npm install @nostrgg/react
|
2022-12-20 05:21:07 +00:00
|
|
|
```
|
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
## Example usage:
|
2022-12-20 05:21:07 +00:00
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
Wrap your app in the NostrProvider:
|
2022-12-20 05:21:07 +00:00
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
```tsx
|
|
|
|
import { NostrProvider } from "@nostrgg/react";
|
2022-12-20 05:21:07 +00:00
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
const relayUrls = [
|
|
|
|
"wss://nostr-pub.wellorder.net",
|
|
|
|
"wss://nostr-relay.untethr.me",
|
|
|
|
];
|
2022-12-20 05:21:07 +00:00
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
function MyApp() {
|
|
|
|
return (
|
2022-12-20 08:33:21 +00:00
|
|
|
<NostrProvider relayUrls={relayUrls} debug>
|
2022-12-20 06:40:26 +00:00
|
|
|
<App />
|
|
|
|
</NostrProvider>
|
|
|
|
);
|
|
|
|
};
|
2022-12-20 05:21:07 +00:00
|
|
|
```
|
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
You can now use the `useNostr` and `useNostrEvents` hooks in your components!
|
|
|
|
|
|
|
|
**Fetching all `text_note` events starting now:**
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
import { Kind, useNostrEvents, dateToUnix } from "@nostrgg/react";
|
|
|
|
|
|
|
|
const GlobalFeed = () => {
|
|
|
|
const { isLoading, events } = useNostrEvents({
|
|
|
|
filter: {
|
|
|
|
kinds: [Kind.TextNote],
|
|
|
|
since: dateToUnix(new Date()), // all new events from now
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{events.map((event) => (
|
|
|
|
<p key={event.id}>{event.pubkey} posted: {event.content}</p>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2022-12-20 05:21:07 +00:00
|
|
|
```
|
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
**Fetching all `text_note` events from a specific user, since the beginning of time:**
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
import { Kind, useNostrEvents, dateToUnix } from "@nostrgg/react";
|
|
|
|
|
|
|
|
const ProfileFeed = () => {
|
|
|
|
const { events } = useNostrEvents({
|
|
|
|
filter: {
|
|
|
|
authors: [
|
|
|
|
"9c2a6495b4e3de93f3e1cc254abe4078e17c64e5771abc676a5e205b62b1286c",
|
|
|
|
],
|
|
|
|
since: 0,
|
|
|
|
kinds: [Kind.TextNote],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{events.map((event) => (
|
|
|
|
<p key={event.id}>{event.pubkey} posted: {event.content}</p>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2022-12-20 05:21:07 +00:00
|
|
|
```
|
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
**Post a message:**
|
2022-12-20 05:21:07 +00:00
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
```tsx
|
|
|
|
import {
|
|
|
|
generateSignedEvent,
|
|
|
|
Kind,
|
|
|
|
SendMsgType,
|
|
|
|
useNostr,
|
|
|
|
} from "@nostrgg/react";
|
2022-12-20 05:21:07 +00:00
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
export default function PostButton() {
|
|
|
|
const { sendEvent } = useNostr();
|
2022-12-20 05:21:07 +00:00
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
const onPost = async () => {
|
|
|
|
const privKey = prompt("Paste your private key here:");
|
2022-12-20 05:21:07 +00:00
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
if (!privKey) {
|
|
|
|
alert("no private key provided");
|
|
|
|
return;
|
|
|
|
}
|
2022-12-20 05:21:07 +00:00
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
const event = {
|
|
|
|
content: "Hello world!",
|
|
|
|
kind: Kind.TextNote,
|
|
|
|
tags: [],
|
|
|
|
};
|
2022-12-20 05:21:07 +00:00
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
const signedEvent = await generateSignedEvent(event, privKey);
|
2022-12-20 05:21:07 +00:00
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
sendEvent?.([SendMsgType.EVENT, signedEvent]);
|
|
|
|
};
|
2022-12-20 05:21:07 +00:00
|
|
|
|
2022-12-20 06:40:26 +00:00
|
|
|
return (
|
|
|
|
<Button onClick={onPost}>Post a message!</Button>
|
|
|
|
);
|
|
|
|
}
|
2022-12-20 05:21:07 +00:00
|
|
|
```
|