added supervisor token + snackbar for errors

This commit is contained in:
ari committed 2022-05-31 12:19:58 -04:00
1 parent e22f64dd51
commit 21fe64f4bf
9 files changed
+88 -21

No files matched your search

@@ -12,7 +12,6 @@ export const ActionDeviceState: OptionManager<DeviceAction> = {
}),
isReady: () => true,
renderOptionList: (state, setState, entities) => {
console.log(entities.collection?.state)
const update = updateActionData(state, setState);
return <>
<InputDevice
+59 -10
View File
@@ -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>
}
+11 -1
View File
@@ -14,6 +14,7 @@ import {
const HASS_URL = process.env.REACT_APP_HASS_URL as string;
const HASS_TOKEN = process.env.REACT_APP_HASS_TOKEN;
const HASS_SUPERVISOR_TOKEN = process.env.REACT_APP_SUPERVISOR_TOKEN as string;
export type HAConnection = {
status: 'loading'
} | {
@@ -51,6 +52,9 @@ const createAuth = async () => {
if (HASS_TOKEN) {
return createLongLivedTokenAuth(HASS_URL, HASS_TOKEN);
}
if (HASS_SUPERVISOR_TOKEN) {
return await getAuth({ hassUrl: HASS_URL, authCode: HASS_SUPERVISOR_TOKEN });
}
try {
auth = await getAuth();
return auth
@@ -79,7 +83,13 @@ async function startHAConnection() {
} catch (error) {
haConnection = {
status: 'error',
error: translateErrorCodes(error as any),
error: {
originalError: JSON.stringify(error),
message: translateErrorCodes(error as any),
hassToken: !HASS_TOKEN ? "no" : "yes",
hasSuperToken: HASS_SUPERVISOR_TOKEN,
url: HASS_URL,
},
}
throw error
}