2025-02-14 13:15:10 +01:00
|
|
|
import { NDKSubscriptionCacheUsage } from '@nostr-dev-kit/ndk'
|
|
|
|
import { FALLBACK_PROFILE_IMAGE } from '../../constants'
|
2025-02-19 19:19:42 +01:00
|
|
|
import { useAppSelector, useLocalCache } from 'hooks'
|
2025-02-14 13:15:10 +01:00
|
|
|
import { useProfile } from 'hooks/useProfile'
|
2025-02-14 17:29:06 +01:00
|
|
|
import { Navigate, useNavigation, useSubmit } from 'react-router-dom'
|
2025-02-14 13:15:10 +01:00
|
|
|
import { appRoutes } from 'routes'
|
2025-02-14 13:50:22 +01:00
|
|
|
import { useEffect, useRef, useState } from 'react'
|
2025-02-19 19:19:42 +01:00
|
|
|
import { adjustTextareaHeight, NOTE_DRAFT_CACHE_KEY } from 'utils'
|
2025-02-14 13:15:10 +01:00
|
|
|
import { NotePreview } from './NotePreview'
|
2025-02-19 19:19:42 +01:00
|
|
|
import { NoteSubmitForm } from 'types'
|
2025-02-14 13:15:10 +01:00
|
|
|
|
|
|
|
interface NoteSubmitProps {
|
|
|
|
initialContent?: string | undefined
|
2025-02-14 13:50:22 +01:00
|
|
|
handleClose?: () => void | undefined
|
2025-02-14 13:15:10 +01:00
|
|
|
}
|
|
|
|
|
2025-02-14 13:50:22 +01:00
|
|
|
export const NoteSubmit = ({
|
|
|
|
initialContent,
|
|
|
|
handleClose
|
|
|
|
}: NoteSubmitProps) => {
|
2025-02-14 17:29:06 +01:00
|
|
|
const navigation = useNavigation()
|
2025-02-14 13:15:10 +01:00
|
|
|
const userState = useAppSelector((state) => state.user)
|
|
|
|
const profile = useProfile(userState.user?.pubkey as string | undefined, {
|
|
|
|
cacheUsage: NDKSubscriptionCacheUsage.PARALLEL
|
|
|
|
})
|
2025-02-19 19:19:42 +01:00
|
|
|
const [cache, setCache] = useLocalCache<NoteSubmitForm>(NOTE_DRAFT_CACHE_KEY)
|
|
|
|
const [content, setContent] = useState(initialContent ?? cache?.content ?? '')
|
|
|
|
const [nsfw, setNsfw] = useState(cache?.nsfw ?? false)
|
2025-02-14 13:50:22 +01:00
|
|
|
const [showPreview, setShowPreview] = useState(!!initialContent)
|
2025-02-14 13:15:10 +01:00
|
|
|
const image = profile?.image || FALLBACK_PROFILE_IMAGE
|
2025-02-14 13:50:22 +01:00
|
|
|
const ref = useRef<HTMLTextAreaElement>(null)
|
2025-02-14 13:15:10 +01:00
|
|
|
const submit = useSubmit()
|
|
|
|
|
2025-02-14 13:50:22 +01:00
|
|
|
useEffect(() => {
|
2025-02-19 19:19:42 +01:00
|
|
|
if (ref.current && (!!initialContent || !!cache?.content)) {
|
2025-02-14 13:50:22 +01:00
|
|
|
adjustTextareaHeight(ref.current)
|
|
|
|
ref.current.focus()
|
|
|
|
}
|
2025-02-19 19:19:42 +01:00
|
|
|
}, [cache?.content, initialContent])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setCache({
|
|
|
|
content,
|
|
|
|
nsfw
|
|
|
|
})
|
|
|
|
}, [content, nsfw, setCache])
|
2025-02-14 13:50:22 +01:00
|
|
|
|
2025-02-14 13:15:10 +01:00
|
|
|
const handleContentChange = (
|
|
|
|
event: React.ChangeEvent<HTMLTextAreaElement>
|
|
|
|
) => {
|
|
|
|
setContent(event.currentTarget.value)
|
|
|
|
adjustTextareaHeight(event.currentTarget)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleFormSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
|
|
|
|
event.preventDefault()
|
|
|
|
const formSubmit = {
|
2025-02-19 21:22:56 +01:00
|
|
|
intent: 'submit',
|
|
|
|
data: {
|
|
|
|
content,
|
|
|
|
nsfw
|
|
|
|
}
|
2025-02-14 13:15:10 +01:00
|
|
|
}
|
2025-02-14 17:39:49 +01:00
|
|
|
|
|
|
|
// Reset form
|
|
|
|
setContent('')
|
|
|
|
setNsfw(false)
|
|
|
|
|
2025-02-14 13:15:10 +01:00
|
|
|
submit(JSON.stringify(formSubmit), {
|
|
|
|
method: 'post',
|
|
|
|
encType: 'application/json'
|
|
|
|
})
|
2025-02-14 17:40:49 +01:00
|
|
|
|
|
|
|
typeof handleClose === 'function' && handleClose()
|
2025-02-14 13:15:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const handlePreviewToggle = () => {
|
|
|
|
setShowPreview((prev) => !prev)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!userState.user?.pubkey) return <Navigate to={appRoutes.home} />
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<form className='feedPostsPost' onSubmit={handleFormSubmit}>
|
|
|
|
<div className='feedPostsPostInside'>
|
|
|
|
<div className='feedPostsPostInsideInputWrapper'>
|
|
|
|
<div className='feedPostsPostInsidePP'>
|
|
|
|
<div className='IBMSMSMBSSCL_CommentTopPPWrapper'>
|
|
|
|
<div
|
|
|
|
className='IBMSMSMBSSCL_CommentTopPP'
|
|
|
|
style={{
|
|
|
|
background: `url(${image}) center/cover no-repeat`,
|
|
|
|
width: '50px',
|
|
|
|
height: '50px',
|
|
|
|
borderRadius: '8px'
|
|
|
|
}}
|
|
|
|
></div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<textarea
|
|
|
|
id='postSocialTextarea'
|
|
|
|
name='content'
|
|
|
|
className='inputMain feedPostsPostInsideInput'
|
|
|
|
placeholder='Watcha thinking about?'
|
2025-02-14 13:50:22 +01:00
|
|
|
ref={ref}
|
2025-02-14 13:15:10 +01:00
|
|
|
value={content}
|
|
|
|
onChange={handleContentChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className='feedPostsPostInsideAction'>
|
|
|
|
<div
|
|
|
|
className='inputLabelWrapperMain inputLabelWrapperMainAlt'
|
|
|
|
style={{ width: 'unset' }}
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type='checkbox'
|
|
|
|
className='CheckboxMain'
|
|
|
|
checked={nsfw}
|
|
|
|
id='nsfw'
|
|
|
|
name='nsfw'
|
|
|
|
onChange={() => setNsfw((nsfw) => !nsfw)}
|
|
|
|
/>
|
|
|
|
<label htmlFor='nsfw' className='form-label labelMain'>
|
|
|
|
NSFW
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
style={{ display: 'flex', flexDirection: 'row', gridGap: '10px' }}
|
|
|
|
>
|
2025-02-14 13:50:22 +01:00
|
|
|
{typeof handleClose === 'function' && (
|
|
|
|
<button
|
|
|
|
className='btn btnMain'
|
|
|
|
type='button'
|
|
|
|
style={{ padding: '5px 20px', borderRadius: '8px' }}
|
|
|
|
onClick={handleClose}
|
|
|
|
>
|
|
|
|
Close
|
|
|
|
</button>
|
|
|
|
)}
|
2025-02-14 13:15:10 +01:00
|
|
|
<button
|
|
|
|
className='btn btnMain'
|
|
|
|
type='button'
|
|
|
|
style={{ padding: '5px 20px', borderRadius: '8px' }}
|
|
|
|
onClick={handlePreviewToggle}
|
|
|
|
>
|
|
|
|
Preview
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
className='btn btnMain'
|
|
|
|
type='submit'
|
|
|
|
style={{ padding: '5px 20px', borderRadius: '8px' }}
|
2025-02-14 17:29:06 +01:00
|
|
|
disabled={navigation.state !== 'idle'}
|
2025-02-14 13:15:10 +01:00
|
|
|
>
|
2025-02-14 17:29:06 +01:00
|
|
|
{navigation.state === 'idle' ? 'Post' : 'Posting...'}
|
2025-02-14 13:15:10 +01:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2025-02-14 22:48:58 +00:00
|
|
|
{showPreview && <NotePreview content={content} />}
|
2025-02-14 13:15:10 +01:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|