import React, { useMemo } from 'react'; import PropTypes from 'prop-types'; import { Avatar, Badge, Icon } from '@openedx/paragon'; import { Question } from '@openedx/paragon/icons'; import classNames from 'classnames'; import { useSelector } from 'react-redux'; import { getConfig } from '@edx/frontend-platform'; import { useIntl } from '@edx/frontend-platform/i18n'; import { AvatarOutlineAndLabelColors, ThreadType } from '../../../data/constants'; import { AuthorLabel } from '../../common'; import { useAlertBannerVisible } from '../../data/hooks'; import { selectAuthorAvatar } from '../data/selectors'; import messages from './messages'; export const PostAvatar = React.memo(({ author, postType, authorLabel, fromPostLink, read, postUsers, }) => { const outlineColor = AvatarOutlineAndLabelColors[authorLabel]; const authorAvatars = useSelector(selectAuthorAvatar(author)); const avatarSize = useMemo(() => { let size = '2rem'; if (postType === ThreadType.DISCUSSION && !fromPostLink) { size = '2rem'; } else if (postType === ThreadType.QUESTION) { size = '1.5rem'; } return size; }, [postType]); const avatarSpacing = useMemo(() => { let spacing = 'mr-3 '; if (postType === ThreadType.DISCUSSION && fromPostLink) { spacing += 'pt-2 ml-0.5'; } else if (postType === ThreadType.DISCUSSION) { spacing += 'ml-0.5 mt-0.5'; } return spacing; }, [postType]); const profileImage = getConfig()?.ENABLE_PROFILE_IMAGE === 'true' ? Object.values(postUsers ?? {})[0]?.profile?.image : null; return (
{postType === ThreadType.QUESTION && ( )}
); }); PostAvatar.propTypes = { author: PropTypes.string.isRequired, postType: PropTypes.string.isRequired, authorLabel: PropTypes.string, fromPostLink: PropTypes.bool, read: PropTypes.bool, postUsers: PropTypes.shape({}).isRequired, }; PostAvatar.defaultProps = { authorLabel: null, fromPostLink: false, read: false, }; const PostHeader = ({ abuseFlagged, author, authorLabel, closed, createdAt, hasEndorsed, lastEdit, title, postType, preview, postUsers, }) => { const intl = useIntl(); const showAnsweredBadge = preview && hasEndorsed && postType === ThreadType.QUESTION; const authorLabelColor = AvatarOutlineAndLabelColors[authorLabel]; const hasAnyAlert = useAlertBannerVisible({ author, abuseFlagged, lastEdit, closed, }); return (
{preview ? (
{title}
{showAnsweredBadge && {intl.formatMessage(messages.answered)}}
) : (
{title}
)}
); }; PostHeader.propTypes = { preview: PropTypes.bool, hasEndorsed: PropTypes.bool.isRequired, postType: PropTypes.string.isRequired, authorLabel: PropTypes.string, author: PropTypes.string.isRequired, title: PropTypes.string.isRequired, createdAt: PropTypes.string.isRequired, abuseFlagged: PropTypes.bool, lastEdit: PropTypes.shape({ reason: PropTypes.string, }), closed: PropTypes.bool, postUsers: PropTypes.shape({}).isRequired, }; PostHeader.defaultProps = { authorLabel: null, preview: false, abuseFlagged: false, lastEdit: {}, closed: false, }; export default React.memo(PostHeader);