2017-11-21 17:49:33 +00:00
|
|
|
// @flow
|
|
|
|
import * as React from 'react';
|
|
|
|
import autobind from 'autobind-decorator';
|
|
|
|
import * as electron from 'electron';
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
children: React.Node,
|
2018-12-12 17:36:11 +00:00
|
|
|
className: ?string,
|
2017-11-21 17:49:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
status: string,
|
|
|
|
checking: boolean,
|
2018-12-12 17:36:11 +00:00
|
|
|
updateAvailable: boolean,
|
2017-11-21 17:49:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
class CheckForUpdatesButton extends React.PureComponent<Props, State> {
|
2018-06-25 17:42:50 +00:00
|
|
|
constructor(props: Props) {
|
2017-11-21 17:49:33 +00:00
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
status: '',
|
|
|
|
checking: false,
|
2018-12-12 17:36:11 +00:00
|
|
|
updateAvailable: false,
|
2017-11-21 17:49:33 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_listenerCheckComplete(e: any, updateAvailable: true, status: string) {
|
|
|
|
this.setState({ status, updateAvailable });
|
2017-11-22 21:10:34 +00:00
|
|
|
}
|
2017-11-21 17:49:33 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_listenerCheckStatus(e: any, status: string) {
|
2017-11-22 21:10:34 +00:00
|
|
|
if (this.state.checking) {
|
2018-06-25 17:42:50 +00:00
|
|
|
this.setState({ status });
|
2017-11-22 21:10:34 +00:00
|
|
|
}
|
2017-11-21 17:49:33 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_handleCheckForUpdates() {
|
2017-11-21 17:49:33 +00:00
|
|
|
electron.ipcRenderer.send('updater.check');
|
2018-06-25 17:42:50 +00:00
|
|
|
this.setState({ checking: true });
|
2017-11-21 17:49:33 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
componentDidMount() {
|
2017-11-22 21:10:34 +00:00
|
|
|
electron.ipcRenderer.on('updater.check.status', this._listenerCheckStatus);
|
2018-10-17 16:42:33 +00:00
|
|
|
electron.ipcRenderer.on('updater.check.complete', this._listenerCheckComplete);
|
2017-11-22 21:10:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
componentWillUnmount() {
|
2018-10-17 16:42:33 +00:00
|
|
|
electron.ipcRenderer.removeListener('updater.check.complete', this._listenerCheckComplete);
|
|
|
|
electron.ipcRenderer.removeListener('updater.check.status', this._listenerCheckStatus);
|
2017-11-22 21:10:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
render() {
|
|
|
|
const { children, className } = this.props;
|
|
|
|
const { status, checking } = this.state;
|
2017-11-21 17:49:33 +00:00
|
|
|
|
|
|
|
return (
|
2018-10-17 16:42:33 +00:00
|
|
|
<button className={className} disabled={checking} onClick={this._handleCheckForUpdates}>
|
2017-11-22 22:43:10 +00:00
|
|
|
{status || children}
|
2017-11-21 17:49:33 +00:00
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default CheckForUpdatesButton;
|