/* connection.tsx - ESP3D WebUI area file Copyright (c) 2020 Luc Lebosse. All rights reserved. This code is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with This code; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ import { FunctionalComponent, TargetedMouseEvent } from "preact" import { useRef } from "preact/hooks" import { useUiContext, useSettingsContext, useUiContextFn } from "../contexts" import { Loading } from "../components/Controls" import { AppLogo } from "../targets" import { Minus, HardDrive, Frown, AlertTriangle, Slash, } from "preact-feather" import { T } from "../components/Translations" import { espHttpURL } from "../components/Helpers" import { restartdelay } from "../targets" /* * Local const * */ const ConnectionContainer: FunctionalComponent = () => { const { connection } = useUiContext() const { connectionSettings } = useSettingsContext() const timerCtrl = useRef(null) let contentIcon: any let contentSubtitle: any let contentTitle: string | undefined let contentAction: any let intervalTimer = 0 if ( !connection.connectionState.connected || connection.connectionState.updating ) { const refreshTimer = () => { if (intervalTimer > 0) { intervalTimer-- if (timerCtrl.current) timerCtrl.current.innerHTML = intervalTimer.toString() setTimeout(refreshTimer, 1000) } } const onclick = (e: TargetedMouseEvent): void => { useUiContextFn.haptic() connection.setConnectionState({ connected: false, page: "connecting", }) window.location.href = espHttpURL().toString() } console.log("Rendering connection state:", connection.connectionState.page) switch (connection.connectionState.page) { //No connection case "error": useUiContextFn.beepError() contentTitle = T("S1") //"Connection error" contentIcon = contentSubtitle = T("S5") //"Cannot connect with board" if (connection.connectionState.extraMsg) contentSubtitle += `: ${ connection.connectionState.extraMsg}` document.title = `${connectionSettings.current && connectionSettings.current.HostName ? connectionSettings.current.HostName : "ESP3D" }(${ T("S22") })` contentAction = ( ) break //Session timeout case "sessiontimeout": //Error connection lost case "connectionlost": useUiContextFn.beepError() contentTitle = T("S1") //"Connection error" contentIcon = contentSubtitle = connection.connectionState.page == "connectionlost" ? T("S10") : T("S173") //"Connection with board is lost" document.title = `${connectionSettings.current && connectionSettings.current.HostName ? connectionSettings.current.HostName : "ESP3D" }(${ T("S9") })` contentAction = ( ) break //Disconnected case "already connected": useUiContextFn.beep() contentTitle = T("S9") contentIcon = contentSubtitle = T("S3") document.title = `${connectionSettings.current && connectionSettings.current.HostName ? connectionSettings.current.HostName : "ESP3D" }(${ T("S9") })` contentAction = ( ) break //restart case "restart": intervalTimer = restartdelay setTimeout(refreshTimer, 1000) document.title = `${connectionSettings.current && connectionSettings.current.HostName ? connectionSettings.current.HostName : "ESP3D" }(${ T("S35") })` contentTitle = T("S35") //"restarting"; contentIcon = (
) contentSubtitle = ( {T("S60")} {restartdelay} {T("S119")} ) contentAction = "" break default: //"Please wait..." if (connection.connectionState.updating) { contentTitle = T("S35") //"restarting"; connection.setConnectionState({ connected: connection.connectionState.connected, page: connection.connectionState.page, updating: false, }) } else { document.title = `${connectionSettings.current && connectionSettings.current.HostName ? connectionSettings.current.HostName : "ESP3D" }(${ T("S2") })` contentTitle = T("S2") //"Connecting"; } contentIcon = (
) contentSubtitle = T("S60") contentAction = "" } return (
{contentIcon}
{contentTitle}
{contentSubtitle}
{contentAction}
) } return null; } export { ConnectionContainer }