element-call/src/App.jsx

137 lines
3.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-12-10 21:27:27 +00:00
import React, { useEffect, useState } from "react";
import {
BrowserRouter as Router,
Switch,
Route,
useLocation,
useHistory,
} from "react-router-dom";
2021-10-06 18:38:26 +00:00
import * as Sentry from "@sentry/react";
2021-12-09 20:58:30 +00:00
import { OverlayProvider } from "@react-aria/overlays";
2021-08-20 00:49:45 +00:00
import { Home } from "./Home";
2021-08-20 23:23:12 +00:00
import { LoginPage } from "./LoginPage";
2021-12-09 20:58:30 +00:00
import { RegisterPage } from "./RegisterPage";
import { Room } from "./Room";
import { ClientProvider } from "./ConferenceCallManagerHooks";
2021-12-10 21:06:16 +00:00
import { useFocusVisible } from "@react-aria/interactions";
import styles from "./App.module.css";
2021-12-10 21:27:27 +00:00
import { ErrorModal } from "./ErrorModal";
2021-07-16 21:22:03 +00:00
2021-10-06 18:38:26 +00:00
const SentryRoute = Sentry.withSentryRouting(Route);
2021-12-09 20:58:30 +00:00
const { protocol, host } = window.location;
// Assume homeserver is hosted on same domain (proxied in development by vite)
const homeserverUrl = `${protocol}//${host}`;
2021-07-16 21:22:03 +00:00
2021-12-09 20:58:30 +00:00
export default function App() {
2021-12-10 21:06:16 +00:00
const { isFocusVisible } = useFocusVisible();
useEffect(() => {
const classList = document.body.classList;
const hasClass = classList.contains(styles.hideFocus);
if (isFocusVisible && hasClass) {
classList.remove(styles.hideFocus);
} else if (!isFocusVisible && !hasClass) {
classList.add(styles.hideFocus);
}
return () => {
classList.remove(styles.hideFocus);
};
}, [isFocusVisible]);
2021-07-16 21:22:03 +00:00
return (
2021-12-04 00:42:29 +00:00
<Router>
2021-12-09 20:58:30 +00:00
<ClientProvider homeserverUrl={homeserverUrl}>
<OverlayProvider>
2021-12-04 00:42:29 +00:00
<Switch>
2021-12-09 20:58:30 +00:00
<SentryRoute exact path="/">
<Home />
</SentryRoute>
2021-12-04 00:42:29 +00:00
<SentryRoute exact path="/login">
2021-12-09 20:58:30 +00:00
<LoginPage />
2021-12-04 00:42:29 +00:00
</SentryRoute>
<SentryRoute exact path="/register">
2021-12-09 20:58:30 +00:00
<RegisterPage />
2021-12-04 00:42:29 +00:00
</SentryRoute>
<SentryRoute path="/room/:roomId?">
2021-12-09 20:58:30 +00:00
<Room />
2021-12-04 00:42:29 +00:00
</SentryRoute>
2021-12-10 21:27:27 +00:00
<SentryRoute path="*">
<RoomRedirect />
</SentryRoute>
2021-12-04 00:42:29 +00:00
</Switch>
2021-12-09 20:58:30 +00:00
</OverlayProvider>
</ClientProvider>
2021-12-04 00:42:29 +00:00
</Router>
2021-07-16 21:22:03 +00:00
);
}
2021-12-10 21:27:27 +00:00
function RoomRedirect() {
const { pathname } = useLocation();
const history = useHistory();
const [error, setError] = useState();
useEffect(() => {
async function redirect() {
let roomId = pathname;
if (pathname.startsWith("/")) {
roomId = roomId.substr(1, roomId.length);
}
if (!roomId.startsWith("#") && !roomId.startsWith("!")) {
let loginHomeserverUrl = homeserverUrl.trim();
if (!loginHomeserverUrl.includes("://")) {
loginHomeserverUrl = "https://" + loginHomeserverUrl;
}
try {
const wellKnownUrl = new URL(
"/.well-known/matrix/client",
window.location
);
const response = await fetch(wellKnownUrl);
const config = await response.json();
if (config["m.homeserver"]) {
loginHomeserverUrl = config["m.homeserver"];
}
} catch (error) {}
const { host } = new URL(loginHomeserverUrl);
roomId = `#${roomId}:${host}`;
}
history.replace(`/room/${roomId}`);
}
redirect().catch(setError);
}, [history, pathname]);
if (error) {
return <ErrorModal error={error} />;
}
return <div>Loading...</div>;
}