-
Notifications
You must be signed in to change notification settings - Fork 87
/
AirgapUploadProgress.jsx
224 lines (200 loc) · 8.5 KB
/
AirgapUploadProgress.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
import React from "react"
import { withRouter } from "react-router-dom";
import Loader from "./shared/Loader";
import { formatByteSize, calculateTimeDifference, Utilities } from "@src/utilities/utilities";
import { Repeater } from "@src/utilities/repeater";
import "@src/scss/components/AirgapUploadProgress.scss";
import get from "lodash/get";
let processingImages = null;
class AirgapUploadProgress extends React.Component {
constructor(props) {
super(props);
this.state = {
installStatus: "",
currentMessage: "",
getAirgapInstallStatusJob: new Repeater(),
};
}
componentDidMount() {
processingImages = null;
this.getAirgapInstallStatus();
}
componentWillUnmount() {
this.state.getAirgapInstallStatusJob.stop();
}
getAirgapInstallStatus = async () => {
try {
const res = await fetch(`${window.env.API_ENDPOINT}/app/airgap/status`, {
headers: {
"Authorization": Utilities.getToken(),
"Content-Type": "application/json",
},
method: "GET",
});
const response = await res.json();
this.setState({
installStatus: response.installStatus,
currentMessage: response.currentMessage,
});
} catch(err) {
console.log(err);
}
}
render() {
const { total, sent, onProgressError, onProgressSuccess, smallSize } = this.props;
const { installStatus, currentMessage } = this.state;
if (installStatus === "installed") {
// this conditional is really awkward but im keeping the functionality the same
if (!smallSize) {
this.state.getAirgapInstallStatusJob.stop();
}
if (onProgressSuccess) {
onProgressSuccess();
}
if (!smallSize) {
return null;
}
}
const hasError = installStatus === "airgap_upload_error";
if (hasError) {
this.state.getAirgapInstallStatusJob.stop();
onProgressError(currentMessage);
return null;
}
if (this.props.unkownProgress) {
return (
<div>
<Loader className="flex justifyContent--center" size="32" />
<p className="u-marginTop--10 u-fontSize--small u-color--dustyGray u-fontWeight--medium" style={{ maxWidth: 200 }}>
This may take a while depending on your network connection and size of your bundle
</p>
</div>
)
}
let progressBar;
let percentage;
let uploadComplete;
if (total > 0 && sent > 0) {
uploadComplete = sent === total
percentage = (sent / total * 100).toFixed(2) + "%";
progressBar = (
<div className={`progressbar ${smallSize ? "small" : ""}`}>
<div className={`progressbar-meter ${uploadComplete ? "complete" : ""}`} style={{ width: `${(sent / total) * (smallSize ? 100 : 600)}px` }} />
</div>
);
} else {
percentage = "0%";
progressBar = (
<div className={`progressbar ${smallSize ? "small" : ""}`}>
<div className="progressbar-meter" style={{ width: "0px" }} />
</div>
);
}
this.state.getAirgapInstallStatusJob.start(this.getAirgapInstallStatus, 1000);
let statusMsg = currentMessage;
try {
// Some of these messages will be JSON formatted progress reports.
const jsonMessage = JSON.parse(statusMsg);
const type = get(jsonMessage, "type");
if (type === "progressReport") {
statusMsg = jsonMessage.compatibilityMessage;
processingImages = jsonMessage.images.sort((a, b) => (a.displayName < b.displayName) ? -1 : 1);
}
} catch {
// empty
}
let statusDiv = (
<div
className={`u-marginTop--20 u-fontWeight--medium u-lineHeight--medium u-textAlign--center`}
>
<div className="flex flex1 u-marginBottom--10 justifyContent--center alignItems--center u-color--tundora">
{statusMsg && <Loader className="flex u-marginRight--5" size="24" />}
<p>{statusMsg}</p>
</div>
<p className="u-color--dustyGray">This may take a while depending on your network connection and size of your bundle</p>
</div>
);
if (smallSize) {
statusDiv = statusMsg && (
<div
className={`u-marginTop--10 u-paddingRight--30 u-color--dustyGray u-fontWeight--bold u-lineHeight--medium u-textAlign--center`}
style={{ maxWidth: 200 }}
>
{statusMsg.substring(0, 30) + "..."}
</div>
);
}
return (
<div className="AirgapUploadProgress--wrapper flex-1-auto flex-column alignItems--center justifyContent--center">
<div className="flex1 flex-column u-color--tuna">
{processingImages ?
<div className="flex1 flex-column alignItems--center justifyContent--center">
<div className="flex-auto">
<h1 className="u-fontSize--larger u-fontWeight--bold u-marginBottom--10 u-textAlign--center">
Pushing {processingImages?.length} image{processingImages?.length === 1 ? "" : "s"} to your registry
</h1>
{processingImages?.map((image, i) => {
let imageProgressBar;
let percentage;
if (image.total > 0 && image.current > 0) {
percentage = Math.floor((image.current / image.total) * 100).toFixed() + "%";
imageProgressBar = (
<div className="progressbar">
<div className={`progressbar-meter ${image.status === "uploaded" ? "complete" : ""}`} style={{ width: `${(image.current / image.total) * (600)}px` }} />
</div>
);
} else {
percentage = "0%";
imageProgressBar = (
<div className="progressbar u-opacity--half">
<div className={`progressbar-meter ${image.status === "uploaded" ? "complete" : ""}`} style={{ width: "0px" }} />
</div>
);
}
let currentMessage = "Waiting to start";
if (image.error !== "") {
currentMessage = image.error;
} else if (image.status === "uploaded") {
const completedTime = calculateTimeDifference(image.startTime, image.endTime);
currentMessage = `Completed in ${completedTime}`;
} else if (image.status === "uploading") {
currentMessage = statusMsg;
}
return (
<div key={`${image.displayName}-${i}`} className="flex1 u-marginTop--20">
<div className="flex flex1 alignItems--center">
<p className={`u-fontWeight--bold u-fontSize--normal u-color--tundora u-marginRight--10 u-textAlign--right flex1 ${image.status === "queued" ? "u-opacity--half" : ""}`}>{image.displayName}</p>
{imageProgressBar}
{image.status === "uploaded" ? <span className="u-marginLeft--10 icon checkmark-icon" /> : <span className="u-fontWeight--medium u-fontSize--normal u-color--tundora u-marginLeft--10">{percentage}</span>}
</div>
<div className="u-marginTop--5">
{currentMessage ? <p className="u-textAlign--center u-fontSize--small u-fontWeight--medium u-color--dustyGray">{currentMessage}</p> : <p className="u-fontSize--small"></p>}
</div>
</div>
)
})}
</div>
</div>
:
<div className="flex1 flex-column alignItems--center justifyContent--center">
<h1 className={`${smallSize ? "u-fontSize--large" : "u-fontSize--larger"} u-fontWeight--bold u-marginBottom--10`}>
Uploading your airgap bundle
</h1>
<div className="flex alignItems--center u-marginTop--20">
<span className="u-fontWeight--bold u-fontSize--normal u-color--tundora u-marginRight--10">{percentage}</span>
{progressBar}
{uploadComplete ? <span className="u-marginLeft--10 icon checkmark-icon" /> : <span className="u-fontWeight--medium u-fontSize--normal u-color--tundora u-marginLeft--10">{formatByteSize(total)}</span>}
</div>
{statusDiv}
</div>
}
</div>
</div>
);
}
}
AirgapUploadProgress.defaultProps = {
total: 0,
sent: 0
};
export default withRouter(AirgapUploadProgress);