import { FC, useCallback, useState } from 'react' import { CommentId, ReviewPanelCommentThreadMessage, } from '../../../../../types/review-panel/review-panel' import { useTranslation } from 'react-i18next' import { formatTimeBasedOnYear } from '@/features/utils/format-date' import OLTooltip from '@/features/ui/components/ol/ol-tooltip' import MaterialIcon from '@/shared/components/material-icon' import AutoExpandingTextArea from '@/shared/components/auto-expanding-text-area' import { buildName } from '../utils/build-name' import ReviewPanelCommentOptions from './review-panel-comment-options' import { ExpandableContent } from './review-panel-expandable-content' import ReviewPanelDeleteCommentModal from './review-panel-delete-comment-modal' import { useUserContext } from '@/shared/context/user-context' export const ReviewPanelMessage: FC<{ message: ReviewPanelCommentThreadMessage hasReplies: boolean isReply: boolean onResolve?: () => Promise onEdit?: (commentId: CommentId, content: string) => Promise onDelete?: () => void isThreadResolved: boolean }> = ({ message, isReply, hasReplies, onResolve, onEdit, onDelete, isThreadResolved, }) => { const { t } = useTranslation() const [editing, setEditing] = useState(false) const [deleting, setDeleting] = useState(false) const [content, setContent] = useState(message.content) const user = useUserContext() const handleEditOption = useCallback(() => setEditing(true), []) const showDeleteModal = useCallback(() => setDeleting(true), []) const hideDeleteModal = useCallback(() => setDeleting(false), []) const handleSubmit = useCallback(() => { onEdit?.(message.id, content) setEditing(false) }, [content, message.id, onEdit]) const handleDelete = useCallback(() => { onDelete?.() setDeleting(false) }, [onDelete]) return (
{buildName(message.user)}
{formatTimeBasedOnYear(message.timestamp)}
{!editing && !isReply && !isThreadResolved && ( )} {!editing && !isThreadResolved && ( )}
{editing ? ( setContent(e.target.value)} onKeyDown={e => { if ( e.key === 'Enter' && !e.shiftKey && !e.ctrlKey && !e.metaKey && content ) { e.preventDefault() ;(e.target as HTMLTextAreaElement).blur() } }} value={content} autoFocus // eslint-disable-line jsx-a11y/no-autofocus /> ) : ( )} {deleting && ( )}
) }