import { NDKSubscriptionCacheUsage } from '@nostr-dev-kit/ndk' import { FALLBACK_PROFILE_IMAGE } from '../../constants' import { useAppSelector } from 'hooks' import { useProfile } from 'hooks/useProfile' import { Navigate, useNavigation, useSubmit } from 'react-router-dom' import { appRoutes } from 'routes' import { useEffect, useRef, useState } from 'react' import { adjustTextareaHeight } from 'utils' import { NotePreview } from './NotePreview' interface NoteSubmitProps { initialContent?: string | undefined handleClose?: () => void | undefined } export const NoteSubmit = ({ initialContent, handleClose }: NoteSubmitProps) => { const navigation = useNavigation() const userState = useAppSelector((state) => state.user) const profile = useProfile(userState.user?.pubkey as string | undefined, { cacheUsage: NDKSubscriptionCacheUsage.PARALLEL }) const [content, setContent] = useState(initialContent ?? '') const [nsfw, setNsfw] = useState(false) const [showPreview, setShowPreview] = useState(!!initialContent) const image = profile?.image || FALLBACK_PROFILE_IMAGE const ref = useRef(null) const submit = useSubmit() useEffect(() => { if (ref.current && !!initialContent) { adjustTextareaHeight(ref.current) ref.current.focus() } }, [initialContent]) const handleContentChange = ( event: React.ChangeEvent ) => { setContent(event.currentTarget.value) adjustTextareaHeight(event.currentTarget) } const handleFormSubmit = async (event: React.FormEvent) => { event.preventDefault() const formSubmit = { content, nsfw } // Reset form setContent('') setNsfw(false) submit(JSON.stringify(formSubmit), { method: 'post', encType: 'application/json' }) } const handlePreviewToggle = () => { setShowPreview((prev) => !prev) } if (!userState.user?.pubkey) return return ( <>