import { PaperAirplaneIcon } from '@heroicons/react/solid' import { Editor } from '@tiptap/react' import clsx from 'clsx' import { User } from 'common/user' import { useEffect, useState } from 'react' import { useUser } from 'web/hooks/use-user' import { MAX_COMMENT_LENGTH } from 'web/lib/firebase/comments' import { Avatar } from './avatar' import { TextEditor, useTextEditor } from './editor' import { Row } from './layout/row' import { LoadingIndicator } from './loading-indicator' export function CommentInput(props: { replyToUser?: { id: string; username: string } // Reply to a free response answer parentAnswerOutcome?: string // Reply to another comment parentCommentId?: string onSubmitComment?: (editor: Editor, betId: string | undefined) => void className?: string presetId?: string }) { const { parentAnswerOutcome, parentCommentId, replyToUser, onSubmitComment, presetId, } = props const user = useUser() const { editor, upload } = useTextEditor({ simple: true, max: MAX_COMMENT_LENGTH, placeholder: !!parentCommentId || !!parentAnswerOutcome ? 'Write a reply...' : 'Write a comment...', }) const [isSubmitting, setIsSubmitting] = useState(false) async function submitComment(betId: string | undefined) { if (!editor || editor.isEmpty || isSubmitting) return setIsSubmitting(true) onSubmitComment?.(editor, betId) setIsSubmitting(false) } if (user?.isBannedFromPosting) return <> return (
) } export function CommentInputTextArea(props: { user: User | undefined | null replyToUser?: { id: string; username: string } editor: Editor | null upload: Parameters[0]['upload'] submitComment: (id?: string) => void isSubmitting: boolean presetId?: string }) { const { user, editor, upload, submitComment, presetId, isSubmitting, replyToUser, } = props useEffect(() => { editor?.setEditable(!isSubmitting) }, [isSubmitting, editor]) const submit = () => { submitComment(presetId) editor?.commands?.clearContent() } useEffect(() => { if (!editor) { return } // Submit on ctrl+enter or mod+enter key editor.setOptions({ editorProps: { handleKeyDown: (view, event) => { if ( event.key === 'Enter' && !event.shiftKey && (event.ctrlKey || event.metaKey) && // mention list is closed !(view.state as any).mention$.active ) { submit() event.preventDefault() return true } return false }, }, }) // insert at mention and focus if (replyToUser) { editor .chain() .setContent({ type: 'mention', attrs: { label: replyToUser.username, id: replyToUser.id }, }) .insertContent(' ') .focus() .run() } // eslint-disable-next-line react-hooks/exhaustive-deps }, [editor]) return ( <> {user && !isSubmitting && ( )} {isSubmitting && ( )} {!user && ( )} ) }