element-call/src/App.jsx

100 lines
2.6 KiB
React
Raw Normal View History

2021-07-16 21:38:44 +00:00
/*
Copyright 2021 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2021-07-27 19:27:59 +00:00
import React from "react";
2021-07-19 19:55:30 +00:00
import {
BrowserRouter as Router,
Switch,
Route,
Redirect,
2021-08-10 01:38:19 +00:00
useLocation,
2021-07-19 19:55:30 +00:00
} from "react-router-dom";
2021-07-27 19:27:59 +00:00
import { useConferenceCallManager } from "./ConferenceCallManagerHooks";
2021-08-20 00:49:45 +00:00
import { Home } from "./Home";
2021-09-03 22:45:07 +00:00
import { Room, RoomAuth } from "./Room";
2021-08-11 23:02:40 +00:00
import { GridDemo } from "./GridDemo";
2021-08-20 23:23:12 +00:00
import { RegisterPage } from "./RegisterPage";
import { LoginPage } from "./LoginPage";
2021-08-23 19:56:41 +00:00
import { Center } from "./Layout";
2021-07-16 21:22:03 +00:00
export default function App() {
const { protocol, host } = window.location;
// Assume homeserver is hosted on same domain (proxied in development by vite)
const homeserverUrl = `${protocol}//${host}`;
2021-09-03 22:45:07 +00:00
const {
loading,
authenticated,
error,
manager,
login,
loginAsGuest,
register,
} = useConferenceCallManager(homeserverUrl);
2021-07-16 21:22:03 +00:00
return (
2021-07-19 19:55:30 +00:00
<Router>
2021-08-20 00:49:45 +00:00
<>
2021-07-16 21:22:03 +00:00
{loading ? (
2021-08-23 19:56:41 +00:00
<Center>
<p>Loading...</p>
</Center>
2021-07-16 21:22:03 +00:00
) : (
2021-07-19 19:55:30 +00:00
<Switch>
2021-08-20 23:23:12 +00:00
<AuthenticatedRoute authenticated={authenticated} exact path="/">
<Home manager={manager} error={error} />
</AuthenticatedRoute>
<Route exact path="/login">
<LoginPage onLogin={login} error={error} />
</Route>
<Route exact path="/register">
<RegisterPage onRegister={register} error={error} />
2021-07-19 19:55:30 +00:00
</Route>
2021-09-03 22:45:07 +00:00
<Route path="/room/:roomId">
{authenticated ? (
<Room manager={manager} error={error} />
) : (
<RoomAuth error={error} onLoginAsGuest={loginAsGuest} />
)}
</Route>
2021-08-11 23:02:40 +00:00
<Route exact path="/grid">
<GridDemo />
</Route>
2021-07-19 19:55:30 +00:00
</Switch>
2021-07-16 21:22:03 +00:00
)}
2021-08-20 00:49:45 +00:00
</>
2021-07-19 19:55:30 +00:00
</Router>
2021-07-16 21:22:03 +00:00
);
}
2021-08-10 01:38:19 +00:00
function AuthenticatedRoute({ authenticated, children, ...rest }) {
const location = useLocation();
return (
<Route {...rest}>
{authenticated ? (
children
) : (
<Redirect
to={{
2021-08-20 23:23:12 +00:00
pathname: "/login",
2021-08-10 01:38:19 +00:00
state: { from: location },
}}
/>
)}
</Route>
);
}