import { AccountContext } from '@lume/shared/accountProvider'; import { ImagePicker } from '@lume/shared/form/imagePicker'; import { RelayContext } from '@lume/shared/relaysProvider'; import { chatContentAtom } from '@lume/stores/chat'; import { FULL_RELAYS } from '@lume/stores/constants'; import { dateToUnix } from '@lume/utils/getDate'; import { useAtom } from 'jotai'; import { useResetAtom } from 'jotai/utils'; import { getEventHash, nip04, signEvent } from 'nostr-tools'; import { useCallback, useContext } from 'react'; export default function FormChat({ receiverPubkey }: { receiverPubkey: string }) { const pool: any = useContext(RelayContext); const activeAccount: any = useContext(AccountContext); const [value, setValue] = useAtom(chatContentAtom); const resetValue = useResetAtom(chatContentAtom); const encryptMessage = useCallback( async (privkey: string) => { return await nip04.encrypt(privkey, receiverPubkey, value); }, [receiverPubkey, value] ); const submitEvent = useCallback(() => { encryptMessage(activeAccount.privkey) .then((encryptedContent) => { const event: any = { content: encryptedContent, created_at: dateToUnix(), kind: 4, pubkey: activeAccount.pubkey, tags: [['p', receiverPubkey]], }; event.id = getEventHash(event); event.sig = signEvent(event, activeAccount.privkey); // publish note pool.publish(event, FULL_RELAYS); // reset state resetValue(); }) .catch(console.error); }, [activeAccount.privkey, activeAccount.pubkey, receiverPubkey, pool, resetValue, encryptMessage]); const handleEnterPress = (e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); submitEvent(); } }; return (