add ndk provider

This commit is contained in:
Ren Amamiya
2023-07-09 07:21:41 +07:00
parent 0109ec28c4
commit 24807b2758
26 changed files with 178 additions and 106 deletions

View File

@@ -1,14 +1,12 @@
import { useQuery } from '@tanstack/react-query';
import { useContext } from 'react';
import { useNDK } from '@libs/ndk/provider';
import { createNote, getNoteByID } from '@libs/storage';
import { RelayContext } from '@shared/relayProvider';
import { parser } from '@utils/parser';
export function useEvent(id: string) {
const ndk = useContext(RelayContext);
const { ndk } = useNDK();
const { status, data, error, isFetching } = useQuery(
['note', id],
async () => {

View File

@@ -1,12 +1,10 @@
import { useQuery } from '@tanstack/react-query';
import { useContext } from 'react';
import { useNDK } from '@libs/ndk/provider';
import { createMetadata, getUserMetadata } from '@libs/storage';
import { RelayContext } from '@shared/relayProvider';
export function useProfile(pubkey: string, fallback?: string) {
const ndk = useContext(RelayContext);
const { ndk } = useNDK();
const {
status,
data: user,

View File

@@ -0,0 +1,36 @@
import { NDKEvent, NDKKind, NDKPrivateKeySigner } from '@nostr-dev-kit/ndk';
import { useNDK } from '@libs/ndk/provider';
import { useAccount } from '@utils/hooks/useAccount';
export function usePublish() {
const { ndk } = useNDK();
const { account } = useAccount();
const publish = async ({
content,
kind,
tags,
}: {
content: string;
kind: NDKKind;
tags: string[][];
}): Promise<NDKEvent> => {
const event = new NDKEvent(ndk);
const signer = new NDKPrivateKeySigner(account.privkey);
event.content = content;
event.kind = kind;
event.created_at = Math.floor(Date.now() / 1000);
event.pubkey = account.pubkey;
event.tags = tags;
await event.sign(signer);
await event.publish();
return event;
};
return publish;
}

View File

@@ -1,22 +1,19 @@
import { NDKEvent, NDKFilter } from '@nostr-dev-kit/ndk';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useContext } from 'react';
import { usePublish } from '@libs/ndk';
import { useNDK } from '@libs/ndk/provider';
import { createNote } from '@libs/storage';
import { RelayContext } from '@shared/relayProvider';
import { dateToUnix, getHourAgo } from '@utils/date';
import { useAccount } from '@utils/hooks/useAccount';
import { usePublish } from '@utils/hooks/usePublish';
import { nip02ToArray } from '@utils/transform';
import { useAccount } from './useAccount';
export function useSocial() {
const ndk = useContext(RelayContext);
const queryClient = useQueryClient();
const publish = usePublish();
const { ndk } = useNDK();
const { account } = useAccount();
const { status, data: userFollows } = useQuery(
['userFollows', account.pubkey],