From c34eac03e53f7bce403bc2341766f9df4951e35d Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Fri, 21 Jul 2023 15:08:53 -0400 Subject: [PATCH] Restore the client 'loading' state https://github.com/vector-im/element-call/pull/1173 regressed the client loading sequence, such that the app would pretend that you were signed out when it was really just loading your saved session. This makes the proper loading state appear again. --- src/ClientContext.tsx | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/ClientContext.tsx b/src/ClientContext.tsx index 69a9295e..9e87e1a2 100644 --- a/src/ClientContext.tsx +++ b/src/ClientContext.tsx @@ -149,8 +149,9 @@ interface Props { export const ClientProvider: FC = ({ children }) => { const history = useHistory(); + // null = signed out, undefined = loading const [initClientState, setInitClientState] = useState< - InitResult | undefined + InitResult | null | undefined >(undefined); const initializing = useRef(false); @@ -162,14 +163,7 @@ export const ClientProvider: FC = ({ children }) => { initializing.current = true; loadClient() - .then((maybeClient) => { - if (!maybeClient) { - logger.error("Failed to initialize client"); - return; - } - - setInitClientState(maybeClient); - }) + .then(setInitClientState) .catch((err) => logger.error(err)) .finally(() => (initializing.current = false)); }, []); @@ -264,20 +258,22 @@ export const ClientProvider: FC = ({ children }) => { }, [initClientState?.client, setAlreadyOpenedErr, t]) ); - const state: ClientState = useMemo(() => { + const state: ClientState | undefined = useMemo(() => { if (alreadyOpenedErr) { return { state: "error", error: alreadyOpenedErr }; } - let authenticated = undefined; - if (initClientState) { - authenticated = { - client: initClientState.client, - isPasswordlessUser: initClientState.passwordlessUser, - changePassword, - logout, - }; - } + if (initClientState === undefined) return undefined; + + const authenticated = + initClientState === null + ? undefined + : { + client: initClientState.client, + isPasswordlessUser: initClientState.passwordlessUser, + changePassword, + logout, + }; return { state: "valid", authenticated, setClient }; }, [alreadyOpenedErr, changePassword, initClientState, logout, setClient]); @@ -308,7 +304,7 @@ type InitResult = { passwordlessUser: boolean; }; -async function loadClient(): Promise { +async function loadClient(): Promise { if (widget) { // We're inside a widget, so let's engage *matryoshka mode* logger.log("Using a matryoshka client"); @@ -322,7 +318,8 @@ async function loadClient(): Promise { try { const session = loadSession(); if (!session) { - throw new Error("No session stored"); + logger.log("No session stored; continuing without a client"); + return null; } logger.log("Using a standalone client");