element-call/src/App.jsx

97 lines
2.5 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 {
2021-10-06 18:19:42 +00:00
HashRouter as Router,
2021-07-19 19:55:30 +00:00
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-09-10 19:20:17 +00:00
import { useClient } from "./ConferenceCallManagerHooks";
2021-08-20 00:49:45 +00:00
import { Home } from "./Home";
2021-09-10 19:20:17 +00:00
import { Room } from "./Room";
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-09-10 19:20:17 +00:00
import { GuestAuthPage } from "./GuestAuthPage";
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,
2021-09-10 19:20:17 +00:00
client,
2021-09-03 22:45:07 +00:00
login,
2021-09-10 19:20:17 +00:00
logout,
registerGuest,
2021-09-03 22:45:07 +00:00
register,
2021-09-10 19:20:17 +00:00
} = useClient(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="/">
2021-09-10 19:20:17 +00:00
<Home client={client} onLogout={logout} />
2021-08-20 23:23:12 +00:00
</AuthenticatedRoute>
<Route exact path="/login">
2021-09-10 19:20:17 +00:00
<LoginPage onLogin={login} />
2021-08-20 23:23:12 +00:00
</Route>
<Route exact path="/register">
2021-09-10 19:20:17 +00:00
<RegisterPage onRegister={register} />
2021-07-19 19:55:30 +00:00
</Route>
2021-09-03 22:45:07 +00:00
<Route path="/room/:roomId">
{authenticated ? (
2021-09-10 19:20:17 +00:00
<Room client={client} />
2021-09-03 22:45:07 +00:00
) : (
2021-09-10 19:20:17 +00:00
<GuestAuthPage onRegisterGuest={registerGuest} />
2021-09-03 22:45:07 +00:00
)}
</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>
);
}