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

View File

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

View File

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