-
Notifications
You must be signed in to change notification settings - Fork 27
/
OnCallViews.jsx
415 lines (353 loc) · 10.2 KB
/
OnCallViews.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// ------- Imports -------------------------------------------------------------
import { DateTime } from 'luxon';
import PropTypes from 'prop-types';
import React from 'react';
// ------- Internal imports ----------------------------------------------------
import { OnCall } from '../../../models/OnCall';
import { Incident } from '../../../models/Incident';
import { PagerBeautyFetchNotFoundUiError } from '../ui-errors';
import { StatusIndicatorView } from './StatusIndicatorView';
// ------- OnCallView ----------------------------------------------------------
export class OnCallView extends React.Component {
render() {
const { isLoaded, data, error, isFetching } = this.props;
const is404 = error instanceof PagerBeautyFetchNotFoundUiError;
// Handle cases prior to first successful data load.
if (!isLoaded && !is404) {
if (error) {
// Data hasn't been loaded even once, got an error.
return <span>{`Loading error: ${error.message}`}</span>;
}
// Still loading.
return <span>Loading...</span>;
}
// Not first load:
// Ignore errors and show stale content after first successful data load.
// 404s should reset content
// @todo: update report errors
let onCall;
let userInfo;
let state;
if (!is404) {
onCall = new OnCall(data);
userInfo = {
name: onCall.userName,
url: onCall.userURL,
avatar: onCall.userAvatarSized(),
};
state = onCall.incident ? 'active_incident' : 'normal';
} else {
state = 'not_found';
}
// Resolve what to show on status row.
let statusRow;
switch (state) {
case 'not_found':
statusRow = <OnCallStatusRowView />;
break;
case 'active_incident':
statusRow = (
<OnCallStatusRowView>
<OnCallIncidentRowView incident={onCall.incident} />
</OnCallStatusRowView>
);
break;
default:
statusRow = (
<OnCallStatusRowView>
<OnCallDatesRowView onCall={onCall} />
</OnCallStatusRowView>
);
}
return (
<div className={`schedule state_${state}`}>
{ /* Header */ }
<OnCallScheduleRowView filled>
<span>ON CALL</span>
<OnCallStatusIndicatorView error={error} isFetching={isFetching} />
</OnCallScheduleRowView>
{ /* Schedule name */ }
{state !== 'not_found' && (
<OnCallScheduleRowView>
<a href={onCall.schedule.url} className="schedule_name">{onCall.schedule.name}</a>
</OnCallScheduleRowView>
)}
{ /* User info */ }
<OnCallScheduleRowView equalSpacing>
<OnCallUserInfoView userInfo={userInfo} />
</OnCallScheduleRowView>
{ /* Status row */ }
{statusRow}
{ /* End */ }
</div>
);
}
}
OnCallView.propTypes = {
isLoaded: PropTypes.bool,
isFetching: PropTypes.bool,
data: PropTypes.object, // eslint-disable-line react/forbid-prop-types
error: PropTypes.instanceOf(Error),
};
OnCallView.defaultProps = {
isLoaded: false,
isFetching: false,
data: null,
error: null,
};
// ------- OnCallScheduleRowView -----------------------------------------------
export class OnCallScheduleRowView extends React.Component {
render() {
const { equalSpacing, filled, statusRow, children } = this.props;
const classes = ['schedule_row'];
if (equalSpacing) {
classes.push('equal_spacing');
}
if (filled) {
classes.push('filled_row');
}
if (statusRow) {
classes.push('status_row');
}
return <div className={classes.join(' ')}>{children}</div>;
}
}
OnCallScheduleRowView.propTypes = {
equalSpacing: PropTypes.bool,
filled: PropTypes.bool,
statusRow: PropTypes.bool,
children: PropTypes.node,
};
OnCallScheduleRowView.defaultProps = {
equalSpacing: false,
filled: false,
statusRow: false,
children: null,
};
// ------- OnCallStatusRowView -------------------------------------------------
export class OnCallStatusRowView extends React.Component {
render() {
const { children } = this.props;
return (
<OnCallScheduleRowView filled equalSpacing statusRow>
{children}
</OnCallScheduleRowView>
);
}
}
OnCallStatusRowView.propTypes = {
children: PropTypes.node,
};
OnCallStatusRowView.defaultProps = {
children: null,
};
// ------- OnCallStatusIndicatorView -------------------------------------------
export class OnCallStatusIndicatorView extends React.Component {
constructor(props) {
super(props);
this.state = {
isBlinking: false,
};
this.timeoutId = false;
}
componentDidUpdate(prevProps) {
// Blink for next 3 seconds when swithcing state
const { isFetching } = this.props;
if (!prevProps.isFetching && isFetching) {
this.registerBlink();
}
}
componentWillUnmount() {
if (this.timeoutId) {
clearTimeout(this.timeoutId);
}
}
registerBlink() {
// Blink for 3 seconds
const { isBlinking } = this.state;
if (isBlinking) {
return;
}
this.setState({ isBlinking: true });
this.timeoutId = setTimeout(() => {
this.setState({ isBlinking: false });
}, 2000);
}
render() {
const { error } = this.props;
const { isBlinking } = this.state;
let type = 'success';
let blink = null;
if (error instanceof PagerBeautyFetchNotFoundUiError) {
// No one on call.
type = 'error';
blink = 'slow';
} else if (error) {
// All errors
type = 'warning';
blink = 'fast';
} else {
// Success
type = 'success';
// Blink on success in post-render componentDidUpdate.
if (isBlinking) {
blink = 'fast';
}
}
let title = 'OK';
if (error) {
title = error.message || error.toString();
}
return <StatusIndicatorView type={type} blink={blink} title={title} />;
}
}
OnCallStatusIndicatorView.propTypes = {
isFetching: PropTypes.bool,
error: PropTypes.instanceOf(Error),
};
OnCallStatusIndicatorView.defaultProps = {
isFetching: false,
error: null,
};
// ------- OnCallUserInfoView --------------------------------------------------
export class OnCallUserInfoView extends React.Component {
render() {
const { userInfo } = this.props;
return (
<React.Fragment>
<div className="user_avatar">
{userInfo ? (
<a href={userInfo.url}><img src={userInfo.avatar} alt={userInfo.name} /></a>
) : (
<img src="https://www.gravatar.com/avatar/0?s=2048&d=mp" alt="Generic avatar" />
)}
</div>
<div className={`user_name ${!userInfo ? 'error' : ''}`}>
{userInfo ? (
<a href={userInfo.url}>{userInfo.name}</a>
) : (
'No one is on call'
)}
</div>
</React.Fragment>
);
}
}
OnCallUserInfoView.propTypes = {
userInfo: PropTypes.shape({
name: PropTypes.string,
url: PropTypes.string,
avatar: PropTypes.string,
}),
};
OnCallUserInfoView.defaultProps = {
userInfo: null,
};
// ------- OnCallDatesRowView -----------------------------------------------
export class OnCallDatesRowView extends React.Component {
render() {
const { onCall } = this.props;
const fromDate = (
<OnCallDateRowView
className="date_start"
label="From"
date={onCall.dateStart}
timezone={onCall.schedule.timezone}
/>
);
// dateEnd is null when there's only one user on schedule.
let toDate = null;
if (onCall.dateEnd.isValid) {
toDate = (
<OnCallDateRowView
className="date_end"
label="To"
date={onCall.dateEnd}
timezone={onCall.schedule.timezone}
/>
);
}
return (
<React.Fragment>
{fromDate}
{toDate}
</React.Fragment>
);
}
}
OnCallDatesRowView.propTypes = {
onCall: PropTypes.instanceOf(OnCall).isRequired,
};
// ------- OnCallDateTimeView --------------------------------------------------
export class OnCallDateRowView extends React.Component {
render() {
const { date, timezone, className, label } = this.props;
return (
<div className={`date ${className}`}>
<span>{`${label}: `}</span>
<OnCallDateTimeView date={date} timezone={timezone} />
</div>
);
}
}
OnCallDateRowView.propTypes = {
date: PropTypes.instanceOf(DateTime).isRequired,
timezone: PropTypes.string,
className: PropTypes.string,
label: PropTypes.string,
};
OnCallDateRowView.defaultProps = {
timezone: null,
className: '',
label: '',
};
// ------- OnCallIncidentRowView -----------------------------------------------
export class OnCallIncidentRowView extends React.Component {
render() {
const { incident, className } = this.props;
return (
<div className={`incident ${className}`}>
<div className="incident_summary">
<span>{`Incident ${incident.status}: `}</span>
<a href={incident.url}>{incident.title}</a>
</div>
<div className="incident_service">
<span>Service: </span>
<a href={incident.serviceUrl}>{incident.serviceName}</a>
</div>
</div>
);
}
}
OnCallIncidentRowView.propTypes = {
incident: PropTypes.instanceOf(Incident).isRequired,
className: PropTypes.string,
};
OnCallIncidentRowView.defaultProps = {
className: '',
};
// ------- OnCallDateTimeView --------------------------------------------------
export class OnCallDateTimeView extends React.Component {
render() {
const { date, timezone } = this.props;
let dateInTz = date;
if (timezone) {
dateInTz = date.setZone(timezone);
}
return (
<React.Fragment>
<span className="date_weekday">{`${dateInTz.toFormat('EEEE')}, `}</span>
<span className="date_date">{`${dateInTz.toFormat('MMM dd')} `}</span>
<span className="date_time">{dateInTz.toFormat('t')}</span>
</React.Fragment>
);
}
}
OnCallDateTimeView.propTypes = {
date: PropTypes.instanceOf(DateTime).isRequired,
timezone: PropTypes.string,
};
OnCallDateTimeView.defaultProps = {
timezone: null,
};
// ------- End -----------------------------------------------------------------