insomnia/packages/insomnia-app/app/ui/components/check-for-updates-button.js

66 lines
1.6 KiB
JavaScript
Raw Normal View History

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