nostr-react/README.md

160 lines
3.1 KiB
Markdown
Raw Normal View History

2022-12-20 09:07:49 +00:00
<p align="center">
2022-12-25 17:03:06 +01:00
<b>nostr-react</b>
2022-12-20 09:07:49 +00:00
</p>
<p align="center">
React Hooks for Nostr ✨
</p>
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-25 17:03:06 +01:00
npm install nostr-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
2022-12-25 17:03:06 +01:00
import { NostrProvider } from "nostr-react";
2022-12-20 05:21:07 +00:00
2022-12-20 06:40:26 +00:00
const relayUrls = [
"wss://nostr-pub.wellorder.net",
2022-12-27 23:27:50 +01:00
"wss://relay.nostr.ch",
2022-12-20 06:40:26 +00:00
];
2022-12-20 05:21:07 +00:00
2022-12-20 06:40:26 +00:00
function MyApp() {
return (
2022-12-27 23:27:50 +01:00
<NostrProvider relayUrls={relayUrls} debug={true}>
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
2022-12-27 23:34:25 +01:00
import { useRef } from "react";
2022-12-25 17:03:06 +01:00
import { useNostrEvents, dateToUnix } from "nostr-react";
2022-12-20 06:40:26 +00:00
const GlobalFeed = () => {
2022-12-27 23:27:50 +01:00
const now = useRef(new Date()); // Make sure current time isn't re-rendered
2022-12-27 23:34:25 +01:00
const { events } = useNostrEvents({
2022-12-20 06:40:26 +00:00
filter: {
2022-12-27 23:27:50 +01:00
since: dateToUnix(now.current), // all new events from now
2022-12-25 17:03:06 +01:00
kinds: [1],
2022-12-20 06:40:26 +00:00
},
});
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
2022-12-25 17:56:06 +01:00
import { useNostrEvents } from "nostr-react";
2022-12-20 06:40:26 +00:00
const ProfileFeed = () => {
2022-12-27 23:27:50 +01:00
const { events } = useNostrEvents({
2022-12-20 06:40:26 +00:00
filter: {
authors: [
"9c2a6495b4e3de93f3e1cc254abe4078e17c64e5771abc676a5e205b62b1286c",
],
since: 0,
2022-12-25 17:03:06 +01:00
kinds: [1],
2022-12-20 06:40:26 +00:00
},
});
return (
<>
{events.map((event) => (
<p key={event.id}>{event.pubkey} posted: {event.content}</p>
))}
</>
);
};
2022-12-20 05:21:07 +00:00
```
2023-01-02 10:53:25 +01:00
**Fetching user profiles**
Use the `useProfile` hook to render user profiles. You can use this in multiple components at once (for example, rendering a name and avatar for each message in a chat), the hook will automatically use *batching* to prevent errors where a client sends too many requests at once. 🎉
```tsx
2023-01-02 10:55:36 +01:00
import { useProfile } from "nostr-react";
2023-01-02 10:53:25 +01:00
const Profile = () => {
2023-01-02 10:55:36 +01:00
const { data: userData } = useProfile({
2023-01-02 10:53:25 +01:00
pubkey,
});
return (
<>
<p>Name: userData?.name</p>
<p>Public key: userData?.npub</p>
<p>Picture URL: userData?.avatarUrl</p>
</>
)
}
```
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
2022-12-25 17:56:06 +01:00
import { useNostr, dateToUnix } from "nostr-react";
2022-12-20 05:21:07 +00:00
2022-12-25 17:03:06 +01:00
import {
type Event as NostrEvent,
getEventHash,
getPublicKey,
signEvent,
} from "nostr-tools";
2022-12-20 06:40:26 +00:00
export default function PostButton() {
2022-12-25 17:03:06 +01:00
const { publish } = useNostr();
2022-12-20 05:21:07 +00:00
2022-12-20 06:40:26 +00:00
const onPost = async () => {
2022-12-25 17:03:06 +01:00
const privKey = prompt("Paste your private key:");
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-25 17:03:06 +01:00
const message = prompt("Enter the message you want to send:");
if (!message) {
alert("no message provided");
return;
}
const event: NostrEvent = {
content: message,
kind: 1,
2022-12-20 06:40:26 +00:00
tags: [],
2022-12-25 17:56:06 +01:00
created_at: dateToUnix(),
2022-12-25 17:03:06 +01:00
pubkey: getPublicKey(privKey),
2022-12-20 06:40:26 +00:00
};
2022-12-20 05:21:07 +00:00
2022-12-25 17:03:06 +01:00
event.id = getEventHash(event);
event.sig = signEvent(event, privKey);
2022-12-20 05:21:07 +00:00
2022-12-25 17:03:06 +01:00
publish(event);
2022-12-20 06:40:26 +00:00
};
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
```