From f2eebf2a02ff542ae95946c3f2b375e644d2442c Mon Sep 17 00:00:00 2001 From: ElMoribond Date: Mon, 12 Jul 2021 14:40:52 +0200 Subject: [PATCH] improved user experience --- web/src/components/DialogRestart.jsx | 63 ++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 web/src/components/DialogRestart.jsx diff --git a/web/src/components/DialogRestart.jsx b/web/src/components/DialogRestart.jsx new file mode 100644 index 000000000..53f01a5f7 --- /dev/null +++ b/web/src/components/DialogRestart.jsx @@ -0,0 +1,63 @@ +import { h, Fragment } from 'preact'; +import { useCallback, useEffect, useState } from 'preact/hooks'; +import Dialog from './Dialog'; +import { useApiHost } from '../api'; +import { useRestart } from '../api/mqtt'; + +export default function DialogRestart({ showDialog, setShowDialog }) { + const apiHost = useApiHost(); + const { payload: detectRestarted = null, send: sendRestart } = useRestart(); + const [dialogTitle, setDialogTitle] = useState("Restart in progress"); + const [showDialogWait, setShowDialogWait] = useState(false); + + useEffect(() => { + if (detectRestarted != null && Number.isInteger(detectRestarted)) { + if (!detectRestarted) + setDialogTitle("Server-initiated startup"); + setShowDialog(false); + setShowDialogWait(true); + } + }, [detectRestarted]); + + const waitPlease = async () => { + const delay = ms => new Promise(res => setTimeout(res, ms)); + await delay(3456); + while (true) { + try { + const response = await fetch(`${apiHost}/api/config`, { method: 'GET' }); + if (await response.status == 200) + window.location.reload(); + } + catch (e) {} + await delay(987); + } + }; + + const handleClick = useCallback(() => { + sendRestart(); + setShowDialog(false); + waitPlease(); + }); + + const handleDismiss = useCallback(() => { + setShowDialog(false); + setShowDialogWait(false); + }); + + return ( + + {showDialog ? ( + + ) : detectRestarted != null && ( + + )} + + ); +}