-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
suggestedOwnerHovercard.tsx
241 lines (219 loc) · 6.8 KB
/
suggestedOwnerHovercard.tsx
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import React from 'react';
import moment from 'moment';
import styled from '@emotion/styled';
import {Actor, Commit} from 'app/types';
import {t, tct} from 'app/locale';
import {openInviteMembersModal} from 'app/actionCreators/modal';
import ActorAvatar from 'app/components/avatar/actorAvatar';
import Alert from 'app/components/alert';
import Button from 'app/components/button';
import Hovercard from 'app/components/hovercard';
import {IconCommit, IconWarning} from 'app/icons';
import Link from 'app/components/links/link';
import space from 'app/styles/space';
import theme from 'app/utils/theme';
import {defined} from 'app/utils';
type Props = {
/**
* The suggested actor.
*/
actor: Actor;
/**
* The list of commits the actor is suggested for. May be left blank if the
* actor is not suggested for commits.
*/
commits?: Commit[];
/**
* The list of ownership rules the actor is suggested for. May be left blank
* if the actor is not suggested based on ownership rules.
*/
rules?: any[] | null;
};
type State = {
commitsExpanded: boolean;
rulesExpanded: boolean;
};
class SuggestedOwnerHovercard extends React.Component<Props, State> {
state: State = {
commitsExpanded: false,
rulesExpanded: false,
};
render() {
const {actor, commits, rules, ...props} = this.props;
const {commitsExpanded, rulesExpanded} = this.state;
return (
<Hovercard
header={
<React.Fragment>
<HovercardHeader>
<HovercardActorAvatar actor={actor} />
{actor.name || actor.email}
</HovercardHeader>
{actor.id === undefined && (
<EmailAlert icon={<IconWarning size="xs" />} type="warning">
{tct(
'The email [actorEmail] is not a member of your organization. [inviteUser:Invite] them or link additional emails in [accountSettings:account settings].',
{
actorEmail: <strong>{actor.email}</strong>,
accountSettings: <Link to="/settings/account/emails/" />,
inviteUser: (
<a
onClick={() =>
openInviteMembersModal({
initialData: [
{
emails: new Set([actor.email]),
},
],
source: 'suggested_assignees',
})
}
/>
),
}
)}
</EmailAlert>
)}
</React.Fragment>
}
body={
<HovercardBody>
{commits !== undefined && (
<React.Fragment>
<div className="divider">
<h6>{t('Commits')}</h6>
</div>
<div>
{commits
.slice(0, commitsExpanded ? commits.length : 3)
.map(({message, dateCreated}, i) => (
<CommitReasonItem key={i}>
<CommitIcon />
<CommitMessage message={message} date={dateCreated} />
</CommitReasonItem>
))}
</div>
{commits.length > 3 && !commitsExpanded ? (
<ViewMoreButton
onClick={() => this.setState({commitsExpanded: true})}
/>
) : null}
</React.Fragment>
)}
{defined(rules) && (
<React.Fragment>
<div className="divider">
<h6>{t('Matching Ownership Rules')}</h6>
</div>
<div>
{rules
.slice(0, rulesExpanded ? rules.length : 3)
.map(([type, matched], i) => (
<RuleReasonItem key={i}>
<OwnershipTag tagType={type} />
<OwnershipValue>{matched}</OwnershipValue>
</RuleReasonItem>
))}
</div>
{rules.length > 3 && !rulesExpanded ? (
<ViewMoreButton onClick={() => this.setState({rulesExpanded: true})} />
) : null}
</React.Fragment>
)}
</HovercardBody>
}
{...props}
/>
);
}
}
const tagColors = {
url: theme.green300,
path: theme.purple400,
tag: theme.blue300,
};
const CommitIcon = styled(p => <IconCommit {...p} />)`
margin-right: ${space(0.5)};
flex-shrink: 0;
`;
const CommitMessage = styled(({message = '', date, ...props}) => (
<div {...props}>
{message.split('\n')[0]}
<CommitDate date={date} />
</div>
))`
color: ${p => p.theme.gray800};
font-size: ${p => p.theme.fontSizeExtraSmall};
margin-top: ${space(0.25)};
hyphens: auto;
`;
const CommitDate = styled(({date, ...props}) => (
<div {...props}>{moment(date).fromNow()}</div>
))`
margin-top: ${space(0.5)};
color: ${p => p.theme.gray500};
`;
const CommitReasonItem = styled('div')`
display: flex;
align-items: flex-start;
&:not(:last-child) {
margin-bottom: ${space(1)};
}
`;
const RuleReasonItem = styled('code')`
display: flex;
align-items: flex-start;
&:not(:last-child) {
margin-bottom: ${space(1)};
}
`;
const OwnershipTag = styled(({tagType, ...props}) => <div {...props}>{tagType}</div>)`
background: ${p => tagColors[p.tagType.indexOf('tags') === -1 ? p.tagType : 'tag']};
color: ${p => p.theme.white};
font-size: ${p => p.theme.fontSizeExtraSmall};
padding: ${space(0.25)} ${space(0.5)};
margin: ${space(0.25)} ${space(0.5)} ${space(0.25)} 0;
border-radius: 2px;
font-weight: bold;
min-width: 34px;
text-align: center;
`;
const ViewMoreButton = styled(p => (
<Button {...p} priority="link" size="zero">
{t('View more')}
</Button>
))`
border: none;
color: ${p => p.theme.gray500};
font-size: ${p => p.theme.fontSizeExtraSmall};
padding: ${space(0.25)} ${space(0.5)};
margin: ${space(1)} ${space(0.25)} ${space(0.25)} 0;
width: 100%;
min-width: 34px;
`;
const OwnershipValue = styled('code')`
word-break: break-all;
line-height: 1.2;
`;
const EmailAlert = styled(p => <Alert iconSize="16px" {...p} />)`
margin: 10px -13px -13px;
border-radius: 0;
border-color: #ece0b0;
padding: 10px;
font-size: ${p => p.theme.fontSizeSmall};
font-weight: normal;
box-shadow: none;
`;
const HovercardHeader = styled('div')`
display: flex;
align-items: center;
`;
const HovercardActorAvatar = styled(p => (
<ActorAvatar size={20} hasTooltip={false} {...p} />
))`
margin-right: ${space(1)};
`;
const HovercardBody = styled('div')`
margin-top: -${space(2)};
`;
export default SuggestedOwnerHovercard;