From c912d0371910ba592655f59f68186eebf343fc86 Mon Sep 17 00:00:00 2001 From: highperfocused Date: Fri, 18 Apr 2025 20:11:15 +0200 Subject: [PATCH] add nostr-react prompt --- ...nip-46.prompt.md => nostr-nip46.prompt.md} | 0 .github/prompts/nostr-react.prompt.md | 152 ++++++++++++++++++ 2 files changed, 152 insertions(+) rename .github/prompts/{nostr-nip-46.prompt.md => nostr-nip46.prompt.md} (100%) create mode 100644 .github/prompts/nostr-react.prompt.md diff --git a/.github/prompts/nostr-nip-46.prompt.md b/.github/prompts/nostr-nip46.prompt.md similarity index 100% rename from .github/prompts/nostr-nip-46.prompt.md rename to .github/prompts/nostr-nip46.prompt.md diff --git a/.github/prompts/nostr-react.prompt.md b/.github/prompts/nostr-react.prompt.md new file mode 100644 index 0000000..de1cf9f --- /dev/null +++ b/.github/prompts/nostr-react.prompt.md @@ -0,0 +1,152 @@ +## Installation + +``` +npm install nostr-react +``` + +## Example usage: + +Wrap your app in the NostrProvider: + +```tsx +import { NostrProvider } from "nostr-react"; + +const relayUrls = [ + "wss://nostr-pub.wellorder.net", + "wss://relay.nostr.ch", +]; + +function MyApp() { + return ( + + + + ); +}; +``` + +You can now use the `useNostr` and `useNostrEvents` hooks in your components! + +**Fetching all `text_note` events starting now:** + +```tsx +import { useRef } from "react"; +import { useNostrEvents, dateToUnix } from "nostr-react"; + +const GlobalFeed = () => { + const now = useRef(new Date()); // Make sure current time isn't re-rendered + + const { events } = useNostrEvents({ + filter: { + since: dateToUnix(now.current), // all new events from now + kinds: [1], + }, + }); + + return ( + <> + {events.map((event) => ( +

{event.pubkey} posted: {event.content}

+ ))} + + ); +}; +``` + +**Fetching all `text_note` events from a specific user, since the beginning of time:** + +```tsx +import { useNostrEvents } from "nostr-react"; + +const ProfileFeed = () => { + const { events } = useNostrEvents({ + filter: { + authors: [ + "9c2a6495b4e3de93f3e1cc254abe4078e17c64e5771abc676a5e205b62b1286c", + ], + since: 0, + kinds: [1], + }, + }); + + return ( + <> + {events.map((event) => ( +

{event.pubkey} posted: {event.content}

+ ))} + + ); +}; +``` + +**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 +import { useProfile } from "nostr-react"; + +const Profile = () => { + const { data: userData } = useProfile({ + pubkey, + }); + + return ( + <> +

Name: {userData?.name}

+

Public key: {userData?.npub}

+

Picture URL: {userData?.picture}

+ + ) +} +``` + +**Post a message:** + +```tsx +import { useNostr, dateToUnix } from "nostr-react"; + +import { + type Event as NostrEvent, + getEventHash, + getPublicKey, + signEvent, +} from "nostr-tools"; + +export default function PostButton() { + const { publish } = useNostr(); + + const onPost = async () => { + const privKey = prompt("Paste your private key:"); + + if (!privKey) { + alert("no private key provided"); + return; + } + + const message = prompt("Enter the message you want to send:"); + + if (!message) { + alert("no message provided"); + return; + } + + const event: NostrEvent = { + content: message, + kind: 1, + tags: [], + created_at: dateToUnix(), + pubkey: getPublicKey(privKey), + }; + + event.id = getEventHash(event); + event.sig = signEvent(event, privKey); + + publish(event); + }; + + return ( + + ); +} +``` \ No newline at end of file