import React, { useCallback, useMemo, useState } from 'react'; import PropTypes from 'prop-types'; import { useDispatch, useSelector } from 'react-redux'; import * as timeago from 'timeago.js'; import { useIntl } from '@edx/frontend-platform/i18n'; import { Avatar, useToggle } from '@edx/paragon'; import HTMLLoader from '../../../../components/HTMLLoader'; import { AvatarOutlineAndLabelColors, ContentActions } from '../../../../data/constants'; import { ActionsDropdown, AlertBanner, AuthorLabel, Confirmation, } from '../../../common'; import timeLocale from '../../../common/time-locale'; import { ContentTypes } from '../../../data/constants'; import { useAlertBannerVisible } from '../../../data/hooks'; import { selectCommentOrResponseById } from '../../data/selectors'; import { editComment, removeComment } from '../../data/thunks'; import messages from '../../messages'; import CommentEditor from './CommentEditor'; const Reply = ({ responseId }) => { timeago.register('time-locale', timeLocale); const { id, abuseFlagged, author, authorLabel, endorsed, lastEdit, closed, closedBy, closeReason, createdAt, threadId, parentId, rawBody, renderedBody, editByLabel, closedByLabel, } = useSelector(selectCommentOrResponseById(responseId)); const intl = useIntl(); const dispatch = useDispatch(); const [isEditing, setEditing] = useState(false); const [isDeleting, showDeleteConfirmation, hideDeleteConfirmation] = useToggle(false); const [isReporting, showReportConfirmation, hideReportConfirmation] = useToggle(false); const colorClass = AvatarOutlineAndLabelColors[authorLabel]; const hasAnyAlert = useAlertBannerVisible({ author, abuseFlagged, lastEdit, closed, }); const handleDeleteConfirmation = useCallback(() => { dispatch(removeComment(id)); hideDeleteConfirmation(); }, [id, hideDeleteConfirmation]); const handleReportConfirmation = useCallback(() => { dispatch(editComment(id, { flagged: !abuseFlagged })); hideReportConfirmation(); }, [abuseFlagged, id, hideReportConfirmation]); const handleEditContent = useCallback(() => { setEditing(true); }, []); const handleReplyEndorse = useCallback(() => { dispatch(editComment(id, { endorsed: !endorsed }, ContentActions.ENDORSE)); }, [endorsed, id]); const handleAbusedFlag = useCallback(() => { if (abuseFlagged) { dispatch(editComment(id, { flagged: !abuseFlagged })); } else { showReportConfirmation(); } }, [abuseFlagged, id, showReportConfirmation]); const handleCloseEditor = useCallback(() => { setEditing(false); }, []); const actionHandlers = useMemo(() => ({ [ContentActions.EDIT_CONTENT]: handleEditContent, [ContentActions.ENDORSE]: handleReplyEndorse, [ContentActions.DELETE]: showDeleteConfirmation, [ContentActions.REPORT]: handleAbusedFlag, }), [handleEditContent, handleReplyEndorse, showDeleteConfirmation, handleAbusedFlag]); return (