added supervisor token + snackbar for errors
This commit is contained in:
1 parent
e22f64dd51
commit
21fe64f4bf
9 files changed
+88
-21
No files matched your search
@@ -1,8 +1,10 @@
|
||||
import "./index.css";
|
||||
import { FC, ReactNode } from 'react';
|
||||
import { createTheme, ThemeProvider } from '@mui/material/styles';
|
||||
import { createRef, FC, ReactNode, useEffect, useRef } from 'react';
|
||||
import { createTheme, ThemeProvider, useTheme } from '@mui/material/styles';
|
||||
import { VersionBox } from "components/VersionBox";
|
||||
import { SnackbarProvider } from 'notistack';
|
||||
import { SnackbarKey, SnackbarProvider, useSnackbar } from 'notistack';
|
||||
import { Button } from "@mui/material";
|
||||
import { useHAConnection } from 'haService/connection';
|
||||
|
||||
const theme = createTheme({
|
||||
palette: {
|
||||
@@ -10,19 +12,66 @@ const theme = createTheme({
|
||||
},
|
||||
});
|
||||
|
||||
export const InternalPage: FC<{
|
||||
children: ReactNode
|
||||
}> = ({ children }) => {
|
||||
const conn = useHAConnection();
|
||||
const theme = useTheme();
|
||||
const snackbar = useSnackbar();
|
||||
const snackbarKeyHAConKey = useRef<SnackbarKey | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (conn.status === 'error') {
|
||||
if (snackbarKeyHAConKey.current) {
|
||||
snackbar.closeSnackbar(snackbarKeyHAConKey.current)
|
||||
}
|
||||
snackbarKeyHAConKey.current = snackbar.enqueueSnackbar(`Failed to connect to homeassistant due to "${JSON.stringify(conn.error)}"`, {
|
||||
"variant": "error",
|
||||
"persist": true,
|
||||
})
|
||||
} else if (conn.status === 'loading') {
|
||||
if (snackbarKeyHAConKey.current) {
|
||||
snackbar.closeSnackbar(snackbarKeyHAConKey.current)
|
||||
}
|
||||
snackbarKeyHAConKey.current = snackbar.enqueueSnackbar(`Connecting to websocket...`, {
|
||||
"variant": "info",
|
||||
})
|
||||
} else if (conn.status === "loaded") {
|
||||
if (snackbarKeyHAConKey.current) {
|
||||
snackbar.closeSnackbar(snackbarKeyHAConKey.current)
|
||||
}
|
||||
}
|
||||
}, [conn.status])
|
||||
|
||||
return <main className="page" style={{
|
||||
color: theme.palette.text.primary,
|
||||
background: theme.palette.background.default,
|
||||
}}>
|
||||
<VersionBox />
|
||||
{children}
|
||||
</main>
|
||||
}
|
||||
|
||||
export const Page: FC<{
|
||||
children: ReactNode
|
||||
}> = ({ children }) => {
|
||||
const notistackRef = createRef<any>();
|
||||
const onClickDismiss = (key: SnackbarKey) => () => {
|
||||
notistackRef.current.closeSnackbar(key);
|
||||
}
|
||||
return <ThemeProvider theme={theme}>
|
||||
<SnackbarProvider>
|
||||
<main className="page" style={{
|
||||
color: theme.palette.text.primary,
|
||||
background: theme.palette.background.default,
|
||||
}}>
|
||||
<VersionBox />
|
||||
<SnackbarProvider
|
||||
anchorOrigin={{
|
||||
"vertical": "bottom",
|
||||
"horizontal": "center"
|
||||
}}
|
||||
ref={notistackRef}
|
||||
action={key => <Button onClick={onClickDismiss(key)}>Dismiss</Button>}
|
||||
dense preventDuplicate
|
||||
>
|
||||
<InternalPage>
|
||||
{children}
|
||||
</main>
|
||||
</InternalPage>
|
||||
</SnackbarProvider>
|
||||
</ThemeProvider>
|
||||
}
|
||||
Reference in new issue
Block a user