import Note from '@/components/Note' import { Button } from '@/components/ui/button' import { ScrollArea } from '@/components/ui/scroll-area' import { createCommentDraftEvent, createShortTextNoteDraftEvent } from '@/lib/draft-event' import { isTouchDevice } from '@/lib/utils' import { useNostr } from '@/providers/NostrProvider' import postContentCache from '@/services/post-content-cache.service' import { ImageUp, LoaderCircle, Settings, Smile } from 'lucide-react' import { Event, kinds } from 'nostr-tools' import { useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import { toast } from 'sonner' import EmojiPickerDialog from '../EmojiPickerDialog' import Mentions from './Mentions' import { usePostEditor } from './PostEditorProvider' import PostOptions from './PostOptions' import PostTextarea, { TPostTextareaHandle } from './PostTextarea' import SendOnlyToSwitch from './SendOnlyToSwitch' import Uploader from './Uploader' export default function PostContent({ defaultContent = '', parentEvent, close }: { defaultContent?: string parentEvent?: Event close: () => void }) { const { t } = useTranslation() const { publish, checkLogin } = useNostr() const { uploadingFiles, setUploadingFiles } = usePostEditor() const [text, setText] = useState('') const textareaRef = useRef(null) const [posting, setPosting] = useState(false) const [showMoreOptions, setShowMoreOptions] = useState(false) const [addClientTag, setAddClientTag] = useState(false) const [specifiedRelayUrls, setSpecifiedRelayUrls] = useState(undefined) const [mentions, setMentions] = useState([]) const [isNsfw, setIsNsfw] = useState(false) const canPost = !!text && !posting && !uploadingFiles const post = async (e?: React.MouseEvent) => { e?.stopPropagation() checkLogin(async () => { if (!canPost) return setPosting(true) try { const draftEvent = parentEvent && parentEvent.kind !== kinds.ShortTextNote ? await createCommentDraftEvent(text, parentEvent, mentions, { addClientTag, protectedEvent: !!specifiedRelayUrls, isNsfw }) : await createShortTextNoteDraftEvent(text, mentions, { parentEvent, addClientTag, protectedEvent: !!specifiedRelayUrls, isNsfw }) await publish(draftEvent, { specifiedRelayUrls }) postContentCache.clearPostCache({ defaultContent, parentEvent }) close() } catch (error) { if (error instanceof AggregateError) { error.errors.forEach((e) => toast.error(`${t('Failed to post')}: ${e.message}`)) } else if (error instanceof Error) { toast.error(`${t('Failed to post')}: ${error.message}`) } console.error(error) return } finally { setPosting(false) } toast.success(t('Post successful'), { duration: 2000 }) }) } return (
{parentEvent && (
)} post()} />
{ textareaRef.current?.appendText(url + '\n') }} onUploadingChange={(uploading) => setUploadingFiles((prev) => (uploading ? prev + 1 : prev - 1)) } accept="image/*,video/*,audio/*" > {/* I'm not sure why, but after triggering the virtual keyboard, opening the emoji picker drawer causes an issue, the emoji I tap isn't the one that gets inserted. */} {!isTouchDevice() && ( textareaRef.current?.insertText(emoji)}> )}
) }