2021-02-10 22:21:43 +01:00
|
|
|
import { h, Fragment } from 'preact';
|
|
|
|
import BaseAppBar from './components/AppBar';
|
|
|
|
import LinkedLogo from './components/LinkedLogo';
|
|
|
|
import Menu, { MenuItem, MenuSeparator } from './components/Menu';
|
|
|
|
import AutoAwesomeIcon from './icons/AutoAwesome';
|
|
|
|
import LightModeIcon from './icons/LightMode';
|
|
|
|
import DarkModeIcon from './icons/DarkMode';
|
2021-06-21 17:11:09 +02:00
|
|
|
import FrigateRestartIcon from './icons/FrigateRestart';
|
2022-04-10 15:09:41 +02:00
|
|
|
import Prompt from './components/Prompt';
|
2021-02-10 22:21:43 +01:00
|
|
|
import { useDarkMode } from './context';
|
|
|
|
import { useCallback, useRef, useState } from 'preact/hooks';
|
2021-06-21 17:11:09 +02:00
|
|
|
import { useRestart } from './api/mqtt';
|
2021-02-10 22:21:43 +01:00
|
|
|
|
|
|
|
export default function AppBar() {
|
|
|
|
const [showMoreMenu, setShowMoreMenu] = useState(false);
|
2021-06-21 17:11:09 +02:00
|
|
|
const [showDialog, setShowDialog] = useState(false);
|
|
|
|
const [showDialogWait, setShowDialogWait] = useState(false);
|
2021-02-10 22:21:43 +01:00
|
|
|
const { setDarkMode } = useDarkMode();
|
2021-06-21 17:11:09 +02:00
|
|
|
const { send: sendRestart } = useRestart();
|
2021-02-10 22:21:43 +01:00
|
|
|
|
|
|
|
const handleSelectDarkMode = useCallback(
|
2022-02-26 20:11:00 +01:00
|
|
|
(value) => {
|
2021-02-10 22:21:43 +01:00
|
|
|
setDarkMode(value);
|
|
|
|
setShowMoreMenu(false);
|
|
|
|
},
|
|
|
|
[setDarkMode, setShowMoreMenu]
|
|
|
|
);
|
|
|
|
|
|
|
|
const moreRef = useRef(null);
|
|
|
|
|
|
|
|
const handleShowMenu = useCallback(() => {
|
|
|
|
setShowMoreMenu(true);
|
|
|
|
}, [setShowMoreMenu]);
|
|
|
|
|
|
|
|
const handleDismissMoreMenu = useCallback(() => {
|
|
|
|
setShowMoreMenu(false);
|
|
|
|
}, [setShowMoreMenu]);
|
|
|
|
|
2021-06-21 17:11:09 +02:00
|
|
|
const handleClickRestartDialog = useCallback(() => {
|
|
|
|
setShowDialog(false);
|
|
|
|
setShowDialogWait(true);
|
|
|
|
sendRestart();
|
2021-06-24 15:08:13 +02:00
|
|
|
}, [setShowDialog]); // eslint-disable-line react-hooks/exhaustive-deps
|
2021-06-21 17:11:09 +02:00
|
|
|
|
2021-07-06 14:34:39 +02:00
|
|
|
const handleDismissRestartDialog = useCallback(() => {
|
2021-06-21 17:11:09 +02:00
|
|
|
setShowDialog(false);
|
2021-07-06 14:34:39 +02:00
|
|
|
}, [setShowDialog]);
|
2021-06-21 17:11:09 +02:00
|
|
|
|
|
|
|
const handleRestart = useCallback(() => {
|
|
|
|
setShowMoreMenu(false);
|
|
|
|
setShowDialog(true);
|
2021-06-23 14:38:38 +02:00
|
|
|
}, [setShowDialog]);
|
2021-06-21 17:11:09 +02:00
|
|
|
|
2021-02-10 22:21:43 +01:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<BaseAppBar title={LinkedLogo} overflowRef={moreRef} onOverflowClick={handleShowMenu} />
|
|
|
|
{showMoreMenu ? (
|
|
|
|
<Menu onDismiss={handleDismissMoreMenu} relativeTo={moreRef}>
|
|
|
|
<MenuItem icon={AutoAwesomeIcon} label="Auto dark mode" value="media" onSelect={handleSelectDarkMode} />
|
|
|
|
<MenuSeparator />
|
|
|
|
<MenuItem icon={LightModeIcon} label="Light" value="light" onSelect={handleSelectDarkMode} />
|
|
|
|
<MenuItem icon={DarkModeIcon} label="Dark" value="dark" onSelect={handleSelectDarkMode} />
|
2021-06-21 17:11:09 +02:00
|
|
|
<MenuSeparator />
|
|
|
|
<MenuItem icon={FrigateRestartIcon} label="Restart Frigate" onSelect={handleRestart} />
|
2021-02-10 22:21:43 +01:00
|
|
|
</Menu>
|
2021-08-19 18:24:23 +02:00
|
|
|
) : null}
|
2021-06-21 17:11:09 +02:00
|
|
|
{showDialog ? (
|
2022-04-10 15:09:41 +02:00
|
|
|
<Prompt
|
2021-06-21 17:11:09 +02:00
|
|
|
onDismiss={handleDismissRestartDialog}
|
|
|
|
title="Restart Frigate"
|
2021-06-24 14:06:27 +02:00
|
|
|
text="Are you sure?"
|
2021-06-21 17:11:09 +02:00
|
|
|
actions={[
|
2021-06-23 15:24:57 +02:00
|
|
|
{ text: 'Yes', color: 'red', onClick: handleClickRestartDialog },
|
2021-06-23 14:38:38 +02:00
|
|
|
{ text: 'Cancel', onClick: handleDismissRestartDialog },
|
2021-06-21 17:11:09 +02:00
|
|
|
]}
|
|
|
|
/>
|
2021-08-19 18:24:23 +02:00
|
|
|
) : null}
|
2021-06-21 17:11:09 +02:00
|
|
|
{showDialogWait ? (
|
2022-04-10 15:09:41 +02:00
|
|
|
<Prompt
|
2021-06-21 17:11:09 +02:00
|
|
|
title="Restart in progress"
|
|
|
|
text="Please wait a few seconds for the restart to complete before reloading the page."
|
|
|
|
/>
|
2021-02-10 22:21:43 +01:00
|
|
|
) : null}
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|