2021-08-05 09:47:00 -04:00
|
|
|
/* eslint-disable react-hooks/rules-of-hooks */
|
2021-06-18 14:25:33 -04:00
|
|
|
import { Client } from "revolt.js";
|
2021-06-18 15:07:26 -04:00
|
|
|
import { Route } from "revolt.js/dist/api/routes";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
|
|
|
import { createContext } from "preact";
|
2021-07-24 06:22:08 -04:00
|
|
|
import { useContext, useEffect, useMemo, useState } from "preact/hooks";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
|
|
|
import { dispatch } from "../../redux";
|
2021-06-18 15:07:26 -04:00
|
|
|
import { connectState } from "../../redux/connector";
|
|
|
|
import { AuthState } from "../../redux/reducers/auth";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
|
|
|
import Preloader from "../../components/ui/Preloader";
|
|
|
|
|
|
|
|
import { Children } from "../../types/Preact";
|
|
|
|
import { useIntermediate } from "../intermediate/Intermediate";
|
2021-06-18 17:47:25 -04:00
|
|
|
import { registerEvents, setReconnectDisallowed } from "./events";
|
2021-07-05 06:23:23 -04:00
|
|
|
import { takeError } from "./util";
|
2021-08-30 11:08:22 -04:00
|
|
|
import { Session } from "revolt-api/types/Auth";
|
2021-06-18 12:57:08 -04:00
|
|
|
|
|
|
|
export enum ClientStatus {
|
2021-07-05 06:25:20 -04:00
|
|
|
INIT,
|
|
|
|
LOADING,
|
|
|
|
READY,
|
|
|
|
OFFLINE,
|
|
|
|
DISCONNECTED,
|
|
|
|
CONNECTING,
|
|
|
|
RECONNECTING,
|
|
|
|
ONLINE,
|
2021-06-18 12:57:08 -04:00
|
|
|
}
|
|
|
|
|
2021-06-18 15:07:26 -04:00
|
|
|
export interface ClientOperations {
|
2021-07-05 06:25:20 -04:00
|
|
|
login: (data: Route<"POST", "/auth/login">["data"]) => Promise<void>;
|
|
|
|
logout: (shouldRequest?: boolean) => Promise<void>;
|
|
|
|
loggedIn: () => boolean;
|
|
|
|
ready: () => boolean;
|
2021-06-18 15:07:26 -04:00
|
|
|
}
|
|
|
|
|
2021-07-04 21:22:33 -04:00
|
|
|
// By the time they are used, they should all be initialized.
|
|
|
|
// Currently the app does not render until a client is built and the other two are always initialized on first render.
|
|
|
|
// - insert's words
|
2021-07-04 07:09:39 -04:00
|
|
|
export const AppContext = createContext<Client>(null!);
|
|
|
|
export const StatusContext = createContext<ClientStatus>(null!);
|
|
|
|
export const OperationsContext = createContext<ClientOperations>(null!);
|
2021-06-18 15:07:26 -04:00
|
|
|
|
2021-07-05 05:59:48 -04:00
|
|
|
type Props = {
|
2021-07-05 06:25:20 -04:00
|
|
|
auth: AuthState;
|
|
|
|
children: Children;
|
2021-06-18 15:07:26 -04:00
|
|
|
};
|
|
|
|
|
2021-07-05 05:59:48 -04:00
|
|
|
function Context({ auth, children }: Props) {
|
2021-07-05 06:25:20 -04:00
|
|
|
const { openScreen } = useIntermediate();
|
|
|
|
const [status, setStatus] = useState(ClientStatus.INIT);
|
|
|
|
const [client, setClient] = useState<Client>(
|
|
|
|
undefined as unknown as Client,
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
const client = new Client({
|
|
|
|
autoReconnect: false,
|
|
|
|
apiURL: import.meta.env.VITE_API_URL,
|
|
|
|
debug: import.meta.env.DEV,
|
|
|
|
});
|
|
|
|
|
|
|
|
setClient(client);
|
|
|
|
setStatus(ClientStatus.LOADING);
|
|
|
|
})();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (status === ClientStatus.INIT) return null;
|
|
|
|
|
|
|
|
const operations: ClientOperations = useMemo(() => {
|
|
|
|
return {
|
|
|
|
login: async (data) => {
|
|
|
|
setReconnectDisallowed(true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const onboarding = await client.login(data);
|
|
|
|
setReconnectDisallowed(false);
|
|
|
|
const login = () =>
|
|
|
|
dispatch({
|
|
|
|
type: "LOGIN",
|
2021-08-30 11:08:22 -04:00
|
|
|
session: client.session as Session,
|
2021-07-05 06:25:20 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
if (onboarding) {
|
|
|
|
openScreen({
|
|
|
|
id: "onboarding",
|
2021-07-25 08:55:45 -04:00
|
|
|
callback: async (username: string) =>
|
2021-07-05 06:25:20 -04:00
|
|
|
onboarding(username, true).then(login),
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
login();
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
setReconnectDisallowed(false);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
logout: async (shouldRequest) => {
|
|
|
|
dispatch({ type: "LOGOUT" });
|
|
|
|
|
|
|
|
client.reset();
|
|
|
|
dispatch({ type: "RESET" });
|
|
|
|
|
|
|
|
openScreen({ id: "none" });
|
|
|
|
setStatus(ClientStatus.READY);
|
|
|
|
|
|
|
|
client.websocket.disconnect();
|
|
|
|
|
|
|
|
if (shouldRequest) {
|
|
|
|
try {
|
|
|
|
await client.logout();
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
loggedIn: () => typeof auth.active !== "undefined",
|
|
|
|
ready: () =>
|
|
|
|
operations.loggedIn() && typeof client.user !== "undefined",
|
|
|
|
};
|
2021-08-05 09:47:00 -04:00
|
|
|
}, [client, auth.active, openScreen]);
|
2021-07-05 06:25:20 -04:00
|
|
|
|
|
|
|
useEffect(
|
2021-07-29 16:01:03 -04:00
|
|
|
() => registerEvents({ operations }, setStatus, client),
|
2021-08-05 09:47:00 -04:00
|
|
|
[client, operations],
|
2021-07-05 06:25:20 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
if (auth.active) {
|
|
|
|
dispatch({ type: "QUEUE_FAIL_ALL" });
|
|
|
|
|
|
|
|
const active = auth.accounts[auth.active];
|
|
|
|
client.user = client.users.get(active.session.user_id);
|
|
|
|
if (!navigator.onLine) {
|
|
|
|
return setStatus(ClientStatus.OFFLINE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operations.ready()) setStatus(ClientStatus.CONNECTING);
|
|
|
|
|
|
|
|
if (navigator.onLine) {
|
|
|
|
await client
|
|
|
|
.fetchConfiguration()
|
|
|
|
.catch(() =>
|
|
|
|
console.error("Failed to connect to API server."),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await client.fetchConfiguration();
|
|
|
|
const callback = await client.useExistingSession(
|
|
|
|
active.session,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
openScreen({ id: "onboarding", callback });
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
setStatus(ClientStatus.DISCONNECTED);
|
|
|
|
const error = takeError(err);
|
|
|
|
if (error === "Forbidden" || error === "Unauthorized") {
|
|
|
|
operations.logout(true);
|
|
|
|
openScreen({ id: "signed_out" });
|
|
|
|
} else {
|
|
|
|
openScreen({ id: "error", error });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
await client.fetchConfiguration();
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Failed to connect to API server.");
|
|
|
|
}
|
|
|
|
|
|
|
|
setStatus(ClientStatus.READY);
|
|
|
|
}
|
|
|
|
})();
|
2021-08-05 09:47:00 -04:00
|
|
|
// eslint-disable-next-line
|
2021-07-05 06:25:20 -04:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (status === ClientStatus.LOADING) {
|
|
|
|
return <Preloader type="spinner" />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AppContext.Provider value={client}>
|
|
|
|
<StatusContext.Provider value={status}>
|
|
|
|
<OperationsContext.Provider value={operations}>
|
|
|
|
{children}
|
|
|
|
</OperationsContext.Provider>
|
|
|
|
</StatusContext.Provider>
|
|
|
|
</AppContext.Provider>
|
|
|
|
);
|
2021-06-18 15:07:26 -04:00
|
|
|
}
|
|
|
|
|
2021-07-05 06:23:23 -04:00
|
|
|
export default connectState<{ children: Children }>(Context, (state) => {
|
2021-07-05 06:25:20 -04:00
|
|
|
return {
|
|
|
|
auth: state.auth,
|
|
|
|
sync: state.sync,
|
|
|
|
};
|
2021-07-05 06:23:23 -04:00
|
|
|
});
|
2021-07-24 06:22:08 -04:00
|
|
|
|
|
|
|
export const useClient = () => useContext(AppContext);
|