File size: 11,328 Bytes
8fd7a1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import 'regenerator-runtime/runtime';
import {FormattedMessage} from 'react-intl';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import React from 'react';
import bindAll from 'lodash.bindall';
import keyMirror from 'keymirror';

import BalancedFormattedMessage from '../../containers/balanced-formatted-message.jsx';
import Box from '../box/box.jsx';
import ProgressRingComponent from '../progress-ring/progress-ring.jsx';

import backIcon from './icons/back.svg';
import sendUpdateIcon from './icons/send-update.svg';
import sendUpdateGlyph from './icons/send-update-white.svg';

import styles from './connection-modal.css';

/** @enum{string} UPDATE_ACTIVITY */
const UPDATE_ACTIVITY = keyMirror({
    getReady: null,
    sendUpdate: null,
    results: null
});

const microBitFirmwareUrl = 'https://microbit.org/get-started/user-guide/firmware/';

class UpdatePeripheralStep extends React.Component {
    constructor (props) {
        super(props);
        bindAll(this, [
            'handleSendUpdate'
        ]);
        this.state = {
            /** @type {UPDATE_ACTIVITY} */
            activity: UPDATE_ACTIVITY.getReady,

            /** @type {number} */
            progressPercentage: 0,

            /** @type {Error?} */
            err: null,

            /** @type {any} */
            res: null
        };
    }

    async handleSendUpdate () {
        this.setState({
            activity: UPDATE_ACTIVITY.sendUpdate,
            progress: 0,
            err: null,
            res: null
        });
        try {
            const res = await this.props.onSendPeripheralUpdate(progress => {
                // On my computer, I get a progress update every 0.005% or so.
                // Rendering the progress ring is a little expensive, so filtering updates here reduces the CPU load.
                // Updating every 1% doesn't look very smooth, but 0.5% (1/200) looks good to me.
                this.setState({progressPercentage: Math.floor(progress * 200) / 2});
            });
            this.setState({
                activity: UPDATE_ACTIVITY.results,
                res
            });
        } catch (err) {
            this.setState({
                activity: UPDATE_ACTIVITY.results,
                err
            });
        }
    }

    renderGetReady () {
        return (<Box className={styles.activityArea}>
            <Box className={styles.scratchLinkHelp}>
                <Box className={styles.scratchLinkHelpStep}>
                    <Box className={styles.helpStepNumber}>
                        {'1'}
                    </Box>
                    <img
                        className={styles.helpStepImage}
                        src={this.props.connectionSmallIconURL}
                        draggable={false}
                    />
                    {
                        // The instructions for getting the peripheral ready for the update process will vary
                        // depending on the peripheral. Should we get this from the extension somehow?
                    }
                    <FormattedMessage
                        className={styles.helpStepText}
                        defaultMessage="Connect your {extensionName} to this device using a USB cable."
                        description="Instructions to connect the micro:bit to the computer for the update process"
                        id="gui.connection.updatePeripheral.microBitConnect"
                        values={{
                            extensionName: this.props.name
                        }}
                    />
                </Box>
                <Box className={styles.scratchLinkHelpStep}>
                    <Box className={styles.helpStepNumber}>
                        {'2'}
                    </Box>
                    <img
                        className={styles.helpStepImage}
                        src={sendUpdateIcon}
                        draggable={false}
                    />
                    <FormattedMessage
                        defaultMessage="Press &quot;Do Update&quot; and allow the update to complete."
                        description="Instructions to press the button to begin the update process"
                        id="gui.connection.updatePeripheral.pressUpdate"
                    />
                </Box>
            </Box>
        </Box>);
    }

    renderSendUpdate () {
        return (<Box className={styles.activityArea}>
            <ProgressRingComponent
                sizePx={36}
                value={this.state.progressPercentage}
                max={100}
            />
            <FormattedMessage
                defaultMessage="Updating {progressPercentage}%"
                description="Progress message while updating the peripheral"
                id="gui.connection.updatePeripheral.progress"
                values={{
                    progressPercentage: Math.floor(this.state.progressPercentage)
                }}
            />
        </Box>);
    }

    renderResults () {
        let resultsContent;
        if (this.state.err === null) {
            resultsContent = (<FormattedMessage
                defaultMessage="Update successful!"
                description="Message to indicate that the peripheral update was successful"
                id="gui.connection.updatePeripheral.updateSuccessful"
            />);
        } else if (this.state.err.message === 'No valid interfaces found.') {
            // this is a special case where the micro:bit's communication firmware is too old to support WebUSB
            resultsContent = (<BalancedFormattedMessage
                defaultMessage="Please visit this link to update your micro:bit firmware: {microBitFirmwareLink}"
                description="Message to indicate that the special micro:bit interface firmware needs to be updated"
                id="gui.connection.updatePeripheral.updateMicroBitFirmware"
                values={{
                    microBitFirmwareLink: <a
                        rel="noopener noreferrer"
                        target="_blank"
                        href={microBitFirmwareUrl}
                    >
                        {microBitFirmwareUrl}
                    </a>
                }}
            />);
        } else {
            resultsContent = (
                <Box className={styles.scratchLinkError}>
                    <FormattedMessage
                        className={styles.centeredRow}
                        defaultMessage="Update failed."
                        description="Message to indicate that the peripheral update failed"
                        id="gui.connection.updatePeripheral.updateFailed"
                    />
                    <textarea
                        className={styles.scratchLinkErrorDetails}
                        readOnly
                    >
                        {this.state.err.message}
                    </textarea>
                </Box>
            );
        }
        return (<Box className={styles.activityArea}>
            {resultsContent}
        </Box>);
    }

    render () {
        const showGetReady = this.state.activity === UPDATE_ACTIVITY.getReady;
        const showSendUpdate = this.state.activity === UPDATE_ACTIVITY.sendUpdate;
        const showResults = this.state.activity === UPDATE_ACTIVITY.results;
        const showBadResults = showResults && !!this.state.err;
        return (
            <Box className={styles.body}>
                {showGetReady && this.renderGetReady()}
                {showSendUpdate && this.renderSendUpdate()}
                {showResults && this.renderResults()}
                <Box className={styles.bottomArea}>
                    {!showResults &&
                        <BalancedFormattedMessage
                            className={styles.bottomAreaItem}
                            defaultMessage={'Do not leave or reload Scratch or disconnect your {extensionName} ' +
                                'until the update is complete.'}
                            description="Notice to not disrupt the peripheral update process"
                            id="gui.connection.updatePeripheral.doNotDisconnect"
                            values={{
                                extensionName: this.props.name
                            }}
                        />
                    }
                    {!showSendUpdate &&
                        <Box className={classNames(styles.bottomAreaItem, styles.buttonRow)}>
                            <button
                                className={styles.connectionButton}
                                onClick={this.props.onScanning}
                            >
                                <img
                                    className={classNames(styles.buttonIconLeft, styles.buttonIconBack)}
                                    src={backIcon}
                                    draggable={false}
                                />
                                <FormattedMessage
                                    defaultMessage="Go Back"
                                    description="Button to leave the peripheral update process"
                                    id="gui.connection.updatePeripheral.goBackButton"
                                />
                            </button>
                            {(showGetReady || showBadResults) &&
                                <button
                                    className={styles.connectionButton}
                                    onClick={this.handleSendUpdate}
                                >
                                    {showGetReady &&
                                        <FormattedMessage
                                            defaultMessage="Do Update"
                                            description="Button to start the peripheral update"
                                            id="gui.connection.updatePeripheral.updateNowButton"
                                        />
                                    }
                                    {showBadResults &&
                                        <FormattedMessage
                                            defaultMessage="Try Again"
                                            description="Button to try the peripheral update again"
                                            id="gui.connection.updatePeripheral.updateAgainButton"
                                        />
                                    }
                                    <img
                                        className={styles.buttonIconRight}
                                        src={sendUpdateGlyph}
                                        draggable={false}
                                    />
                                </button>
                            }
                        </Box>
                    }
                </Box>
            </Box>
        );
    }
}

UpdatePeripheralStep.propTypes = {
    connectionSmallIconURL: PropTypes.string,
    name: PropTypes.string.isRequired,
    onScanning: PropTypes.func.isRequired,
    onSendPeripheralUpdate: PropTypes.func.isRequired
};

export default UpdatePeripheralStep;