This commit is contained in:
Ren Amamiya
2023-06-26 21:08:36 +07:00
parent b2dd231f00
commit 2f553d8039
13 changed files with 192 additions and 161 deletions

View File

@@ -2,8 +2,13 @@ import NDK, {
NDKConstructorParams,
NDKEvent,
NDKFilter,
NDKKind,
NDKPrivateKeySigner,
} from "@nostr-dev-kit/ndk";
import { RelayContext } from "@shared/relayProvider";
import { FULL_RELAYS } from "@stores/constants";
import { useAccount } from "@utils/hooks/useAccount";
import { useContext } from "react";
export async function initNDK(
relays?: string[],
@@ -39,3 +44,31 @@ export async function prefetchEvents(
});
});
}
export function usePublish() {
const { account } = useAccount();
const ndk = useContext(RelayContext);
if (!ndk.signer) {
const signer = new NDKPrivateKeySigner(account?.privkey);
ndk.signer = signer;
}
function publish({
content,
kind,
tags,
}: { content: string; kind: NDKKind; tags: string[][] }) {
const event = new NDKEvent(ndk);
event.content = content;
event.kind = kind;
event.created_at = Math.floor(Date.now() / 1000);
event.pubkey = account.pubkey;
event.tags = tags;
event.publish();
}
return publish;
}