2021-05-12 06:35:00 +00:00
|
|
|
import React, { PureComponent, ReactNode } from 'react';
|
2021-02-02 22:23:42 +00:00
|
|
|
import { autoBindMethodsForReact } from 'class-autobind-decorator';
|
2017-11-21 17:49:33 +00:00
|
|
|
import * as electron from 'electron';
|
2021-02-02 22:23:42 +00:00
|
|
|
import { AUTOBIND_CFG } from '../../common/constants';
|
2017-11-21 17:49:33 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
interface Props {
|
|
|
|
children: ReactNode;
|
|
|
|
className?: string | null;
|
|
|
|
}
|
2017-11-21 17:49:33 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
interface State {
|
|
|
|
status: string;
|
|
|
|
checking: boolean;
|
|
|
|
updateAvailable: boolean;
|
|
|
|
}
|
2017-11-21 17:49:33 +00:00
|
|
|
|
2021-02-02 22:23:42 +00:00
|
|
|
@autoBindMethodsForReact(AUTOBIND_CFG)
|
2021-05-12 06:35:00 +00:00
|
|
|
class CheckForUpdatesButton extends PureComponent<Props, State> {
|
|
|
|
state: State = {
|
|
|
|
status: '',
|
|
|
|
checking: false,
|
|
|
|
updateAvailable: false,
|
2017-11-21 17:49:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
_listenerCheckComplete(_e, updateAvailable: true, status: string) {
|
|
|
|
this.setState({
|
|
|
|
status,
|
|
|
|
updateAvailable,
|
|
|
|
});
|
2017-11-22 21:10:34 +00:00
|
|
|
}
|
2017-11-21 17:49:33 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
_listenerCheckStatus(_e, status: string) {
|
2017-11-22 21:10:34 +00:00
|
|
|
if (this.state.checking) {
|
2021-05-12 06:35:00 +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
|
|
|
}
|
|
|
|
|
2020-02-18 17:44:08 +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 (
|
2021-05-12 06:35:00 +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;
|