migrate to ndk
This commit is contained in:
@@ -3,17 +3,15 @@ import { RelayContext } from "@shared/relayProvider";
|
||||
import { useActiveAccount } from "@stores/accounts";
|
||||
import { useChannels } from "@stores/channels";
|
||||
import { useChatMessages, useChats } from "@stores/chats";
|
||||
import { DEFAULT_AVATAR, READONLY_RELAYS } from "@stores/constants";
|
||||
import { dateToUnix } from "@utils/date";
|
||||
import { DEFAULT_AVATAR } from "@stores/constants";
|
||||
import { usePageContext } from "@utils/hooks/usePageContext";
|
||||
import { useProfile } from "@utils/hooks/useProfile";
|
||||
import { sendNativeNotification } from "@utils/notification";
|
||||
import { createNote } from "@utils/storage";
|
||||
import { useContext } from "react";
|
||||
import useSWRSubscription from "swr/subscription";
|
||||
|
||||
export function ActiveAccount({ data }: { data: any }) {
|
||||
const pool: any = useContext(RelayContext);
|
||||
const ndk = useContext(RelayContext);
|
||||
const account = useActiveAccount((state: any) => state.account);
|
||||
|
||||
const pageContext = usePageContext();
|
||||
@@ -35,60 +33,38 @@ export function ActiveAccount({ data }: { data: any }) {
|
||||
() => {
|
||||
const follows = JSON.parse(account.follows);
|
||||
// subscribe to channel
|
||||
const unsubscribe = pool.subscribe(
|
||||
[
|
||||
{
|
||||
kinds: [1, 6],
|
||||
authors: follows,
|
||||
since: dateToUnix(),
|
||||
},
|
||||
{
|
||||
"#p": [data.pubkey],
|
||||
since: lastLogin,
|
||||
},
|
||||
],
|
||||
READONLY_RELAYS,
|
||||
(event) => {
|
||||
switch (event.kind) {
|
||||
case 1:
|
||||
case 6: {
|
||||
createNote(
|
||||
event.id,
|
||||
account.id,
|
||||
event.pubkey,
|
||||
event.kind,
|
||||
event.tags,
|
||||
event.content,
|
||||
event.created_at,
|
||||
);
|
||||
break;
|
||||
const sub = ndk.subscribe({
|
||||
"#p": [data.pubkey],
|
||||
since: lastLogin,
|
||||
});
|
||||
|
||||
sub.addListener("event", (event) => {
|
||||
switch (event.kind) {
|
||||
case 4:
|
||||
if (!isChatPage) {
|
||||
// save
|
||||
saveChat(data.pubkey, event);
|
||||
// update state
|
||||
notifyChat(event.pubkey);
|
||||
// send native notifiation
|
||||
sendNativeNotification("You've received new message");
|
||||
}
|
||||
case 4:
|
||||
if (!isChatPage) {
|
||||
// save
|
||||
saveChat(data.pubkey, event);
|
||||
// update state
|
||||
notifyChat(event.pubkey);
|
||||
// send native notifiation
|
||||
sendNativeNotification("You've received new message");
|
||||
}
|
||||
break;
|
||||
case 42:
|
||||
if (!isChannelPage) {
|
||||
// update state
|
||||
notifyChannel(event);
|
||||
// send native notifiation
|
||||
sendNativeNotification(event.content);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
},
|
||||
);
|
||||
break;
|
||||
case 42:
|
||||
if (!isChannelPage) {
|
||||
// update state
|
||||
notifyChannel(event);
|
||||
// send native notifiation
|
||||
sendNativeNotification(event.content);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
return () => {
|
||||
unsubscribe();
|
||||
sub.stop();
|
||||
};
|
||||
},
|
||||
);
|
||||
@@ -96,7 +72,7 @@ export function ActiveAccount({ data }: { data: any }) {
|
||||
return (
|
||||
<button type="button" className="relative h-11 w-11 overflow-hidden">
|
||||
<Image
|
||||
src={user?.picture || DEFAULT_AVATAR}
|
||||
src={user?.image || DEFAULT_AVATAR}
|
||||
alt={data.npub}
|
||||
className="h-11 w-11 rounded-md object-cover"
|
||||
/>
|
||||
|
||||
@@ -9,7 +9,7 @@ export function InactiveAccount({ data }: { data: any }) {
|
||||
return (
|
||||
<div className="relative h-11 w-11 shrink rounded-md">
|
||||
<Image
|
||||
src={user?.picture || DEFAULT_AVATAR}
|
||||
src={user?.image || DEFAULT_AVATAR}
|
||||
alt={data.npub}
|
||||
className="h-11 w-11 rounded-lg object-cover"
|
||||
/>
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { NDKEvent, NDKPrivateKeySigner } from "@nostr-dev-kit/ndk";
|
||||
import { ImageUploader } from "@shared/composer/imageUploader";
|
||||
import { TrashIcon } from "@shared/icons";
|
||||
import { RelayContext } from "@shared/relayProvider";
|
||||
import { WRITEONLY_RELAYS } from "@stores/constants";
|
||||
import { dateToUnix } from "@utils/date";
|
||||
import { getEventHash, getSignature } from "nostr-tools";
|
||||
import { useCallback, useContext, useMemo, useState } from "react";
|
||||
import { Node, Transforms, createEditor } from "slate";
|
||||
import { withHistory } from "slate-history";
|
||||
@@ -59,12 +58,13 @@ const ImagePreview = ({
|
||||
};
|
||||
|
||||
export function Post({ pubkey, privkey }: { pubkey: string; privkey: string }) {
|
||||
const pool: any = useContext(RelayContext);
|
||||
const ndk = useContext(RelayContext);
|
||||
|
||||
const editor = useMemo(
|
||||
() => withReact(withImages(withHistory(createEditor()))),
|
||||
[],
|
||||
);
|
||||
|
||||
const [content, setContent] = useState<Node[]>([
|
||||
{
|
||||
children: [
|
||||
@@ -82,20 +82,19 @@ export function Post({ pubkey, privkey }: { pubkey: string; privkey: string }) {
|
||||
const submit = () => {
|
||||
// serialize content
|
||||
const serializedContent = serialize(content);
|
||||
console.log(serializedContent);
|
||||
|
||||
const event: any = {
|
||||
content: serializedContent,
|
||||
created_at: dateToUnix(),
|
||||
kind: 1,
|
||||
pubkey: pubkey,
|
||||
tags: [],
|
||||
};
|
||||
event.id = getEventHash(event);
|
||||
event.sig = getSignature(event, privkey);
|
||||
const signer = new NDKPrivateKeySigner(privkey);
|
||||
ndk.signer = signer;
|
||||
|
||||
// publish note
|
||||
pool.publish(event, WRITEONLY_RELAYS);
|
||||
const event = new NDKEvent(ndk);
|
||||
event.kind = 1;
|
||||
event.content = serializedContent;
|
||||
event.created_at = dateToUnix();
|
||||
event.pubkey = pubkey;
|
||||
event.tags = [];
|
||||
|
||||
// publish event
|
||||
event.publish();
|
||||
};
|
||||
|
||||
const renderElement = useCallback((props: any) => {
|
||||
|
||||
@@ -9,7 +9,7 @@ export function User({ pubkey }: { pubkey: string }) {
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="h-8 w-8 shrink-0 overflow-hidden rounded bg-zinc-900">
|
||||
<Image
|
||||
src={user?.picture || DEFAULT_AVATAR}
|
||||
src={user?.image || DEFAULT_AVATAR}
|
||||
alt={pubkey}
|
||||
className="h-8 w-8 object-cover"
|
||||
loading="auto"
|
||||
|
||||
@@ -1,16 +1,10 @@
|
||||
import { FULL_RELAYS } from "@stores/constants";
|
||||
import { RelayPool } from "nostr-relaypool";
|
||||
import { initNDK } from "@libs/ndk";
|
||||
import NDK from "@nostr-dev-kit/ndk";
|
||||
import { createContext } from "react";
|
||||
|
||||
export const RelayContext = createContext({});
|
||||
|
||||
const pool = new RelayPool(FULL_RELAYS, {
|
||||
useEventCache: false,
|
||||
subscriptionCache: true,
|
||||
logErrorsAndNotices: false,
|
||||
logSubscriptions: false,
|
||||
});
|
||||
export const RelayContext = createContext<NDK>(null);
|
||||
const ndk = await initNDK();
|
||||
|
||||
export function RelayProvider({ children }: { children: React.ReactNode }) {
|
||||
return <RelayContext.Provider value={pool}>{children}</RelayContext.Provider>;
|
||||
return <RelayContext.Provider value={ndk}>{children}</RelayContext.Provider>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user