2022-10-12 18:05:58 +00:00
|
|
|
import { PaperAirplaneIcon, XCircleIcon } from '@heroicons/react/solid'
|
2022-09-07 22:09:20 +00:00
|
|
|
import { Editor } from '@tiptap/react'
|
|
|
|
import clsx from 'clsx'
|
2022-10-12 18:05:58 +00:00
|
|
|
import { Answer } from 'common/answer'
|
|
|
|
import { AnyContractType, Contract } from 'common/contract'
|
2022-09-07 22:09:20 +00:00
|
|
|
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'
|
2022-10-12 18:05:58 +00:00
|
|
|
import Curve from 'web/public/custom-components/curve'
|
2022-09-07 22:09:20 +00:00
|
|
|
import { Avatar } from './avatar'
|
|
|
|
import { TextEditor, useTextEditor } from './editor'
|
2022-10-12 18:05:58 +00:00
|
|
|
import { CommentsAnswer } from './feed/feed-answer-comment-group'
|
|
|
|
import { ContractCommentInput } from './feed/feed-comments'
|
2022-09-07 22:09:20 +00:00
|
|
|
import { Row } from './layout/row'
|
|
|
|
import { LoadingIndicator } from './loading-indicator'
|
|
|
|
|
|
|
|
export function CommentInput(props: {
|
2022-09-22 19:40:44 +00:00
|
|
|
replyTo?: { id: string; username: string }
|
2022-09-07 22:09:20 +00:00
|
|
|
// Reply to a free response answer
|
|
|
|
parentAnswerOutcome?: string
|
|
|
|
// Reply to another comment
|
|
|
|
parentCommentId?: string
|
2022-09-20 22:25:58 +00:00
|
|
|
onSubmitComment?: (editor: Editor) => void
|
2022-10-11 19:52:27 +00:00
|
|
|
// unique id for autosave
|
|
|
|
pageId: string
|
2022-09-07 22:09:20 +00:00
|
|
|
className?: string
|
|
|
|
}) {
|
2022-10-11 19:52:27 +00:00
|
|
|
const {
|
|
|
|
parentAnswerOutcome,
|
|
|
|
parentCommentId,
|
|
|
|
replyTo,
|
|
|
|
onSubmitComment,
|
|
|
|
pageId,
|
|
|
|
} = props
|
2022-09-07 22:09:20 +00:00
|
|
|
const user = useUser()
|
|
|
|
|
|
|
|
const { editor, upload } = useTextEditor({
|
2022-10-11 19:52:27 +00:00
|
|
|
key: `comment ${pageId} ${parentCommentId ?? parentAnswerOutcome ?? ''}`,
|
2022-09-07 22:09:20 +00:00
|
|
|
simple: true,
|
|
|
|
max: MAX_COMMENT_LENGTH,
|
|
|
|
placeholder:
|
|
|
|
!!parentCommentId || !!parentAnswerOutcome
|
|
|
|
? 'Write a reply...'
|
|
|
|
: 'Write a comment...',
|
|
|
|
})
|
|
|
|
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
|
2022-09-20 22:25:58 +00:00
|
|
|
async function submitComment() {
|
2022-09-07 22:09:20 +00:00
|
|
|
if (!editor || editor.isEmpty || isSubmitting) return
|
|
|
|
setIsSubmitting(true)
|
2022-09-20 22:25:58 +00:00
|
|
|
onSubmitComment?.(editor)
|
2022-09-07 22:09:20 +00:00
|
|
|
setIsSubmitting(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user?.isBannedFromPosting) return <></>
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Row className={clsx(props.className, 'mb-2 gap-1 sm:gap-2')}>
|
|
|
|
<Avatar
|
|
|
|
avatarUrl={user?.avatarUrl}
|
|
|
|
username={user?.username}
|
|
|
|
size="sm"
|
|
|
|
className="mt-2"
|
|
|
|
/>
|
|
|
|
<div className="min-w-0 flex-1 pl-0.5 text-sm">
|
|
|
|
<CommentInputTextArea
|
|
|
|
editor={editor}
|
|
|
|
upload={upload}
|
2022-09-22 19:40:44 +00:00
|
|
|
replyTo={replyTo}
|
2022-09-07 22:09:20 +00:00
|
|
|
user={user}
|
|
|
|
submitComment={submitComment}
|
|
|
|
isSubmitting={isSubmitting}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
2022-10-12 18:05:58 +00:00
|
|
|
export function AnswerCommentInput(props: {
|
|
|
|
contract: Contract<AnyContractType>
|
|
|
|
answerResponse: Answer
|
|
|
|
onCancelAnswerResponse?: () => void
|
|
|
|
}) {
|
|
|
|
const { contract, answerResponse, onCancelAnswerResponse } = props
|
|
|
|
const replyTo = {
|
|
|
|
id: answerResponse.id,
|
|
|
|
username: answerResponse.username,
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<CommentsAnswer answer={answerResponse} contract={contract} />
|
|
|
|
<Row>
|
|
|
|
<div className="ml-1">
|
|
|
|
<Curve size={28} strokeWidth={1} color="#D8D8EB" />
|
|
|
|
</div>
|
|
|
|
<div className="relative w-full pt-1">
|
|
|
|
<ContractCommentInput
|
|
|
|
contract={contract}
|
|
|
|
parentAnswerOutcome={answerResponse.number.toString()}
|
|
|
|
replyTo={replyTo}
|
|
|
|
onSubmitComment={onCancelAnswerResponse}
|
|
|
|
/>
|
|
|
|
<button onClick={onCancelAnswerResponse}>
|
|
|
|
<div className="absolute -top-1 -right-2 h-4 w-4 rounded-full bg-white" />
|
|
|
|
<XCircleIcon className="text-greyscale-5 hover:text-greyscale-6 absolute -top-1 -right-2 h-5 w-5" />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</Row>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
2022-09-07 22:09:20 +00:00
|
|
|
|
|
|
|
export function CommentInputTextArea(props: {
|
|
|
|
user: User | undefined | null
|
2022-09-22 19:40:44 +00:00
|
|
|
replyTo?: { id: string; username: string }
|
2022-09-07 22:09:20 +00:00
|
|
|
editor: Editor | null
|
|
|
|
upload: Parameters<typeof TextEditor>[0]['upload']
|
2022-09-20 22:25:58 +00:00
|
|
|
submitComment: () => void
|
2022-09-07 22:09:20 +00:00
|
|
|
isSubmitting: boolean
|
|
|
|
}) {
|
2022-09-22 19:40:44 +00:00
|
|
|
const { user, editor, upload, submitComment, isSubmitting, replyTo } = props
|
2022-09-07 22:09:20 +00:00
|
|
|
useEffect(() => {
|
|
|
|
editor?.setEditable(!isSubmitting)
|
|
|
|
}, [isSubmitting, editor])
|
|
|
|
|
|
|
|
const submit = () => {
|
2022-09-20 22:25:58 +00:00
|
|
|
submitComment()
|
2022-10-11 19:52:27 +00:00
|
|
|
editor?.commands?.clearContent(true)
|
2022-09-07 22:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!editor) {
|
|
|
|
return
|
|
|
|
}
|
2022-09-09 18:09:31 +00:00
|
|
|
// Submit on ctrl+enter or mod+enter key
|
2022-09-07 22:09:20 +00:00
|
|
|
editor.setOptions({
|
|
|
|
editorProps: {
|
|
|
|
handleKeyDown: (view, event) => {
|
|
|
|
if (
|
|
|
|
event.key === 'Enter' &&
|
|
|
|
!event.shiftKey &&
|
2022-09-09 18:09:31 +00:00
|
|
|
(event.ctrlKey || event.metaKey) &&
|
2022-09-07 22:09:20 +00:00
|
|
|
// mention list is closed
|
|
|
|
!(view.state as any).mention$.active
|
|
|
|
) {
|
|
|
|
submit()
|
|
|
|
event.preventDefault()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
// insert at mention and focus
|
2022-10-11 19:52:27 +00:00
|
|
|
if (replyTo && editor.isEmpty) {
|
2022-09-07 22:09:20 +00:00
|
|
|
editor
|
|
|
|
.chain()
|
|
|
|
.setContent({
|
|
|
|
type: 'mention',
|
2022-09-22 19:40:44 +00:00
|
|
|
attrs: { label: replyTo.username, id: replyTo.id },
|
2022-09-07 22:09:20 +00:00
|
|
|
})
|
|
|
|
.insertContent(' ')
|
2022-10-12 18:05:58 +00:00
|
|
|
.focus(undefined, { scrollIntoView: false })
|
2022-09-07 22:09:20 +00:00
|
|
|
.run()
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [editor])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<TextEditor editor={editor} upload={upload}>
|
|
|
|
{user && !isSubmitting && (
|
|
|
|
<button
|
2022-10-02 21:57:57 +00:00
|
|
|
className="px-2 text-gray-400 hover:text-gray-500 disabled:bg-inherit disabled:text-gray-300"
|
2022-09-07 22:09:20 +00:00
|
|
|
disabled={!editor || editor.isEmpty}
|
|
|
|
onClick={submit}
|
|
|
|
>
|
|
|
|
<PaperAirplaneIcon className="m-0 h-[25px] min-w-[22px] rotate-90 p-0" />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{isSubmitting && (
|
2022-09-20 22:25:58 +00:00
|
|
|
<LoadingIndicator spinnerClassName="border-gray-500" />
|
2022-09-07 22:09:20 +00:00
|
|
|
)}
|
|
|
|
</TextEditor>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|