monorepo/visions/ui/src/App.tsx

84 lines
2.7 KiB
TypeScript
Raw Normal View History

import React, { PropsWithChildren, useContext, useEffect, useState } from 'react';
import './App.css';
import { Client } from './client';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import { DesignPage } from './views/Design/Design';
import { GmView } from './views/GmView/GmView';
import { WebsocketProvider } from './components/WebsocketProvider';
2024-11-19 19:53:42 +00:00
import { PlayerView } from './views/PlayerView/PlayerView';
import { Admin } from './views/Admin/Admin';
2024-11-25 13:28:22 +00:00
import Candela from './plugins/Candela';
2024-12-16 03:49:53 +00:00
import { Authentication } from './views/Authentication/Authentication';
import { StateContext, StateProvider } from './providers/StateProvider/StateProvider';
const TEST_CHARSHEET_UUID = "12df9c09-1f2f-4147-8eda-a97bd2a7a803";
interface AppProps {
client: Client;
}
const CandelaCharsheet = ({ client }: { client: Client }) => {
let [sheet, setSheet] = useState(undefined);
useEffect(
() => { client.charsheet(TEST_CHARSHEET_UUID).then((c) => setSheet(c)); },
[client, setSheet]
);
return sheet ? <Candela.CharsheetElement sheet={sheet} /> : <div> </div>
}
2024-12-17 04:46:02 +00:00
interface AuthedViewProps {
client: Client;
}
const AuthedView = ({ client, children }: PropsWithChildren<AuthedViewProps>) => {
const [state, manager] = useContext(StateContext);
return (
<Authentication onAdminPassword={(password) => {
manager.setAdminPassword(password);
}} onAuth={(username, password) => console.log(username, password)}>
{children}
</Authentication>
);
2024-12-17 04:46:02 +00:00
}
const App = ({ client }: AppProps) => {
console.log("rendering app");
const [websocketUrl, setWebsocketUrl] = useState<string | undefined>(undefined);
useEffect(() => {
client.registerWebsocket().then((url) => setWebsocketUrl(url))
}, [client]);
let router =
createBrowserRouter([
2024-12-16 03:49:53 +00:00
{
path: "/",
element: <StateProvider client={client}><AuthedView client={client}> <PlayerView client={client} /> </AuthedView> </StateProvider>
2024-12-16 03:49:53 +00:00
},
{
path: "/gm",
element: websocketUrl ? <WebsocketProvider websocketUrl={websocketUrl}> <GmView client={client} /> </WebsocketProvider> : <div> </div>
},
{
path: "/admin",
element: <Admin client={client} />
},
2024-11-25 13:28:22 +00:00
{
path: "/candela",
element: <CandelaCharsheet client={client} />
},
{
path: "/design",
element: <DesignPage />
}
]);
return (
<div className="App">
<RouterProvider router={router} />
</div>
);
}
export default App;