-
Notifications
You must be signed in to change notification settings - Fork 45
/
Task.tsx
231 lines (214 loc) · 8.6 KB
/
Task.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
import * as React from 'react';
import startCase from 'lodash-es/startCase';
import upperCase from 'lodash-es/upperCase';
import { DownloadStationTask } from 'synology-typescript-api';
import * as classNamesProxy from 'classnames';
// https://github.com/rollup/rollup/issues/1267
const classNames: typeof classNamesProxy = (classNamesProxy as any).default || classNamesProxy;
import { formatMetric1024, formatTime, formatPercentage } from '../common/format';
import { CallbackResponse } from './popupTypes';
import { matchesFilter } from './filtering';
export interface Props {
task: DownloadStationTask;
onDelete?: (taskId: string) => Promise<CallbackResponse>;
onPause?: (taskId: string) => Promise<CallbackResponse>;
onResume?: (taskId: string) => Promise<CallbackResponse>;
}
export interface State {
pauseResumeState: 'none' | 'in-progress' | { failMessage: string };
deleteState: 'none' | 'in-progress' | CallbackResponse;
}
export class Task extends React.PureComponent<Props, State> {
state: State = {
pauseResumeState: 'none',
deleteState: 'none',
};
render() {
if (this.state.deleteState === 'success') {
return null;
} else {
const isErrored = matchesFilter(this.props.task, 'errored');
return (
<li className='task' key={this.props.task.id}>
<div className='header'>
<div className='name-and-status'>
<div className='name' title={this.props.task.title}>
{this.props.task.title}
</div>
<div className='status'>
{this.renderStatus()}
</div>
</div>
{this.renderPauseResumeButton()}
{this.renderRemoveButton()}
</div>
<div className='progress-bar'>
<div
className={classNames('bar-fill', {
'in-progress': matchesFilter(this.props.task, 'downloading' ),
'completed': matchesFilter(this.props.task, 'uploading') || matchesFilter(this.props.task, 'completed'),
'errored': isErrored,
'unknown': matchesFilter(this.props.task, 'other')
})}
style={{ width: `${(isErrored ? 1 : this.computeFractionComplete()) * 100}%` }}
/>
<div className='bar-background'/>
</div>
</li>
);
}
}
private computeFractionComplete() {
const fractionComplete = Math.round(this.props.task.additional!.transfer!.size_downloaded / this.props.task.size * 100) / 100;
return Number.isFinite(fractionComplete) ? fractionComplete : 0;
}
private computeSecondsRemaining(): number | undefined {
const secondsRemaining = Math.round((this.props.task.size - this.props.task.additional!.transfer!.size_downloaded) / this.props.task.additional!.transfer!.speed_download);
return Number.isFinite(secondsRemaining) ? secondsRemaining : undefined;
}
private renderStatus() {
const renderStatusLine = (iconName: string, subtitle: React.ReactChild) => {
return (
<span title={startCase(this.props.task.status)}>
<span className={classNames('status-icon', iconName)}/>
<span className='text'>{subtitle}</span>
</span>
);
};
if (matchesFilter(this.props.task, 'downloading')) {
const fraction = this.computeFractionComplete();
const eta = this.computeSecondsRemaining();
return renderStatusLine(
'fa fa-arrow-down',
browser.i18n.getMessage('ZpercentZ_ZcurrentZ_of_ZtotalZ_at_ZspeedZ', [
(Number.isFinite(fraction) ? formatPercentage(fraction) : '0%'),
`${formatMetric1024(this.props.task.additional!.transfer!.size_downloaded)}B`,
`${formatMetric1024(this.props.task.size)}B`,
`${formatMetric1024(this.props.task.additional!.transfer!.speed_download)}B/s`
]) + (Number.isFinite(eta as number)
? browser.i18n.getMessage('_ZetaZ_remaining', [ formatTime(eta as number) ])
: browser.i18n.getMessage('_no_estimate'))
);
} else if (matchesFilter(this.props.task, 'uploading')) {
return renderStatusLine(
'fa fa-arrow-up',
browser.i18n.getMessage('ZtotalZ_uploaded_ZspeedZ', [
`${formatMetric1024(this.props.task.additional!.transfer!.size_uploaded)}B`,
`${formatMetric1024(this.props.task.additional!.transfer!.speed_upload)}B/s`,
])
);
} else if (matchesFilter(this.props.task, 'completed')) {
return renderStatusLine(
'fa fa-check',
browser.i18n.getMessage('ZtotalZ_downloaded', [
`${formatMetric1024(this.props.task.size)}B`
])
);
} else if (matchesFilter(this.props.task, 'errored')) {
return renderStatusLine(
'fa fa-exclamation-circle',
<span className='intent-error'>
<span className='fa fa-exclamation-triangle error-icon'/>
{upperCase(this.props.task.status)} {this.props.task.status_extra ? `\u2013 ${startCase(this.props.task.status_extra.error_detail)}` : ''}
</span>
);
} else {
return renderStatusLine(
'far fa-clock',
browser.i18n.getMessage('ZcurrentZ_of_ZtotalZ_downloaded', [
`${formatMetric1024(this.props.task.additional!.transfer!.size_downloaded)}B`,
`${formatMetric1024(this.props.task.size)}B`
])
);
}
}
private renderPauseResumeButton() {
const renderButton = (title: string | undefined, state: 'resumable' | 'pausable' | 'in-progress' | 'failed') => {
const isDisabled =
this.props.onPause == null ||
this.props.onResume == null ||
this.state.deleteState === 'in-progress' ||
this.state.pauseResumeState === 'in-progress' ||
(this.state.pauseResumeState !== 'success' && this.state.pauseResumeState !== 'none');
return (
<button
onClick={this.makePauseResume(state === 'resumable' ? 'resume' : 'pause')}
title={title}
disabled={isDisabled}
className={classNames('pause-resume-button', { 'disabled': isDisabled })}
>
<div className={classNames('fa', {
'fa-pause': state === 'pausable',
'fa-play': state === 'resumable',
'fa-sync fa-spin': state === 'in-progress',
'fa-exclamation': state === 'failed'
})}/>
</button>
);
};
if (this.state.pauseResumeState === 'in-progress') {
return renderButton(undefined, 'in-progress');
} else if (this.state.pauseResumeState === 'none') {
if (this.props.task.status === 'paused') {
return renderButton(browser.i18n.getMessage('Resume'), 'resumable');
} else if (this.props.task.status === 'finished') {
return renderButton(browser.i18n.getMessage('Start_seeding'), 'resumable');
} else {
return renderButton(browser.i18n.getMessage('Pause'), 'pausable');
}
} else {
return renderButton(this.state.pauseResumeState.failMessage, 'failed');
}
}
private renderRemoveButton() {
let title: string = '';
let disabled: boolean = this.state.pauseResumeState === 'in-progress';
if (this.props.onDelete == null || this.state.deleteState === 'in-progress') {
title = browser.i18n.getMessage('Remove_download');
disabled = true;
} else if (this.state.deleteState === 'none') {
title = browser.i18n.getMessage('Remove_download');
} else if (this.state.deleteState !== 'success') {
title = this.state.deleteState.failMessage;
disabled = true;
}
return (
<button
onClick={this.deleteTask}
title={title}
disabled={disabled}
className={classNames('remove-button', { 'disabled': disabled })}
>
<div className={classNames('fa', {
'fa-times': this.state.deleteState !== 'in-progress',
'fa-sync fa-spin': this.state.deleteState === 'in-progress'
})}/>
</button>
);
}
private makePauseResume(what: 'pause' | 'resume') {
return () => {
this.setState({
pauseResumeState: 'in-progress'
});
(what === 'pause' ? this.props.onPause! : this.props.onResume!)(this.props.task.id)
.then(response => {
this.setState({
// This is a little gross, but here we just unset the state and fall back onto whatever this.props.task states.
pauseResumeState: response === 'success' ? 'none' : response
});
});
};
}
private deleteTask = () => {
this.setState({
deleteState: 'in-progress'
});
this.props.onDelete!(this.props.task.id)
.then(response => {
this.setState({
deleteState: response
});
})
};
}