Properly model the state of a group call load

This commit is contained in:
Daniel Abramov
2023-07-03 19:14:03 +01:00
parent 647599a39e
commit 259ef27bd0
2 changed files with 35 additions and 27 deletions

View File

@@ -21,7 +21,6 @@ import { useTranslation } from "react-i18next";
import { useLoadGroupCall } from "./useLoadGroupCall"; import { useLoadGroupCall } from "./useLoadGroupCall";
import { ErrorView, FullScreenView } from "../FullScreenView"; import { ErrorView, FullScreenView } from "../FullScreenView";
import { usePageTitle } from "../usePageTitle";
interface Props { interface Props {
client: MatrixClient; client: MatrixClient;
@@ -39,26 +38,23 @@ export function GroupCallLoader({
createPtt, createPtt,
}: Props): JSX.Element { }: Props): JSX.Element {
const { t } = useTranslation(); const { t } = useTranslation();
const { loading, error, groupCall } = useLoadGroupCall( const groupCallState = useLoadGroupCall(
client, client,
roomIdOrAlias, roomIdOrAlias,
viaServers, viaServers,
createPtt createPtt
); );
usePageTitle(groupCall ? groupCall.room.name : t("Loading…")); switch (groupCallState.kind) {
case "loading":
if (loading) { return (
return ( <FullScreenView>
<FullScreenView> <h1>{t("Loading…")}</h1>
<h1>{t("Loading…")}</h1> </FullScreenView>
</FullScreenView> );
); case "loaded":
return <>{children(groupCallState.groupCall)}</>;
case "failed":
return <ErrorView error={groupCallState.error} />;
} }
if (error) {
return <ErrorView error={error} />;
}
return groupCall ? <>{children(groupCall)}</> : <></>;
} }

View File

@@ -34,8 +34,26 @@ import { widget } from "../widget";
const STATS_COLLECT_INTERVAL_TIME_MS = 10000; 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 { export interface GroupCallLoadState {
loading: boolean;
error?: Error; error?: Error;
groupCall?: GroupCall; groupCall?: GroupCall;
} }
@@ -45,13 +63,11 @@ export const useLoadGroupCall = (
roomIdOrAlias: string, roomIdOrAlias: string,
viaServers: string[], viaServers: string[],
createPtt: boolean createPtt: boolean
): GroupCallLoadState => { ): GroupCallStatus => {
const { t } = useTranslation(); const { t } = useTranslation();
const [state, setState] = useState<GroupCallLoadState>({ loading: true }); const [state, setState] = useState<GroupCallStatus>({ kind: "loading" });
useEffect(() => { useEffect(() => {
setState({ loading: true });
const fetchOrCreateRoom = async (): Promise<Room> => { const fetchOrCreateRoom = async (): Promise<Room> => {
try { try {
// We lowercase the localpart when we create the room, so we must lowercase // We lowercase the localpart when we create the room, so we must lowercase
@@ -176,12 +192,8 @@ export const useLoadGroupCall = (
waitForClientSyncing() waitForClientSyncing()
.then(fetchOrCreateGroupCall) .then(fetchOrCreateGroupCall)
.then((groupCall) => .then((groupCall) => setState({ kind: "loaded", groupCall }))
setState((prevState) => ({ ...prevState, loading: false, groupCall })) .catch((error) => setState({ kind: "failed", error }));
)
.catch((error) =>
setState((prevState) => ({ ...prevState, loading: false, error }))
);
}, [client, roomIdOrAlias, viaServers, createPtt, t]); }, [client, roomIdOrAlias, viaServers, createPtt, t]);
return state; return state;