From feb9b98b2aef5f9b61f917a3bbab393564550c61 Mon Sep 17 00:00:00 2001 From: Daniel Scalzi Date: Mon, 24 Aug 2020 21:26:24 -0400 Subject: [PATCH] Add mojang statuses to landing component. --- src/renderer/components/landing/Landing.tsx | 114 +++++++++++++++++++- 1 file changed, 110 insertions(+), 4 deletions(-) diff --git a/src/renderer/components/landing/Landing.tsx b/src/renderer/components/landing/Landing.tsx index 304b560b..37f24ff1 100644 --- a/src/renderer/components/landing/Landing.tsx +++ b/src/renderer/components/landing/Landing.tsx @@ -1,8 +1,114 @@ import * as React from 'react' +import { Status, StatusColor } from 'common/mojang/model/internal/Status' +import { MojangResponse } from 'common/mojang/model/internal/MojangResponse' +import { Mojang } from 'common/mojang/mojang' +import { RestResponseStatus } from 'common/got/RestResponse' +import { LoggerUtil } from 'common/logging/loggerutil' + import './Landing.css' -export default class Landing extends React.Component { +interface LandingState { + mojangStatuses: Status[] +} + +export default class Landing extends React.Component { + + private static readonly logger = LoggerUtil.getLogger('Landing') + + private mojangStatusInterval!: NodeJS.Timeout + + constructor(props: unknown) { + 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 Mojang.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 = StatusColor.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 Mojang.statusToHex(StatusColor.GREY) + } + + // If any essential are red, it's red. + if(essential.filter(s => s.status === StatusColor.RED).length > 0) { + return Mojang.statusToHex(StatusColor.RED) + } + // If any essential are yellow, it's yellow. + if(essential.filter(s => s.status === StatusColor.YELLOW).length > 0) { + return Mojang.statusToHex(StatusColor.YELLOW) + } + // If any non-essential are not green, return yellow. + if(this.state.mojangStatuses.filter(s => s.status !== StatusColor.GREEN && s.status !== StatusColor.GREY).length > 0) { + return Mojang.statusToHex(StatusColor.YELLOW) + } + // if all are grey, return grey. + if(this.state.mojangStatuses.filter(s => s.status === StatusColor.GREY).length === this.state.mojangStatuses.length) { + return Mojang.statusToHex(StatusColor.GREY) + } + + return Mojang.statusToHex(StatusColor.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 + } render(): JSX.Element { return <> @@ -111,11 +217,11 @@ export default class Landing extends React.Component {
MOJANG STATUS - +
Services
- {/* Essential Mojang services are populated here. */} + {this.getMojangStatusesAsJSX(true)}
@@ -123,7 +229,7 @@ export default class Landing extends React.Component {
- {/* Non Essential Mojang services are populated here. */} + {this.getMojangStatusesAsJSX(false)}