import React from 'react'; import { connect } from 'react-redux'; import moment from 'moment'; import { showNotification, TShowNotification } from 'actions/notifications'; import { Spinner, NewTabLink } from 'components/ui'; import Modal, { IButton } from 'components/ui/Modal'; import { addListener, sendEvent } from 'utils/electron'; import EVENTS from 'shared/electronEvents'; import { bytesToHuman } from 'utils/formatters'; import './UpdateModal.scss'; export interface UpdateInfo { version: string; sha512: string; releaseDate: string; releaseName: string; releaseNotes: string; } export interface DownloadProgress { bytesPerSecond: number; percent: number; transferred: number; total: number; } interface Props { isOpen: boolean; updateInfo: UpdateInfo; showNotification: TShowNotification; handleClose(): void; } interface State { isDownloading: boolean; downloadProgress: DownloadProgress | null; } class UpdateModal extends React.Component { public state: State = { isDownloading: false, downloadProgress: null }; public componentDidMount() { addListener(EVENTS.UPDATE.UPDATE_DOWNLOADED, () => { sendEvent(EVENTS.UPDATE.QUIT_AND_INSTALL); }); addListener(EVENTS.UPDATE.DOWNLOAD_PROGRESS, downloadProgress => { this.setState({ downloadProgress }); }); addListener(EVENTS.UPDATE.ERROR, err => { console.error('Update failed:', err); this.setState({ isDownloading: false }); this.props.showNotification( 'danger', Update could not be downloaded, please visit{' '} our github {' '} to download the latest release , Infinity ); }); } public render() { const { isOpen, updateInfo, handleClose } = this.props; const { isDownloading, downloadProgress } = this.state; const buttons: IButton[] | undefined = downloadProgress ? undefined : [ { text: {isDownloading && } Download Update, type: 'primary', onClick: this.downloadUpdate, disabled: isDownloading }, { text: 'Close', type: 'default', onClick: handleClose } ]; return (
{downloadProgress ? (

Downloading...

Downloaded {downloadProgress.percent.toFixed(1)}% {bytesToHuman(downloadProgress.bytesPerSecond)}/s Total Size {bytesToHuman(downloadProgress.total)}
) : (

{updateInfo.releaseName}

{moment(updateInfo.releaseDate).format('LL')}
)}
); } private downloadUpdate = () => { this.setState({ isDownloading: true }); sendEvent('UPDATE:download-update'); }; } export default connect(undefined, { showNotification })(UpdateModal);