VirtWeb/virtweb_frontend/src/App.tsx

76 lines
2.5 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-10-17 16:11:31 +00:00
import { VMListRoute } from "./routes/VMListRoute";
2023-10-16 17:00:15 +00:00
import { CreateVMRoute, EditVMRoute } from "./routes/EditVMRoute";
2023-10-17 16:11:31 +00:00
import { VMRoute } from "./routes/VMRoute";
2023-10-18 16:00:25 +00:00
import { VNCRoute } from "./routes/VNCRoute";
2023-10-31 14:55:15 +00:00
import { NetworksListRoute } from "./routes/NetworksListRoute";
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-10-16 17:00:15 +00:00
2023-10-17 16:11:31 +00:00
<Route path="vms" element={<VMListRoute />} />
2023-10-16 17:00:15 +00:00
<Route path="vms/new" element={<CreateVMRoute />} />
2023-10-17 16:11:31 +00:00
<Route path="vm/:uuid" element={<VMRoute />} />
2023-10-16 17:22:15 +00:00
<Route path="vm/:uuid/edit" element={<EditVMRoute />} />
2023-10-18 16:00:25 +00:00
<Route path="vm/:uuid/vnc" element={<VNCRoute />} />
2023-10-16 17:00:15 +00:00
2023-10-31 14:55:15 +00:00
<Route path="net" element={<NetworksListRoute />} />
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)!;
}