From 35fb1e710b2e248b168c92655625e6139048d2fd Mon Sep 17 00:00:00 2001 From: Robert Long Date: Thu, 3 Mar 2022 16:56:45 -0800 Subject: [PATCH] Create room when not found and lowercase name --- src/matrix-utils.js | 6 +-- src/room/GroupCallLoader.jsx | 19 ++----- src/room/RoomNotFoundView.jsx | 74 ---------------------------- src/room/RoomNotFoundView.module.css | 11 ----- src/room/RoomRedirect.jsx | 2 +- src/room/useLoadGroupCall.js | 47 ++++++++++++++---- 6 files changed, 44 insertions(+), 115 deletions(-) delete mode 100644 src/room/RoomNotFoundView.jsx delete mode 100644 src/room/RoomNotFoundView.module.css diff --git a/src/matrix-utils.js b/src/matrix-utils.js index ee8d064..4634c46 100644 --- a/src/matrix-utils.js +++ b/src/matrix-utils.js @@ -55,10 +55,8 @@ export function roomNameFromRoomId(roomId) { .match(/([^:]+):.*$/)[1] .substring(1) .split("-") - .map((part) => - part.length > 0 ? part.charAt(0).toUpperCase() + part.slice(1) : part - ) - .join(" "); + .join(" ") + .toLowerCase(); } export function isLocalRoomId(roomId) { diff --git a/src/room/GroupCallLoader.jsx b/src/room/GroupCallLoader.jsx index 3aa361b..58934d0 100644 --- a/src/room/GroupCallLoader.jsx +++ b/src/room/GroupCallLoader.jsx @@ -2,14 +2,13 @@ import React from "react"; import { useLoadGroupCall } from "./useLoadGroupCall"; import { ErrorView, FullScreenView } from "../FullScreenView"; import { usePageTitle } from "../usePageTitle"; -import { isLocalRoomId } from "../matrix-utils"; -import { RoomNotFoundView } from "./RoomNotFoundView"; export function GroupCallLoader({ client, roomId, viaServers, children }) { - const { loading, error, groupCall, reload } = useLoadGroupCall( + const { loading, error, groupCall } = useLoadGroupCall( client, roomId, - viaServers + viaServers, + true ); usePageTitle(groupCall ? groupCall.room.name : "Loading..."); @@ -22,18 +21,6 @@ export function GroupCallLoader({ client, roomId, viaServers, children }) { ); } - if ( - error && - (error.errcode === "M_NOT_FOUND" || - (error.message && - error.message.indexOf("Failed to fetch alias") !== -1)) && - isLocalRoomId(roomId) - ) { - return ( - - ); - } - if (error) { return ; } diff --git a/src/room/RoomNotFoundView.jsx b/src/room/RoomNotFoundView.jsx deleted file mode 100644 index 0172085..0000000 --- a/src/room/RoomNotFoundView.jsx +++ /dev/null @@ -1,74 +0,0 @@ -import React, { useState, useCallback } from "react"; -import { FullScreenView } from "../FullScreenView"; -import { Headline, Subtitle } from "../typography/Typography"; -import { createRoom, roomNameFromRoomId } from "../matrix-utils"; -import { FieldRow, ErrorMessage, InputField } from "../input/Input"; -import { Button } from "../button"; -import { Form } from "../form/Form"; -import { useHistory } from "react-router-dom"; -import styles from "./RoomNotFoundView.module.css"; - -export function RoomNotFoundView({ client, roomId, onReload }) { - const history = useHistory(); - const [loading, setLoading] = useState(false); - const [error, setError] = useState(); - const roomName = roomNameFromRoomId(roomId); - const onSubmit = useCallback( - (e) => { - e.preventDefault(); - - async function submit() { - setError(undefined); - setLoading(true); - - await createRoom(client, roomName); - - onReload(); - } - - submit().catch((error) => { - console.error(error); - setLoading(false); - setError(error); - }); - }, - [client, roomName] - ); - - return ( - - Call Not Found - Would you like to create this call? -
- - - - - - - {error && ( - - {error.message} - - )} -
-
- ); -} diff --git a/src/room/RoomNotFoundView.module.css b/src/room/RoomNotFoundView.module.css deleted file mode 100644 index 3270b87..0000000 --- a/src/room/RoomNotFoundView.module.css +++ /dev/null @@ -1,11 +0,0 @@ -.form { - padding: 0 24px; - justify-content: center; - max-width: 409px; - width: calc(100% - 48px); - margin-bottom: 72px; -} - -.button { - width: 100%; -} diff --git a/src/room/RoomRedirect.jsx b/src/room/RoomRedirect.jsx index 16454f8..37c3f17 100644 --- a/src/room/RoomRedirect.jsx +++ b/src/room/RoomRedirect.jsx @@ -18,7 +18,7 @@ export function RoomRedirect() { roomId = `#${roomId}:${defaultHomeserverHost}`; } - history.replace(`/room/${roomId}`); + history.replace(`/room/${roomId.toLowerCase()}`); }, [pathname, history]); return ; diff --git a/src/room/useLoadGroupCall.js b/src/room/useLoadGroupCall.js index ffd3033..c763ce7 100644 --- a/src/room/useLoadGroupCall.js +++ b/src/room/useLoadGroupCall.js @@ -1,4 +1,5 @@ -import { useState, useEffect, useCallback } from "react"; +import { useState, useEffect } from "react"; +import { isLocalRoomId, createRoom, roomNameFromRoomId } from "../matrix-utils"; async function fetchGroupCall( client, @@ -36,17 +37,49 @@ async function fetchGroupCall( }); } -export function useLoadGroupCall(client, roomId, viaServers) { +export function useLoadGroupCall(client, roomId, viaServers, createIfNotFound) { const [state, setState] = useState({ loading: true, error: undefined, groupCall: undefined, - reloadId: 0, }); useEffect(() => { + async function fetchOrCreateGroupCall() { + try { + const groupCall = await fetchGroupCall( + client, + roomId, + viaServers, + 30000 + ); + return groupCall; + } catch (error) { + if ( + createIfNotFound && + (error.errcode === "M_NOT_FOUND" || + (error.message && + error.message.indexOf("Failed to fetch alias") !== -1)) && + isLocalRoomId(roomId) + ) { + const roomName = roomNameFromRoomId(roomId); + await createRoom(client, roomName); + const groupCall = await fetchGroupCall( + client, + roomId, + viaServers, + 30000 + ); + return groupCall; + } + + throw error; + } + } + setState({ loading: true }); - fetchGroupCall(client, roomId, viaServers, 30000) + + fetchOrCreateGroupCall() .then((groupCall) => setState((prevState) => ({ ...prevState, loading: false, groupCall })) ) @@ -55,9 +88,5 @@ export function useLoadGroupCall(client, roomId, viaServers) { ); }, [client, roomId, state.reloadId]); - const reload = useCallback(() => { - setState((prevState) => ({ ...prevState, reloadId: prevState.reloadId++ })); - }, []); - - return { ...state, reload }; + return state; }