From 259ef27bd0484767c88826d390a11c2cfb79d3cc Mon Sep 17 00:00:00 2001 From: Daniel Abramov Date: Mon, 3 Jul 2023 19:14:03 +0100 Subject: [PATCH] Properly model the state of a group call load --- src/room/GroupCallLoader.tsx | 28 ++++++++++++---------------- src/room/useLoadGroupCall.ts | 34 +++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/room/GroupCallLoader.tsx b/src/room/GroupCallLoader.tsx index 3b201cb2..ca52a163 100644 --- a/src/room/GroupCallLoader.tsx +++ b/src/room/GroupCallLoader.tsx @@ -21,7 +21,6 @@ import { useTranslation } from "react-i18next"; import { useLoadGroupCall } from "./useLoadGroupCall"; import { ErrorView, FullScreenView } from "../FullScreenView"; -import { usePageTitle } from "../usePageTitle"; interface Props { client: MatrixClient; @@ -39,26 +38,23 @@ export function GroupCallLoader({ createPtt, }: Props): JSX.Element { const { t } = useTranslation(); - const { loading, error, groupCall } = useLoadGroupCall( + const groupCallState = useLoadGroupCall( client, roomIdOrAlias, viaServers, createPtt ); - usePageTitle(groupCall ? groupCall.room.name : t("Loading…")); - - if (loading) { - return ( - -

{t("Loading…")}

-
- ); + switch (groupCallState.kind) { + case "loading": + return ( + +

{t("Loading…")}

+
+ ); + case "loaded": + return <>{children(groupCallState.groupCall)}; + case "failed": + return ; } - - if (error) { - return ; - } - - return groupCall ? <>{children(groupCall)} : <>; } diff --git a/src/room/useLoadGroupCall.ts b/src/room/useLoadGroupCall.ts index 8adbe38e..4aa82f11 100644 --- a/src/room/useLoadGroupCall.ts +++ b/src/room/useLoadGroupCall.ts @@ -34,8 +34,26 @@ import { widget } from "../widget"; const STATS_COLLECT_INTERVAL_TIME_MS = 10000; +export type GroupCallLoaded = { + kind: "loaded"; + groupCall: GroupCall; +}; + +export type GroupCallLoadFailed = { + kind: "failed"; + error: Error; +}; + +export type GroupCallLoading = { + kind: "loading"; +}; + +export type GroupCallStatus = + | GroupCallLoaded + | GroupCallLoadFailed + | GroupCallLoading; + export interface GroupCallLoadState { - loading: boolean; error?: Error; groupCall?: GroupCall; } @@ -45,13 +63,11 @@ export const useLoadGroupCall = ( roomIdOrAlias: string, viaServers: string[], createPtt: boolean -): GroupCallLoadState => { +): GroupCallStatus => { const { t } = useTranslation(); - const [state, setState] = useState({ loading: true }); + const [state, setState] = useState({ kind: "loading" }); useEffect(() => { - setState({ loading: true }); - const fetchOrCreateRoom = async (): Promise => { try { // We lowercase the localpart when we create the room, so we must lowercase @@ -176,12 +192,8 @@ export const useLoadGroupCall = ( waitForClientSyncing() .then(fetchOrCreateGroupCall) - .then((groupCall) => - setState((prevState) => ({ ...prevState, loading: false, groupCall })) - ) - .catch((error) => - setState((prevState) => ({ ...prevState, loading: false, error })) - ); + .then((groupCall) => setState({ kind: "loaded", groupCall })) + .catch((error) => setState({ kind: "failed", error })); }, [client, roomIdOrAlias, viaServers, createPtt, t]); return state;