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.
This commit is contained in:
Robin Townsend
2023-07-21 15:08:53 -04:00
parent 6ac14adc02
commit c34eac03e5

View File

@@ -149,8 +149,9 @@ interface Props {
export const ClientProvider: FC<Props> = ({ children }) => { export const ClientProvider: FC<Props> = ({ children }) => {
const history = useHistory(); const history = useHistory();
// null = signed out, undefined = loading
const [initClientState, setInitClientState] = useState< const [initClientState, setInitClientState] = useState<
InitResult | undefined InitResult | null | undefined
>(undefined); >(undefined);
const initializing = useRef(false); const initializing = useRef(false);
@@ -162,14 +163,7 @@ export const ClientProvider: FC<Props> = ({ children }) => {
initializing.current = true; initializing.current = true;
loadClient() loadClient()
.then((maybeClient) => { .then(setInitClientState)
if (!maybeClient) {
logger.error("Failed to initialize client");
return;
}
setInitClientState(maybeClient);
})
.catch((err) => logger.error(err)) .catch((err) => logger.error(err))
.finally(() => (initializing.current = false)); .finally(() => (initializing.current = false));
}, []); }, []);
@@ -264,20 +258,22 @@ export const ClientProvider: FC<Props> = ({ children }) => {
}, [initClientState?.client, setAlreadyOpenedErr, t]) }, [initClientState?.client, setAlreadyOpenedErr, t])
); );
const state: ClientState = useMemo(() => { const state: ClientState | undefined = useMemo(() => {
if (alreadyOpenedErr) { if (alreadyOpenedErr) {
return { state: "error", error: alreadyOpenedErr }; return { state: "error", error: alreadyOpenedErr };
} }
let authenticated = undefined; if (initClientState === undefined) return undefined;
if (initClientState) {
authenticated = { const authenticated =
client: initClientState.client, initClientState === null
isPasswordlessUser: initClientState.passwordlessUser, ? undefined
changePassword, : {
logout, client: initClientState.client,
}; isPasswordlessUser: initClientState.passwordlessUser,
} changePassword,
logout,
};
return { state: "valid", authenticated, setClient }; return { state: "valid", authenticated, setClient };
}, [alreadyOpenedErr, changePassword, initClientState, logout, setClient]); }, [alreadyOpenedErr, changePassword, initClientState, logout, setClient]);
@@ -308,7 +304,7 @@ type InitResult = {
passwordlessUser: boolean; passwordlessUser: boolean;
}; };
async function loadClient(): Promise<InitResult> { async function loadClient(): Promise<InitResult | null> {
if (widget) { if (widget) {
// We're inside a widget, so let's engage *matryoshka mode* // We're inside a widget, so let's engage *matryoshka mode*
logger.log("Using a matryoshka client"); logger.log("Using a matryoshka client");
@@ -322,7 +318,8 @@ async function loadClient(): Promise<InitResult> {
try { try {
const session = loadSession(); const session = loadSession();
if (!session) { 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"); logger.log("Using a standalone client");