VirtWeb/virtweb_frontend/src/App.tsx

64 lines
2.0 KiB
TypeScript
Raw Normal View History

2023-09-04 12:11:56 +00:00
import React from "react";
import "./App.css";
import {
Route,
RouterProvider,
createBrowserRouter,
createRoutesFromElements,
} from "react-router-dom";
import { NotFoundRoute } from "./routes/NotFound";
import { OIDCCbRoute } from "./routes/auth/OIDCCbRoute";
import { BaseLoginPage } from "./widgets/BaseLoginPage";
import { BaseAuthenticatedPage } from "./widgets/BaseAuthenticatedPage";
import { LoginRoute } from "./routes/auth/LoginRoute";
import { AuthApi } from "./api/AuthApi";
2023-09-05 11:19:25 +00:00
import { IsoFilesRoute } from "./routes/IsoFilesRoute";
2023-09-08 07:04:48 +00:00
import { ServerApi } from "./api/ServerApi";
2023-09-08 07:45:41 +00:00
import { SysInfoRoute } from "./routes/SysInfoRoute";
2023-09-08 14:33:45 +00:00
import { VirtualMachinesRoute } from "./routes/VirtualMachinesRoute";
2023-09-04 12:11:56 +00:00
interface AuthContext {
signedIn: boolean;
setSignedIn: (signedIn: boolean) => void;
}
const AuthContextK = React.createContext<AuthContext | null>(null);
export function App() {
const [signedIn, setSignedIn] = React.useState(AuthApi.SignedIn);
const context: AuthContext = {
signedIn: signedIn,
setSignedIn: (s) => setSignedIn(s),
};
const router = createBrowserRouter(
createRoutesFromElements(
2023-09-08 07:04:48 +00:00
signedIn || ServerApi.Config.auth_disabled ? (
2023-09-04 12:11:56 +00:00
<Route path="*" element={<BaseAuthenticatedPage />}>
2023-09-05 11:19:25 +00:00
<Route path="iso" element={<IsoFilesRoute />} />
2023-09-08 14:33:45 +00:00
<Route path="vms" element={<VirtualMachinesRoute />} />
2023-09-08 07:45:41 +00:00
<Route path="sysinfo" element={<SysInfoRoute />} />
2023-09-04 12:11:56 +00:00
<Route path="*" element={<NotFoundRoute />} />
</Route>
) : (
<Route path="*" element={<BaseLoginPage />}>
<Route path="" element={<LoginRoute />} />
<Route path="oidc_cb" element={<OIDCCbRoute />} />
<Route path="*" element={<NotFoundRoute />} />
</Route>
)
)
);
return (
<AuthContextK.Provider value={context}>
<RouterProvider router={router} />
</AuthContextK.Provider>
);
}
export function useAuth(): AuthContext {
return React.useContext(AuthContextK)!;
}