element-call/src/App.jsx

101 lines
2.7 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-11-19 18:47:40 +00:00
BrowserRouter 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-10-06 18:38:26 +00:00
import * as Sentry from "@sentry/react";
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-12-07 01:34:10 +00:00
import { OverlayProvider } from "@react-aria/overlays";
2021-07-16 21:22:03 +00:00
2021-10-06 18:38:26 +00:00
const SentryRoute = Sentry.withSentryRouting(Route);
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-12-04 00:42:29 +00:00
<Router>
2021-12-07 01:34:10 +00:00
<OverlayProvider>
2021-12-04 00:42:29 +00:00
{loading ? (
<Center>
<p>Loading...</p>
</Center>
) : (
<Switch>
<AuthenticatedRoute authenticated={authenticated} exact path="/">
<Home client={client} onLogout={logout} />
</AuthenticatedRoute>
<SentryRoute exact path="/login">
<LoginPage onLogin={login} />
</SentryRoute>
<SentryRoute exact path="/register">
<RegisterPage onRegister={register} />
</SentryRoute>
<SentryRoute path="/room/:roomId?">
{authenticated ? (
<Room client={client} onLogout={logout} />
) : (
<GuestAuthPage onLoginAsGuest={registerGuest} />
)}
</SentryRoute>
</Switch>
)}
2021-12-07 01:34:10 +00:00
</OverlayProvider>
2021-12-04 00:42:29 +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 (
2021-10-06 18:38:26 +00:00
<SentryRoute {...rest}>
2021-08-10 01:38:19 +00:00
{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 },
}}
/>
)}
2021-10-06 18:38:26 +00:00
</SentryRoute>
2021-08-10 01:38:19 +00:00
);
}