import * as React from 'react' import { connect } from 'react-redux' import { CSSTransition } from 'react-transition-group' 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 { ServerStatus } from 'common/mojang/net/ServerStatusAPI' import { MojangStatus, MojangStatusColor } from 'common/mojang/rest/internal/MojangStatus' import { MojangRestAPI } from 'common/mojang/rest/MojangRestAPI' import { LoggerUtil } from 'common/logging/loggerutil' import News from '../news/News' import './Landing.css' interface LandingProps { distribution: HeliosDistribution selectedServer?: HeliosServer selectedServerStatus?: ServerStatus mojangStatuses: MojangStatus[] } interface LandingState { workingServerStatus?: ServerStatus } const mapState = (state: StoreType): Partial => { return { distribution: state.app.distribution!, selectedServer: state.app.selectedServer, selectedServerStatus: state.app.selectedServerStatus, mojangStatuses: state.app.mojangStatuses } } const mapDispatch = { ...AppActionDispatch, ...OverlayActionDispatch } type InternalLandingProps = LandingProps & typeof mapDispatch class Landing extends React.Component { private static readonly logger = LoggerUtil.getLogger('Landing') constructor(props: InternalLandingProps) { super(props) this.state = { workingServerStatus: props.selectedServerStatus } } /* Mojang Status Methods */ private getMainMojangStatusColor = (): string => { const essential = this.props.mojangStatuses.filter(s => s.essential) if(this.props.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.props.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.props.mojangStatuses.filter(s => s.status === MojangStatusColor.GREY).length === this.props.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.props.mojangStatuses.filter(s => s.essential === essential)) { statuses.push(
{status.name}
) } return statuses } /* Selected Server Methods */ private updateWorkingServerStatus = (): void => { this.setState({ ...this.state, workingServerStatus: this.props.selectedServerStatus }) } private openServerSelect = (): void => { this.props.pushServerSelectOverlay({ servers: this.props.distribution.servers, selectedId: this.props.selectedServer?.rawServer.id, onSelection: async (serverId: string) => { Landing.logger.info('Server Selection Change:', serverId) const next: HeliosServer = this.props.distribution.getServerById(serverId)! this.props.setSelectedServer(next) } }) } private getSelectedServerText = (): string => { if(this.props.selectedServer != null) { return `• ${this.props.selectedServer.rawServer.id}` } else { return '• No Server Selected' } } private getSelectedServerStatusText = (): string => { return this.state.workingServerStatus != null ? 'PLAYERS' : 'SERVER' } private getSelectedServerCount = (): string => { if(this.state.workingServerStatus != null) { const { online, max } = this.state.workingServerStatus.players return `${online}/${max}` } else { return 'OFFLINE' } } /* Render */ render(): JSX.Element { return <>
Update Available
{this.getSelectedServerStatusText()} {this.getSelectedServerCount()}
MOJANG STATUS
Services
{this.getMojangStatusesAsJSX(true)}
Non Essential
{this.getMojangStatusesAsJSX(false)}
} } export default connect(mapState, mapDispatch)(Landing)