bazarr/frontend/src/App/index.tsx

88 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-03-25 14:22:43 +00:00
import React, {
FunctionComponent,
useCallback,
useEffect,
useState,
} from "react";
2021-04-23 16:18:09 +00:00
import { Row } from "react-bootstrap";
2021-03-25 14:22:43 +00:00
import { Redirect } from "react-router-dom";
import { bootstrap as ReduxBootstrap } from "../@redux/actions";
import { useReduxAction, useReduxStore } from "../@redux/hooks/base";
import { useNotification } from "../@redux/hooks/site";
import { LoadingIndicator, ModalProvider } from "../components";
import Sidebar from "../Sidebar";
2021-04-23 16:18:09 +00:00
import LaunchError from "../special-pages/LaunchError";
import UIError from "../special-pages/UIError";
import { useHasUpdateInject } from "../utilites";
2021-03-25 14:22:43 +00:00
import Header from "./Header";
import NotificationContainer from "./notifications";
import Router from "./Router";
// Sidebar Toggle
export const SidebarToggleContext = React.createContext<() => void>(() => {});
interface Props {}
const App: FunctionComponent<Props> = () => {
const bootstrap = useReduxAction(ReduxBootstrap);
const { initialized, auth } = useReduxStore((s) => s.site);
const notify = useNotification("has-update", 10);
// Has any update?
const hasUpdate = useHasUpdateInject();
useEffect(() => {
if (initialized) {
if (hasUpdate) {
notify({
type: "info",
message: "A new version of Bazarr is ready, restart is required",
// TODO: Restart action
});
}
}
}, [initialized, hasUpdate, notify]);
useEffect(() => {
bootstrap();
}, [bootstrap]);
const [sidebar, setSidebar] = useState(false);
const toggleSidebar = useCallback(() => setSidebar(!sidebar), [sidebar]);
if (!auth) {
return <Redirect to="/login"></Redirect>;
}
if (typeof initialized === "boolean" && initialized === false) {
return (
<LoadingIndicator>
<span>Please wait</span>
</LoadingIndicator>
);
} else if (typeof initialized === "string") {
2021-04-23 16:18:09 +00:00
return <LaunchError>{initialized}</LaunchError>;
2021-03-25 14:22:43 +00:00
}
2021-03-28 02:15:21 +00:00
try {
return (
<SidebarToggleContext.Provider value={toggleSidebar}>
<Row noGutters className="header-container">
<Header></Header>
</Row>
<Row noGutters className="flex-nowrap">
<Sidebar open={sidebar}></Sidebar>
<ModalProvider>
<Router className="d-flex flex-row flex-grow-1 main-router"></Router>
</ModalProvider>
</Row>
<NotificationContainer></NotificationContainer>
</SidebarToggleContext.Provider>
);
2021-04-23 16:18:09 +00:00
} catch (e) {
return <UIError error={e}></UIError>;
2021-03-28 02:15:21 +00:00
}
2021-03-25 14:22:43 +00:00
};
export default App;