This commit is contained in:
Ren Amamiya 2023-03-21 13:00:32 +07:00
parent c1d818c69d
commit 3ad1fff4f8
5 changed files with 18 additions and 27 deletions

View File

@ -4,6 +4,6 @@ import { createContext, useMemo } from 'react';
export const RelayContext = createContext({});
export default function RelayProvider({ relays, children }: { relays: any; children: React.ReactNode }) {
const value = useMemo(() => new RelayPool(relays, { useEventCache: true, logSubscriptions: false }), [relays]);
const value = useMemo(() => new RelayPool(relays, { useEventCache: false, logSubscriptions: false }), [relays]);
return <RelayContext.Provider value={value}>{children}</RelayContext.Provider>;
}

View File

@ -3,7 +3,7 @@ import { RelayContext } from '@components/contexts/relay';
import { Content } from '@components/note/content';
import useLocalStorage from '@rehooks/local-storage';
import { memo, useCallback, useContext, useMemo, useState } from 'react';
import { memo, useCallback, useContext, useEffect, useState } from 'react';
export const RootNote = memo(function RootNote({ id }: { id: string }) {
const { db }: any = useContext(DatabaseContext);
@ -26,7 +26,7 @@ export const RootNote = memo(function RootNote({ id }: { id: string }) {
const getData = useCallback(async () => {
const result = await db.select(`SELECT * FROM cache_notes WHERE id = "${id}"`);
return result[0];
}, []);
}, [db, id]);
const fetchEvent = useCallback(() => {
relayPool.subscribe(
@ -53,7 +53,7 @@ export const RootNote = memo(function RootNote({ id }: { id: string }) {
);
}, [id, insertDB, relayPool, relays]);
useMemo(() => {
useEffect(() => {
getData()
.then((res) => {
if (res) {

View File

@ -8,16 +8,7 @@ import { useLocalStorage, writeStorage } from '@rehooks/local-storage';
import Image from 'next/image';
import { useRouter } from 'next/router';
import { generatePrivateKey, getEventHash, getPublicKey, nip19, signEvent } from 'nostr-tools';
import {
JSXElementConstructor,
ReactElement,
ReactFragment,
ReactPortal,
useCallback,
useContext,
useMemo,
useState,
} from 'react';
import { JSXElementConstructor, ReactElement, ReactFragment, ReactPortal, useContext, useMemo, useState } from 'react';
import { Config, names, uniqueNamesGenerator } from 'unique-names-generator';
const config: Config = {
@ -42,6 +33,7 @@ export default function Page() {
const npub = nip19.npubEncode(pubKey);
const nsec = nip19.nsecEncode(privKey);
// toggle privatek key
const showPrivateKey = () => {
if (type === 'password') {
setType('text');
@ -49,7 +41,6 @@ export default function Page() {
setType('password');
}
};
// auto-generated profile
const data = useMemo(
() => ({
@ -61,16 +52,16 @@ export default function Page() {
}),
[name]
);
const insertDB = useCallback(async () => {
// insert to database
const insertDB = async () => {
await db.execute(
`INSERT INTO accounts (id, privkey, npub, nsec, metadata) VALUES ("${pubKey}", "${privKey}", "${npub}", "${nsec}", '${JSON.stringify(
data
)}')`
);
}, [data, db, npub, nsec, privKey, pubKey]);
const createAccount = async () => {
};
// build event and broadcast to all relays
const createAccount = () => {
setLoading(true);
// build event
@ -83,7 +74,7 @@ export default function Page() {
};
event.id = getEventHash(event);
event.sig = signEvent(event, privKey);
// insert to database then broadcast
insertDB()
.then(() => {
// publish to relays

View File

@ -78,7 +78,6 @@ export default function Page() {
const arr = follows.includes(pubkey) ? follows.filter((i) => i !== pubkey) : [...follows, pubkey];
setFollows(arr);
};
// insert follow to database
const insertDB = async () => {
// self follow
@ -92,7 +91,6 @@ export default function Page() {
);
});
};
// build event tags
const createTags = () => {
const tags = [];
@ -103,7 +101,6 @@ export default function Page() {
return tags;
};
// commit and publish to relays
const createFollows = () => {
setLoading(true);

View File

@ -45,7 +45,6 @@ export default function Page() {
},
[db, privkey, pubkey]
);
// save follows to database
const insertFollows = useCallback(
async (follows) => {
@ -60,13 +59,13 @@ export default function Page() {
},
[db, pubkey]
);
// submit then redirect to home
const submit = () => {
router.push('/');
};
useEffect(() => {
relayPool.subscribe(
const unsubscribe = relayPool.subscribe(
[
{
authors: [pubkey],
@ -89,6 +88,10 @@ export default function Page() {
console.log(events, relayURL);
}
);
return () => {
unsubscribe();
};
}, [insertAccount, insertFollows, pubkey, relayPool, relays]);
return (