-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathPostHeader.jsx
171 lines (156 loc) · 5.07 KB
/
PostHeader.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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 { 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 messages from './messages';
export const PostAvatar = React.memo(({
author, postType, authorLabel, fromPostLink, read, postUsers,
}) => {
const outlineColor = AvatarOutlineAndLabelColors[authorLabel];
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' && postUsers && Object.values(postUsers)[0].profile.image;
return (
<div className={avatarSpacing}>
{postType === ThreadType.QUESTION && (
<Icon
src={Question}
className={classNames('position-absolute rounded-circle question-icon-size', {
'question-icon-position': fromPostLink, 'bg-white': !read, 'bg-light-300': read,
})}
/>
)}
<Avatar
className={classNames('border-0 mt-1', {
[`outline-${outlineColor}`]: outlineColor,
'outline-anonymous': !outlineColor,
'mt-3 ml-2': postType === ThreadType.QUESTION && fromPostLink,
'mt-2.5': postType === ThreadType.QUESTION && !fromPostLink,
'avarat-img-position mt-17px': postType === ThreadType.QUESTION,
})}
style={{
height: avatarSize,
width: avatarSize,
}}
src={profileImage?.hasImage ? profileImage?.imageUrlSmall : undefined}
alt={author}
/>
</div>
);
});
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 (
<div className={classNames('d-flex flex-fill mw-100', { 'mt-10px': hasAnyAlert && !preview })}>
<div className="flex-shrink-0">
<PostAvatar postType={postType} author={author} authorLabel={authorLabel} postUsers={postUsers} />
</div>
<div className="align-items-center d-flex flex-row">
<div className="d-flex flex-column justify-content-start mw-100">
{preview ? (
<div className="h4 d-flex align-items-center pb-0 mb-0 flex-fill">
<div className="flex-fill text-truncate" role="heading" aria-level="1">
{title}
</div>
{showAnsweredBadge
&& <Badge variant="success">{intl.formatMessage(messages.answered)}</Badge>}
</div>
) : (
<h5
className="mb-0 font-style text-primary-500"
style={{ lineHeight: '21px' }}
aria-level="1"
tabIndex="-1"
accessKey="h"
>
{title}
</h5>
)}
<AuthorLabel
author={author || intl.formatMessage(messages.anonymous)}
authorLabel={authorLabel}
labelColor={authorLabelColor && `text-${authorLabelColor}`}
linkToProfile
postCreatedAt={createdAt}
postOrComment
/>
</div>
</div>
</div>
);
};
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);