import { NDKEvent, NDKPrivateKeySigner } from '@nostr-dev-kit/ndk'; import { useContext, useState } from 'react'; import { UserReply } from '@app/channel/components/messages/userReply'; import { useNDK } from '@libs/ndk/provider'; import { CancelIcon, EnterIcon } from '@shared/icons'; import { MediaUploader } from '@shared/mediaUploader'; import { useChannelMessages } from '@stores/channels'; import { dateToUnix } from '@utils/date'; import { useAccount } from '@utils/hooks/useAccount'; export function ChannelMessageForm({ channelID }: { channelID: string }) { const { ndk } = useNDK(); const [value, setValue] = useState(''); const [replyTo, closeReply] = useChannelMessages((state: any) => [ state.replyTo, state.closeReply, ]); const { account } = useAccount(); const submit = () => { let tags: string[][]; if (replyTo.id !== null) { tags = [ ['e', channelID, '', 'root'], ['e', replyTo.id, '', 'reply'], ['p', replyTo.pubkey, ''], ]; } else { tags = [['e', channelID, '', 'root']]; } const signer = new NDKPrivateKeySigner(account.privkey); ndk.signer = signer; const event = new NDKEvent(ndk); // build event event.content = value; event.kind = 42; event.created_at = dateToUnix(); event.pubkey = account.pubkey; event.tags = tags; // publish event event.publish(); // reset state setValue(''); }; const handleEnterPress = (e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); submit(); } }; const stopReply = () => { closeReply(); }; return (
{replyTo.id && (
{replyTo.content}
)}