import * as React from 'react' import { Button, IconButton, Modal, Paper, Snackbar, SnackbarContent, Typography, } from '@material-ui/core' import { Theme, withStyles } from '@material-ui/core/styles' import { UpdateInfo, checkForUpdates, rendererEvents, updateAvailable } from '../../events' import { green, red } from '@material-ui/core/colors' import { AppState } from './reducers' import Close from '@material-ui/icons/Close' import CloudDownload from '@material-ui/icons/CloudDownload' import { UpdateFileInfo } from 'builder-util-runtime' import { bindActionCreators } from 'redux' import { connect } from 'react-redux' import { updateNotifierActions } from './actions' interface Props { showUpdateNotification: boolean showUpdateDetails: boolean classes: any actions: any } class UpdateNotifier extends React.Component { private updateInfo?: UpdateInfo constructor(props: any) { super(props) this.state = { selectedNode: undefined, } } public componentDidMount() { rendererEvents.emit(checkForUpdates, undefined) rendererEvents.subscribe(updateAvailable, this.handleUpdate) } public componentWillUnmount() { rendererEvents.unsubscribeAll(updateAvailable) } private fixUrl(url: string, version: string) { if (!/^http/.test(url)) { return `https://github.com/thomasnordquist/MQTT-Explorer/releases/download/v${version}/${url}` } return url } private handleUpdate = (updateInfo: UpdateInfo) => { this.updateInfo = updateInfo this.props.actions.showUpdateNotification(true) } private onCloseNotification = (event: React.SyntheticEvent, reason: string) => { if (reason === 'clickaway') { return } this.props.actions.showUpdateNotification(false) } private closeNotification = () => { this.props.actions.showUpdateNotification(false) } private showDetails = () => { this.props.actions.showUpdateNotification(false) this.props.actions.showUpdateDetails(true) } private hideDetails = () => { this.props.actions.showUpdateDetails(false) } public render() { return (
{this.renderUpdateNotification()} {this.renderUpdateDetails()}
) } private renderUpdateNotification() { const snackbarAnchor: any = { vertical: 'top', horizontal: 'right', } return ( ) } private notificationActions() { return [( ), ( ), ] } private renderUpdateDetails() { if (!this.updateInfo) { return null } const releaseNotes = (this.updateInfo.releaseNotes as string) || '' return ( Version {this.updateInfo.version} Changelog
{this.renderDownloads(this.updateInfo)} ) } private urlToFilename(url: string) { const parts = url.split('/') return parts[parts.length - 1] } private renderDownloads(updateInfo: UpdateInfo) { return updateInfo.files .map((file: UpdateFileInfo, index: number) => (
)) } } const styles = (theme: Theme) => ({ success: { backgroundColor: green[600], color: theme.typography.button.color, }, close: { padding: theme.spacing.unit / 2, }, root: { minWidth: '350px', maxWidth: '500px', backgroundColor: theme.palette.background.default, margin: '20vh auto auto auto', padding: `${2 * theme.spacing.unit}px`, outline: 'none', }, title: { color: theme.palette.text.primary, }, releaseNotes: { overflow: 'auto scroll', color: theme.palette.text.secondary, backgroundColor: 'rgba(60, 60, 60, 0.6)', maxHeight: '28vh', }, paper: { padding: `${theme.spacing.unit * 2}px`, color: theme.palette.text.secondary, }, download: { width: '100%', }, closeButton: { display: 'block', margin: '0 0 0 auto', }, }) const mapStateToProps = (state: AppState) => { return { showUpdateNotification: state.tooBigReducer.showUpdateNotification, showUpdateDetails: state.tooBigReducer.showUpdateDetails, } } const mapDispatchToProps = (dispatch: any) => { return { actions: bindActionCreators(updateNotifierActions, dispatch), } } export default withStyles(styles)(connect(mapStateToProps, mapDispatchToProps)(UpdateNotifier))