2022-01-25 17:10:35 +02:00
|
|
|
import Banner from "./Banner";
|
2021-11-08 00:25:27 +02:00
|
|
|
import Connection from "./Connection";
|
|
|
|
import KeyboardController from "./Keyboard";
|
|
|
|
import { getAuth, setAuth } from "./LocalConfiguration";
|
|
|
|
import LoginPrompt from "./LoginPrompt";
|
|
|
|
import TouchpadController from "./Touchpad";
|
|
|
|
|
|
|
|
class App {
|
|
|
|
constructor(mountElement) {
|
|
|
|
this.mountElement = mountElement;
|
|
|
|
|
|
|
|
this.connection = new Connection(`ws://${location.host}/gateway`);
|
|
|
|
this.touchpad = new TouchpadController(this.connection);
|
|
|
|
this.keyboard = new KeyboardController(this.connection);
|
|
|
|
this.loginPromptComponent = null;
|
|
|
|
|
|
|
|
this.connection.connect(getAuth());
|
|
|
|
this.connection.ws.addEventListener("close", ({code}) => {
|
|
|
|
this.unmountApp();
|
|
|
|
if (code === 4001) { // 4001 - code for bad auth
|
|
|
|
this.transitionTo("login");
|
2022-01-25 17:10:35 +02:00
|
|
|
} else {
|
|
|
|
this.transitionTo("reconnectingBanner");
|
2021-11-08 00:25:27 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this.connection.onHandshakeCompleted = () => {
|
|
|
|
this.transitionTo("app");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
transitionTo(type) {
|
|
|
|
switch (type) {
|
|
|
|
case "login":
|
2022-01-25 17:10:35 +02:00
|
|
|
this.unmountBannerComponent();
|
2021-11-08 00:25:27 +02:00
|
|
|
this.unmountApp();
|
|
|
|
this.mountLoginComponent();
|
|
|
|
break;
|
|
|
|
case "app":
|
2022-01-25 17:10:35 +02:00
|
|
|
this.unmountBannerComponent();
|
2021-11-08 00:25:27 +02:00
|
|
|
this.unmountLoginComponent();
|
|
|
|
this.mountApp();
|
|
|
|
break;
|
2022-01-25 17:10:35 +02:00
|
|
|
case "reconnectingBanner":
|
|
|
|
this.unmountApp();
|
|
|
|
this.unmountLoginComponent();
|
|
|
|
this.mountBannerComponent("Connecting...", "Looks like you've lost connection. We're trying to reconnect you.");
|
|
|
|
break;
|
2021-11-08 00:25:27 +02:00
|
|
|
default:
|
|
|
|
throw new Error(`transitionTo type ${type} is invalid`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 17:10:35 +02:00
|
|
|
mountBannerComponent(title, text) {
|
|
|
|
if (!this.bannerComponent)
|
|
|
|
this.bannerComponent = new Banner();
|
|
|
|
|
|
|
|
this.bannerComponent.mountOn(this.mountElement);
|
|
|
|
this.bannerComponent.updateTitle(title);
|
|
|
|
this.bannerComponent.updateText(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
unmountBannerComponent() {
|
|
|
|
if (this.bannerComponent)
|
|
|
|
this.bannerComponent.unmount();
|
|
|
|
}
|
|
|
|
|
2021-11-08 00:25:27 +02:00
|
|
|
mountLoginComponent() {
|
|
|
|
if (!this.loginPromptComponent)
|
|
|
|
this.loginPromptComponent = new LoginPrompt(this.connection);
|
|
|
|
|
|
|
|
this.loginPromptComponent.mountOn(this.mountElement);
|
2022-01-25 17:10:35 +02:00
|
|
|
this.loginPromptComponent.onPasswordSubmitted = (p) => {
|
2021-11-08 00:25:27 +02:00
|
|
|
setAuth(p);
|
|
|
|
this.connection.connect(p);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
unmountLoginComponent() {
|
|
|
|
if (this.loginPromptComponent)
|
|
|
|
this.loginPromptComponent.unmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
mountApp() {
|
|
|
|
this.touchpad.mountOn(this.mountElement);
|
|
|
|
this.keyboard.mountOn(this.mountElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
unmountApp() {
|
|
|
|
this.touchpad.unmount();
|
|
|
|
this.keyboard.unmount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default App;
|