2024-09-16 04:40:59 -04:00
|
|
|
import { Dispatch, memo, SetStateAction, useCallback, useState } from 'react'
|
2024-08-19 09:16:56 -04:00
|
|
|
import { Change, CommentOperation } from '../../../../../types/change'
|
|
|
|
import { ReviewPanelMessage } from './review-panel-message'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import {
|
|
|
|
useThreadsActionsContext,
|
|
|
|
useThreadsContext,
|
|
|
|
} from '../context/threads-context'
|
|
|
|
import AutoExpandingTextArea from '@/shared/components/auto-expanding-text-area'
|
|
|
|
import ReviewPanelResolvedMessage from './review-panel-resolved-message'
|
|
|
|
import { ReviewPanelResolvedCommentThread } from '../../../../../types/review-panel/comment-thread'
|
2024-09-16 04:40:59 -04:00
|
|
|
import useSubmittableTextInput from '../hooks/use-submittable-text-input'
|
2024-08-19 09:16:56 -04:00
|
|
|
|
|
|
|
export const ReviewPanelCommentContent = memo<{
|
|
|
|
comment: Change<CommentOperation>
|
|
|
|
isResolved: boolean
|
2024-10-02 07:58:46 -04:00
|
|
|
onLeave?: () => void
|
|
|
|
onEnter?: () => void
|
|
|
|
}>(({ comment, isResolved, onLeave, onEnter }) => {
|
2024-08-19 09:16:56 -04:00
|
|
|
const { t } = useTranslation()
|
|
|
|
const [submitting, setSubmitting] = useState(false)
|
|
|
|
const [error, setError] = useState<Error>()
|
|
|
|
const threads = useThreadsContext()
|
|
|
|
const { resolveThread, addMessage } = useThreadsActionsContext()
|
|
|
|
|
2024-09-16 04:40:59 -04:00
|
|
|
const handleSubmitReply = useCallback(
|
|
|
|
(content: string, setContent: Dispatch<SetStateAction<string>>) => {
|
|
|
|
setSubmitting(true)
|
|
|
|
addMessage(comment.op.t, content)
|
|
|
|
.then(() => {
|
|
|
|
setContent('')
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
setError(error)
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
setSubmitting(false)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[addMessage, comment.op.t]
|
|
|
|
)
|
2024-08-19 09:16:56 -04:00
|
|
|
|
2024-09-16 04:40:59 -04:00
|
|
|
const { handleChange, handleKeyPress, content } =
|
|
|
|
useSubmittableTextInput(handleSubmitReply)
|
2024-08-19 09:16:56 -04:00
|
|
|
|
|
|
|
const thread = threads?.[comment.op.t]
|
|
|
|
if (!thread) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2024-10-02 07:58:46 -04:00
|
|
|
<div
|
|
|
|
className="review-panel-entry-content"
|
|
|
|
onMouseEnter={onEnter}
|
|
|
|
onMouseLeave={onLeave}
|
|
|
|
>
|
2024-08-19 09:16:56 -04:00
|
|
|
{thread.messages.map((message, i) => {
|
|
|
|
const isReply = i !== 0
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div key={message.id} className="review-panel-comment-wrapper">
|
|
|
|
{isReply && <div className="review-panel-comment-reply-divider" />}
|
|
|
|
<ReviewPanelMessage
|
|
|
|
message={message}
|
|
|
|
threadId={comment.op.t}
|
|
|
|
isReply={isReply}
|
|
|
|
hasReplies={!isReply && thread.messages.length > 1}
|
|
|
|
onResolve={() => resolveThread(comment.op.t)}
|
|
|
|
isThreadResolved={isResolved}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
|
|
|
|
{isResolved && (
|
|
|
|
<div className="review-panel-comment-wrapper">
|
|
|
|
<div className="review-panel-comment-reply-divider" />
|
|
|
|
<ReviewPanelResolvedMessage
|
|
|
|
thread={thread as ReviewPanelResolvedCommentThread}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{!isResolved && (
|
|
|
|
<AutoExpandingTextArea
|
|
|
|
name="content"
|
|
|
|
className="review-panel-comment-input"
|
2024-09-16 04:40:59 -04:00
|
|
|
onChange={handleChange}
|
|
|
|
onKeyDown={handleKeyPress}
|
2024-08-19 09:16:56 -04:00
|
|
|
placeholder={t('reply')}
|
|
|
|
value={content}
|
|
|
|
disabled={submitting}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{error && <div>{error.message}</div>}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
})
|
|
|
|
ReviewPanelCommentContent.displayName = 'ReviewPanelCommentContent'
|