-
Notifications
You must be signed in to change notification settings - Fork 353
/
SelectedJob.jsx
300 lines (264 loc) · 10 KB
/
SelectedJob.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import React from 'react';
import PropTypes from 'prop-types';
import $ from 'jquery';
import { thEvents, thJobNavSelectors } from '../../helpers/constants';
import {
findGroupElement,
findGroupInstance,
findJobInstance,
findSelectedInstance,
scrollToElement,
} from '../../helpers/job';
import { getUrlParam, setUrlParam } from '../../helpers/location';
import { getJobsUrl } from '../../helpers/url';
import JobModel from '../../models/job';
import PushModel from '../../models/push';
import { withPinnedJobs } from './PinnedJobs';
import { withPushes } from './Pushes';
import { withNotifications } from '../../shared/context/Notifications';
const SelectedJobContext = React.createContext({});
class SelectedJobClass extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedJob: null,
repoName: getUrlParam('repo'),
};
this.value = {
...this.state,
setSelectedJob: this.setSelectedJob,
clearSelectedJob: this.clearSelectedJob,
changeSelectedJob: this.changeSelectedJob,
};
}
componentDidMount() {
this.setSelectedJob = this.setSelectedJob.bind(this);
this.clearSelectedJob = this.clearSelectedJob.bind(this);
this.changeSelectedJob = this.changeSelectedJob.bind(this);
this.noMoreUnclassifiedFailures = this.noMoreUnclassifiedFailures.bind(this);
this.handleApplyNewJobs = this.handleApplyNewJobs.bind(this);
// TODO: this.value needs to now get the bound versions of the functions.
// But when we move the function binding to the constructors, we won't
// have to re-do this in componentDidMount.
this.value = {
...this.state,
setSelectedJob: this.setSelectedJob,
clearSelectedJob: this.clearSelectedJob,
changeSelectedJob: this.changeSelectedJob,
};
}
componentDidUpdate(prevProps) {
const { jobsLoaded } = this.props;
if (jobsLoaded !== prevProps.jobsLoaded) {
this.setSelectedJobFromQueryString();
}
window.addEventListener(thEvents.applyNewJobs, this.handleApplyNewJobs);
}
componentWillUnmount() {
window.removeEventListener(thEvents.applyNewJobs, this.handleApplyNewJobs);
}
setValue(newState, callback) {
this.value = { ...this.value, ...newState };
this.setState(newState, callback);
}
/**
* If the URL has a query string param of ``selectedJob`` then select
* that job on load.
*
* If that job isn't in any of the loaded pushes, then throw
* an error and provide a link to load it with the right push.
*/
setSelectedJobFromQueryString() {
const { notify, jobMap } = this.props;
const { repoName, selectedJob } = this.state;
const selectedJobIdStr = getUrlParam('selectedJob');
const selectedJobId = parseInt(selectedJobIdStr, 10);
if (selectedJobIdStr && (!selectedJob || selectedJob.id !== selectedJobId)) {
const selectedJob = jobMap[selectedJobIdStr];
// select the job in question
if (selectedJob) {
this.setSelectedJob(selectedJob);
} else {
setUrlParam('selectedJob');
// If the ``selectedJob`` was not mapped, then we need to notify
// the user it's not in the range of the current result set list.
JobModel.get(repoName, selectedJobId).then((job) => {
PushModel.get(job.push_id).then(async (resp) => {
if (resp.ok) {
const push = await resp.json();
const newPushUrl = getJobsUrl({ repo: repoName, revision: push.revision, selectedJob: selectedJobId });
// the job exists, but isn't in any loaded push.
// provide a message and link to load the right push
notify(
`Selected job id: ${selectedJobId} not within current push range.`,
'danger',
{ sticky: true, linkText: 'Load push', url: newPushUrl });
} else {
throw Error(`Unable to find push with id ${job.push_id} for selected job`);
}
});
}).catch((error) => {
// the job wasn't found in the db. Either never existed,
// or was expired and deleted.
this.clearSelectedJob();
notify(`Selected Job - ${error}`,
'danger',
{ sticky: true });
});
}
} else if (!selectedJobIdStr && selectedJob) {
this.setValue({ selectedJob: null });
}
}
setSelectedJob(job, timeout = 0) {
const { selectedJob } = this.state;
if (selectedJob) {
const selected = findSelectedInstance();
if (selected) selected.setSelected(false);
}
const group = findGroupInstance(job);
if (group) {
group.setExpanded(true);
}
const newSelectedElement = findJobInstance(job.id, true);
if (newSelectedElement) {
newSelectedElement.setSelected(true);
} else {
// If the job is in a group count, then the job element won't exist, but
// its group will. We can try scrolling to that.
const groupEl = findGroupElement(job);
if (groupEl) {
scrollToElement(groupEl);
}
}
// If a timeout is passed in, this will cause a pause before
// the selection takes place. This allows for quick-switching
// with hotkeys.
if (this.jobChangedTimeout) {
window.clearTimeout(this.jobChangedTimeout);
}
this.jobChangedTimeout = window.setTimeout(() => {
this.setValue({ selectedJob: job }, () => {
setUrlParam('selectedJob', job.id);
this.jobChangedTimeout = null;
});
}, timeout);
}
clearSelectedJob() {
const { pinnedJobs } = this.props;
if (!Object.keys(pinnedJobs).length) {
this.setValue({ selectedJob: null });
setUrlParam('selectedJob', null);
const selected = findSelectedInstance();
if (selected) selected.setSelected(false);
}
}
handleApplyNewJobs(event) {
const { updatedSelectedJob } = event.detail;
if (updatedSelectedJob) {
this.setSelectedJob(updatedSelectedJob);
}
}
noMoreUnclassifiedFailures() {
const { pinnedJobs, notify } = this.props;
notify('No unclassified failures to select.');
this.clearSelectedJob(Object.keys(pinnedJobs).length);
}
changeSelectedJob(direction, unclassifiedOnly) {
const { pinnedJobs } = this.props;
const jobNavSelector = unclassifiedOnly ?
thJobNavSelectors.UNCLASSIFIED_FAILURES : thJobNavSelectors.ALL_JOBS;
// Get the appropriate next index based on the direction and current job
// selection (if any). Must wrap end to end.
const getIndex = direction === 'next' ?
(idx, jobs) => (idx + 1 > jobs.length - 1 ? 0 : idx + 1) :
(idx, jobs) => (idx - 1 < 0 ? jobs.length - 1 : idx - 1);
// TODO: (bug 1434679) Move from using jquery here to find the next/prev
// component. This could perhaps be done either with:
// * Document functions like ``querySelectorAll``, etc.
// * ReactJS with ReactDOM and props.children
// Filter the list of possible jobs down to ONLY ones in the .th-view-content
// div (excluding pinBoard) and then to the specific selector passed
// in. And then to only VISIBLE (not filtered away) jobs. The exception
// is for the .selected-job. Even if the ``visible`` field is set to false,
// this includes it because it is the anchor from which we find
// the next/previous job.
//
// The .selected-job can be ``visible: false``, but still showing to the
// user. This can happen when filtered to unclassified failures only,
// and you then classify the selected job. It's ``visible`` field is set
// to false, but it is still showing to the user because it is still
// selected. This is very important to the sheriff workflow. As soon as
// selection changes away from it, the job will no longer be visible.
const jobs = $('.th-view-content')
.find(jobNavSelector.selector)
.filter(':visible, .selected-job, .selected-count');
if (jobs.length) {
const selectedEl = jobs.filter('.selected-job, .selected-count').first();
const selIdx = jobs.index(selectedEl);
const idx = getIndex(selIdx, jobs);
const jobEl = $(jobs[idx]);
if (selIdx !== idx) {
const jobId = jobEl.attr('data-job-id');
const jobInstance = findJobInstance(jobId, true);
if (jobInstance) {
// Delay loading details for the new job right away,
// in case the user is switching rapidly between jobs
this.setSelectedJob(jobInstance.props.job, 200);
return;
}
} else {
this.noMoreUnclassifiedFailures();
}
} else {
this.noMoreUnclassifiedFailures();
}
// if there was no new job selected, then ensure that we clear any job that
// was previously selected.
if ($('.selected-job').css('display') === 'none') {
this.clearSelectedJob(Object.keys(pinnedJobs).length);
}
}
clearIfEligibleTarget(target) {
if (target.hasAttribute('data-job-clear-on-click')) {
this.clearSelectedJob();
}
}
render() {
return (
<SelectedJobContext.Provider value={this.value}>
<div
className="d-flex flex-column h-100"
onClick={evt => this.clearIfEligibleTarget(evt.target)}
>
{this.props.children}
</div>
</SelectedJobContext.Provider>
);
}
}
SelectedJobClass.propTypes = {
jobsLoaded: PropTypes.bool.isRequired,
notify: PropTypes.func.isRequired,
pinnedJobs: PropTypes.object.isRequired,
jobMap: PropTypes.object.isRequired,
children: PropTypes.object.isRequired,
};
export const SelectedJob = withNotifications(withPushes(withPinnedJobs(SelectedJobClass)));
export function withSelectedJob(Component) {
return function SelectedJobComponent(props) {
return (
<SelectedJobContext.Consumer>
{context => (
<Component
{...props}
selectedJob={context.selectedJob}
setSelectedJob={context.setSelectedJob}
clearSelectedJob={context.clearSelectedJob}
changeSelectedJob={context.changeSelectedJob}
/>
)}
</SelectedJobContext.Consumer>
);
};
}