import * as React from 'react' import { connect } from 'react-redux' import { StoreType } from '../../redux/store' import { AppActionDispatch } from '../..//redux/actions/appActions' import { OverlayActionDispatch } from '../../redux/actions/overlayActions' import { HeliosDistribution, HeliosServer } from 'common/distribution/DistributionFactory' import { MojangStatus, MojangStatusColor } from 'common/mojang/rest/internal/MojangStatus' import { MojangResponse } from 'common/mojang/rest/internal/MojangResponse' import { MojangRestAPI } from 'common/mojang/rest/MojangRestAPI' import { RestResponseStatus } from 'common/got/RestResponse' import { LoggerUtil } from 'common/logging/loggerutil' import News from '../news/News' import './Landing.css' interface LandingProps { distribution: HeliosDistribution selectedServer: HeliosServer } interface LandingState { mojangStatuses: MojangStatus[] } const mapState = (state: StoreType): Partial => { return { distribution: state.app.distribution!, selectedServer: state.app.selectedServer! } } const mapDispatch = { ...AppActionDispatch, ...OverlayActionDispatch } type InternalLandingProps = LandingProps & typeof mapDispatch class Landing extends React.Component { private static readonly logger = LoggerUtil.getLogger('LandingTSX') private mojangStatusInterval!: NodeJS.Timeout constructor(props: InternalLandingProps) { super(props) this.state = { mojangStatuses: [] } } async componentDidMount(): Promise { // Load Mojang statuses and setup refresh interval. Landing.logger.info('Loading mojang statuses..') await this.loadMojangStatuses() this.mojangStatusInterval = setInterval(async () => { Landing.logger.info('Refreshing Mojang Statuses..') await this.loadMojangStatuses() }, 300000) } componentWillUnmount(): void { // Clean up intervals. clearInterval(this.mojangStatusInterval) } private loadMojangStatuses = async (): Promise => { const response: MojangResponse = await MojangRestAPI.status() if(response.responseStatus !== RestResponseStatus.SUCCESS) { Landing.logger.warn('Failed to retrieve Mojang Statuses.') } // TODO Temp workaround because their status checker always shows // this as red. https://bugs.mojang.com/browse/WEB-2303 const statuses = response.data for(const status of statuses) { if(status.service === 'sessionserver.mojang.com' || status.service === 'minecraft.net') { status.status = MojangStatusColor.GREEN } } this.setState({ ...this.state, mojangStatuses: response.data }) } private getMainMojangStatusColor = (): string => { const essential = this.state.mojangStatuses.filter(s => s.essential) if(this.state.mojangStatuses.length === 0) { return MojangRestAPI.statusToHex(MojangStatusColor.GREY) } // If any essential are red, it's red. if(essential.filter(s => s.status === MojangStatusColor.RED).length > 0) { return MojangRestAPI.statusToHex(MojangStatusColor.RED) } // If any essential are yellow, it's yellow. if(essential.filter(s => s.status === MojangStatusColor.YELLOW).length > 0) { return MojangRestAPI.statusToHex(MojangStatusColor.YELLOW) } // If any non-essential are not green, return yellow. if(this.state.mojangStatuses.filter(s => s.status !== MojangStatusColor.GREEN && s.status !== MojangStatusColor.GREY).length > 0) { return MojangRestAPI.statusToHex(MojangStatusColor.YELLOW) } // if all are grey, return grey. if(this.state.mojangStatuses.filter(s => s.status === MojangStatusColor.GREY).length === this.state.mojangStatuses.length) { return MojangRestAPI.statusToHex(MojangStatusColor.GREY) } return MojangRestAPI.statusToHex(MojangStatusColor.GREEN) } private getMojangStatusesAsJSX = (essential: boolean): JSX.Element[] => { const statuses: JSX.Element[] = [] for(const status of this.state.mojangStatuses.filter(s => s.essential === essential)) { statuses.push(
{status.name}
) } return statuses } private openServerSelect = (): void => { this.props.pushServerSelectOverlay({ servers: this.props.distribution.servers, selectedId: this.props.selectedServer.rawServer.id, onSelection: (serverId: string) => { Landing.logger.info('Server Selection Change:', serverId) this.props.setSelectedServer(this.props.distribution.getServerById(serverId)!) } }) } private getSelectedServerText = (): string => { if(this.props.selectedServer != null) { return `• ${this.props.selectedServer.rawServer.id}` } else { return '• No Server Selected' } } render(): JSX.Element { return <>
Update Available
SERVER OFFLINE
MOJANG STATUS
Services
{this.getMojangStatusesAsJSX(true)}
Non Essential
{this.getMojangStatusesAsJSX(false)}
} } export default connect(mapState, mapDispatch)(Landing)