/
JobGroup.jsx
200 lines (181 loc) · 5.87 KB
/
JobGroup.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
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
import React from 'react';
import PropTypes from 'prop-types';
import countBy from 'lodash/countBy';
import { withSelectedJob } from '../context/SelectedJob';
import { thFailureResults } from '../../helpers/constants';
import { getBtnClass, getStatus } from '../../helpers/job';
import { getUrlParam } from '../../helpers/location';
import JobButton from './JobButton';
import JobCount from './JobCount';
const GroupSymbol = function GroupSymbol(props) {
const { symbol, tier, toggleExpanded } = props;
return (
<button type="button" className="btn group-symbol" onClick={toggleExpanded}>
{symbol}
{tier !== 1 && <span className="small text-muted">[tier {tier}]</span>}
</button>
);
};
GroupSymbol.propTypes = {
symbol: PropTypes.string.isRequired,
toggleExpanded: PropTypes.func.isRequired,
tier: PropTypes.number,
};
GroupSymbol.defaultProps = {
tier: 1,
};
export class JobGroupComponent extends React.Component {
constructor(props) {
super(props);
const { pushGroupState } = this.props;
// The group should be expanded initially if the global group state is expanded
const groupState = getUrlParam('group_state');
this.state = {
expanded: groupState === 'expanded' || pushGroupState === 'expanded',
};
}
static getDerivedStateFromProps(nextProps, state) {
// We should expand this group if it's own state is set to be expanded,
// or if the push was set to have all groups expanded.
return {
expanded: state.expanded || nextProps.pushGroupState === 'expanded',
};
}
setExpanded(isExpanded) {
this.setState({ expanded: isExpanded });
}
toggleExpanded = () => {
this.setState({ expanded: !this.state.expanded });
};
groupButtonsAndCounts(jobs, expanded) {
const {
selectedJob,
duplicateJobsVisible,
groupCountsExpanded,
} = this.props;
let buttons = [];
const counts = [];
if (expanded || groupCountsExpanded) {
// All buttons should be shown when the group is expanded
buttons = jobs;
} else {
const stateCounts = {};
const typeSymbolCounts = countBy(jobs, 'job_type_symbol');
jobs.forEach(job => {
if (!job.visible) return;
const status = getStatus(job);
let countInfo = {
btnClass: getBtnClass(status, job.failure_classification_id),
countText: status,
};
if (
thFailureResults.includes(status) ||
(typeSymbolCounts[job.job_type_symbol] > 1 && duplicateJobsVisible)
) {
// render the job itself, not a count
buttons.push(job);
} else {
countInfo = { ...countInfo, ...stateCounts[countInfo.btnClass] };
if (
(selectedJob && selectedJob.id === job.id) ||
countInfo.selectedClasses
) {
countInfo.selectedClasses = ' selected-count btn-lg-xform';
} else {
countInfo.selectedClasses = '';
}
if (stateCounts[countInfo.btnClass]) {
countInfo.count = stateCounts[countInfo.btnClass].count + 1;
} else {
countInfo.count = 1;
}
countInfo.lastJob = job;
stateCounts[countInfo.btnClass] = countInfo;
}
});
Object.entries(stateCounts).forEach(([, countInfo]) => {
if (countInfo.count === 1) {
buttons.push(countInfo.lastJob);
} else {
counts.push(countInfo);
}
});
}
return { buttons, counts };
}
render() {
const {
repoName,
filterPlatformCb,
platform,
filterModel,
group: {
name: groupName,
symbol: groupSymbol,
tier: groupTier,
jobs: groupJobs,
mapKey: groupMapKey,
},
} = this.props;
const { expanded } = this.state;
const { buttons, counts } = this.groupButtonsAndCounts(groupJobs, expanded);
return (
<span className="platform-group" data-group-key={groupMapKey}>
<span className="disabled job-group" title={groupName}>
<GroupSymbol
symbol={groupSymbol}
tier={groupTier}
toggleExpanded={this.toggleExpanded}
/>
<span className="group-content">
<span className="group-job-list">
{buttons.map(job => (
<JobButton
job={job}
filterModel={filterModel}
visible={job.visible}
resultStatus={getStatus(job)}
failureClassificationId={job.failure_classification_id}
repoName={repoName}
filterPlatformCb={filterPlatformCb}
platform={platform}
key={job.id}
/>
))}
</span>
<span className="group-count-list">
{counts.map(countInfo => (
<JobCount
count={countInfo.count}
onClick={this.toggleExpanded}
className={`${countInfo.btnClass}-count${
countInfo.selectedClasses
}`}
title={`${countInfo.count} ${
countInfo.countText
} jobs in group`}
key={countInfo.lastJob.id}
/>
))}
</span>
</span>
</span>
</span>
);
}
}
JobGroupComponent.propTypes = {
group: PropTypes.object.isRequired,
repoName: PropTypes.string.isRequired,
filterModel: PropTypes.object.isRequired,
filterPlatformCb: PropTypes.func.isRequired,
platform: PropTypes.object.isRequired,
pushGroupState: PropTypes.string.isRequired,
duplicateJobsVisible: PropTypes.bool.isRequired,
groupCountsExpanded: PropTypes.bool.isRequired,
selectedJob: PropTypes.object,
};
JobGroupComponent.defaultProps = {
selectedJob: null,
};
export default withSelectedJob(JobGroupComponent);