2024-10-10 09:01:30 -04:00
|
|
|
import { memo, useCallback, useState } from 'react'
|
2024-08-12 05:50:54 -04:00
|
|
|
import { Change, CommentOperation } from '../../../../../types/change'
|
2024-10-10 09:01:30 -04:00
|
|
|
import {
|
|
|
|
useThreadsActionsContext,
|
|
|
|
useThreadsContext,
|
|
|
|
} from '../context/threads-context'
|
2024-08-12 05:50:54 -04:00
|
|
|
import classnames from 'classnames'
|
2024-08-15 06:07:40 -04:00
|
|
|
import { ReviewPanelEntry } from './review-panel-entry'
|
2024-08-19 09:16:56 -04:00
|
|
|
import MaterialIcon from '@/shared/components/material-icon'
|
|
|
|
import { ReviewPanelCommentContent } from './review-panel-comment-content'
|
2024-10-10 09:01:30 -04:00
|
|
|
import { CommentId } from '../../../../../types/review-panel/review-panel'
|
|
|
|
import { useModalsContext } from '@/features/ide-react/context/modals-context'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2024-08-12 05:50:54 -04:00
|
|
|
|
|
|
|
export const ReviewPanelComment = memo<{
|
|
|
|
comment: Change<CommentOperation>
|
2024-09-26 10:13:54 -04:00
|
|
|
docId: string
|
2024-08-12 05:50:54 -04:00
|
|
|
top?: number
|
2024-09-26 10:13:54 -04:00
|
|
|
hoverRanges?: boolean
|
2024-10-02 07:58:46 -04:00
|
|
|
onEnter?: () => void
|
|
|
|
onLeave?: () => void
|
|
|
|
hovered?: boolean
|
|
|
|
}>(({ comment, top, hovered, onEnter, onLeave, docId, hoverRanges }) => {
|
2024-08-12 05:50:54 -04:00
|
|
|
const threads = useThreadsContext()
|
2024-10-10 09:01:30 -04:00
|
|
|
const { resolveThread, editMessage, deleteMessage, addMessage } =
|
|
|
|
useThreadsActionsContext()
|
|
|
|
const { showGenericMessageModal } = useModalsContext()
|
|
|
|
const { t } = useTranslation()
|
|
|
|
|
|
|
|
const [processing, setProcessing] = useState(false)
|
|
|
|
|
|
|
|
const handleResolveComment = useCallback(async () => {
|
|
|
|
setProcessing(true)
|
|
|
|
try {
|
|
|
|
await resolveThread(comment.op.t)
|
|
|
|
} catch (err) {
|
|
|
|
debugConsole.error(err)
|
|
|
|
showGenericMessageModal(
|
|
|
|
t('resolve_comment_error_title'),
|
|
|
|
t('resolve_comment_error_message')
|
|
|
|
)
|
|
|
|
} finally {
|
|
|
|
setProcessing(false)
|
|
|
|
}
|
|
|
|
}, [comment.op.t, resolveThread, showGenericMessageModal, t])
|
|
|
|
|
|
|
|
const handleEditMessage = useCallback(
|
|
|
|
async (commentId: CommentId, content: string) => {
|
|
|
|
setProcessing(true)
|
|
|
|
try {
|
|
|
|
await editMessage(comment.op.t, commentId, content)
|
|
|
|
} catch (err) {
|
|
|
|
debugConsole.error(err)
|
|
|
|
showGenericMessageModal(
|
|
|
|
t('edit_comment_error_title'),
|
|
|
|
t('edit_comment_error_message')
|
|
|
|
)
|
|
|
|
} finally {
|
|
|
|
setProcessing(false)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[comment.op.t, editMessage, showGenericMessageModal, t]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handleDeleteMessage = useCallback(
|
|
|
|
async (commentId: CommentId) => {
|
|
|
|
setProcessing(true)
|
|
|
|
try {
|
|
|
|
await deleteMessage(comment.op.t, commentId)
|
|
|
|
} catch (err) {
|
|
|
|
debugConsole.error(err)
|
|
|
|
showGenericMessageModal(
|
|
|
|
t('delete_comment_error_title'),
|
|
|
|
t('delete_comment_error_message')
|
|
|
|
)
|
|
|
|
} finally {
|
|
|
|
setProcessing(false)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[comment.op.t, deleteMessage, showGenericMessageModal, t]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handleSubmitReply = useCallback(
|
|
|
|
async (content: string) => {
|
|
|
|
setProcessing(true)
|
|
|
|
try {
|
|
|
|
await addMessage(comment.op.t, content)
|
|
|
|
} catch (err) {
|
|
|
|
debugConsole.error(err)
|
|
|
|
showGenericMessageModal(
|
|
|
|
t('add_comment_error_title'),
|
|
|
|
t('add_comment_error_message')
|
|
|
|
)
|
|
|
|
throw err
|
|
|
|
} finally {
|
|
|
|
setProcessing(false)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[addMessage, comment.op.t, showGenericMessageModal, t]
|
|
|
|
)
|
2024-08-12 05:50:54 -04:00
|
|
|
|
|
|
|
const thread = threads?.[comment.op.t]
|
2024-09-19 06:07:43 -04:00
|
|
|
if (!thread || thread.resolved || thread.messages.length === 0) {
|
2024-08-12 05:50:54 -04:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2024-08-15 06:07:40 -04:00
|
|
|
<ReviewPanelEntry
|
|
|
|
className={classnames('review-panel-entry-comment', {
|
|
|
|
'review-panel-entry-loaded': !!threads?.[comment.op.t],
|
2024-10-02 07:58:46 -04:00
|
|
|
'review-panel-entry-hover': hovered,
|
2024-08-15 06:07:40 -04:00
|
|
|
})}
|
2024-09-26 10:13:54 -04:00
|
|
|
docId={docId}
|
2024-08-15 06:07:40 -04:00
|
|
|
top={top}
|
|
|
|
op={comment.op}
|
|
|
|
position={comment.op.p}
|
2024-09-26 10:13:54 -04:00
|
|
|
hoverRanges={hoverRanges}
|
2024-10-10 09:01:30 -04:00
|
|
|
disabled={processing}
|
2024-08-12 05:50:54 -04:00
|
|
|
>
|
2024-10-02 07:58:46 -04:00
|
|
|
<div
|
|
|
|
className="review-panel-entry-indicator"
|
|
|
|
onMouseEnter={onEnter}
|
|
|
|
onMouseLeave={onLeave}
|
|
|
|
>
|
2024-09-24 04:54:22 -04:00
|
|
|
<MaterialIcon type="comment" className="review-panel-entry-icon" />
|
2024-08-12 05:50:54 -04:00
|
|
|
</div>
|
2024-10-02 07:58:46 -04:00
|
|
|
<ReviewPanelCommentContent
|
|
|
|
comment={comment}
|
|
|
|
isResolved={false}
|
|
|
|
onLeave={onLeave}
|
|
|
|
onEnter={onEnter}
|
2024-10-10 09:01:30 -04:00
|
|
|
onResolve={handleResolveComment}
|
|
|
|
onEdit={handleEditMessage}
|
|
|
|
onDelete={handleDeleteMessage}
|
|
|
|
onReply={handleSubmitReply}
|
2024-10-02 07:58:46 -04:00
|
|
|
/>
|
2024-08-15 06:07:40 -04:00
|
|
|
</ReviewPanelEntry>
|
2024-08-12 05:50:54 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
ReviewPanelComment.displayName = 'ReviewPanelComment'
|