overleaf/services/web/frontend/js/features/review-panel-new/components/review-panel-comment-content.tsx
Domagoj Kriskovic 4a3eed35d6 Fix accept changes request for new review panel (#20956)
* Fix accept changes request for new review panel

* Implement pessimistic UI updates for new review panel (#20986)

* Implement pessimistic UI updates for new review panel

* deleteThread, reopenThread handlers

* use finally

* handleSubmit in useCallback

GitOrigin-RevId: 358181a6b5601ad1b3579e001564dfa4da67a81d
2024-10-14 11:08:44 +00:00

98 lines
3 KiB
TypeScript

import { memo, useCallback } from 'react'
import { Change, CommentOperation } from '../../../../../types/change'
import { ReviewPanelMessage } from './review-panel-message'
import { useTranslation } from 'react-i18next'
import { 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'
import useSubmittableTextInput from '../hooks/use-submittable-text-input'
import { CommentId } from '../../../../../types/review-panel/review-panel'
export const ReviewPanelCommentContent = memo<{
comment: Change<CommentOperation>
isResolved: boolean
onEdit?: (commentId: CommentId, content: string) => Promise<void>
onReply?: (content: string) => Promise<void>
onDelete?: (commentId: CommentId) => Promise<void>
onResolve?: () => Promise<void>
onLeave?: () => void
onEnter?: () => void
}>(
({
comment,
isResolved,
onResolve,
onDelete,
onEdit,
onReply,
onLeave,
onEnter,
}) => {
const { t } = useTranslation()
const threads = useThreadsContext()
const handleSubmit = useCallback(
(content, setContent) => onReply?.(content).then(() => setContent('')),
[onReply]
)
const { handleChange, handleKeyPress, content } =
useSubmittableTextInput(handleSubmit)
const thread = threads?.[comment.op.t]
if (!thread) {
return null
}
return (
<div
className="review-panel-entry-content"
onMouseEnter={onEnter}
onMouseLeave={onLeave}
>
{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}
isReply={isReply}
hasReplies={!isReply && thread.messages.length > 1}
onResolve={onResolve}
onEdit={onEdit}
onDelete={onDelete}
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"
onChange={handleChange}
onKeyDown={handleKeyPress}
placeholder={t('reply')}
value={content}
/>
)}
</div>
)
}
)
ReviewPanelCommentContent.displayName = 'ReviewPanelCommentContent'