migrated activeAccount state to rehook

This commit is contained in:
Ren Amamiya
2023-04-11 09:02:39 +07:00
parent 0bfcb10253
commit 4e1dcdc2ce
20 changed files with 72 additions and 85 deletions

View File

@@ -6,6 +6,7 @@ import { noteContentAtom } from '@stores/note';
import { dateToUnix } from '@utils/getDate';
import useLocalStorage from '@rehooks/local-storage';
import { useAtom } from 'jotai';
import { useResetAtom } from 'jotai/utils';
import { getEventHash, signEvent } from 'nostr-tools';
@@ -17,9 +18,9 @@ export default function FormBase() {
const [value, setValue] = useAtom(noteContentAtom);
const resetValue = useResetAtom(noteContentAtom);
const submitEvent = () => {
const activeAccount = JSON.parse(localStorage.getItem('activeAccount'));
const [activeAccount]: any = useLocalStorage('activeAccount');
const submitEvent = () => {
const event: any = {
content: value,
created_at: dateToUnix(),

View File

@@ -3,12 +3,14 @@ import { RelayContext } from '@components/relaysProvider';
import { dateToUnix } from '@utils/getDate';
import useLocalStorage from '@rehooks/local-storage';
import { getEventHash, nip04, signEvent } from 'nostr-tools';
import { useCallback, useContext, useState } from 'react';
export default function FormChat({ receiverPubkey }: { receiverPubkey: string }) {
const [pool, relays]: any = useContext(RelayContext);
const [value, setValue] = useState('');
const [activeAccount]: any = useLocalStorage('activeAccount');
const encryptMessage = useCallback(
async (privkey: string) => {
@@ -18,7 +20,6 @@ export default function FormChat({ receiverPubkey }: { receiverPubkey: string })
);
const submitEvent = useCallback(() => {
const activeAccount = JSON.parse(localStorage.getItem('activeAccount'));
encryptMessage(activeAccount.privkey)
.then((encryptedContent) => {
const event: any = {
@@ -36,7 +37,7 @@ export default function FormChat({ receiverPubkey }: { receiverPubkey: string })
setValue('');
})
.catch(console.error);
}, [encryptMessage, receiverPubkey, pool, relays]);
}, [encryptMessage, activeAccount.privkey, activeAccount.pubkey, receiverPubkey, pool, relays]);
const handleEnterPress = (e) => {
if (e.key === 'Enter' && !e.shiftKey) {

View File

@@ -1,22 +1,19 @@
import { ImageWithFallback } from '@components/imageWithFallback';
import { RelayContext } from '@components/relaysProvider';
import { activeAccountAtom } from '@stores/account';
import { dateToUnix } from '@utils/getDate';
import destr from 'destr';
import { useAtomValue } from 'jotai';
import useLocalStorage from '@rehooks/local-storage';
import { getEventHash, signEvent } from 'nostr-tools';
import { useContext, useState } from 'react';
export default function FormComment({ eventID }: { eventID: any }) {
const [pool, relays]: any = useContext(RelayContext);
const activeAccount: any = useAtomValue(activeAccountAtom);
const [activeAccount]: any = useLocalStorage('activeAccount');
const [value, setValue] = useState('');
const profile = destr(activeAccount.metadata);
const profile = JSON.parse(activeAccount.metadata);
const submitEvent = () => {
const event: any = {