feat: fully working onboarding on login

This commit is contained in:
Paul Makles 2022-06-29 11:48:48 +01:00
parent 66ae518e51
commit 31220db8fe
3 changed files with 60 additions and 15 deletions

View file

@ -9,21 +9,25 @@ import { reportError } from "../../lib/ErrorBoundary";
import { useApplicationState } from "../../mobx/State"; import { useApplicationState } from "../../mobx/State";
import { useClient } from "../../controllers/client/ClientController"; import {
useClient,
useSession,
} from "../../controllers/client/ClientController";
export default function SyncManager() { export default function SyncManager() {
const client = useClient(); const client = useClient();
const session = useSession();
const state = useApplicationState(); const state = useApplicationState();
// Sync settings from Revolt. // Sync settings from Revolt.
useEffect(() => { useEffect(() => {
if (client) { if (session?.ready) {
state.sync state.sync
.pull(client) .pull(session.client!)
.catch(console.error) .catch(console.error)
.finally(() => state.changelog.checkForUpdates()); .finally(() => state.changelog.checkForUpdates());
} }
}, [client]); }, [session?.ready]);
// Take data updates from Revolt. // Take data updates from Revolt.
useEffect(() => { useEffect(() => {

View file

@ -64,7 +64,7 @@ class ClientController {
*/ */
@action hydrate(auth: Auth) { @action hydrate(auth: Auth) {
for (const entry of auth.getAccounts()) { for (const entry of auth.getAccounts()) {
this.addSession(entry); this.addSession(entry, "existing");
} }
this.pickNextSession(); this.pickNextSession();
@ -90,7 +90,10 @@ class ClientController {
return this.current === null; return this.current === null;
} }
@action addSession(entry: { session: SessionPrivate; apiUrl?: string }) { @action addSession(
entry: { session: SessionPrivate; apiUrl?: string },
knowledge: "new" | "existing",
) {
const user_id = entry.session.user_id!; const user_id = entry.session.user_id!;
const session = new Session(); const session = new Session();
@ -102,6 +105,7 @@ class ClientController {
session: entry.session, session: entry.session,
apiUrl: entry.apiUrl, apiUrl: entry.apiUrl,
configuration: this.configuration!, configuration: this.configuration!,
knowledge,
}) })
.catch((error) => { .catch((error) => {
if (error === "Forbidden" || error === "Unauthorized") { if (error === "Forbidden" || error === "Unauthorized") {
@ -177,9 +181,12 @@ class ClientController {
} }
} }
this.addSession({ this.addSession(
session, {
}); session,
},
"new",
);
/*const s = session; /*const s = session;

View file

@ -1,6 +1,10 @@
import { action, computed, makeAutoObservable } from "mobx"; import { action, computed, makeAutoObservable } from "mobx";
import { API, Client } from "revolt.js"; import { API, Client } from "revolt.js";
import { state } from "../../mobx/State";
import { __thisIsAHack } from "../../context/intermediate/Intermediate";
type State = "Ready" | "Connecting" | "Online" | "Disconnected" | "Offline"; type State = "Ready" | "Connecting" | "Online" | "Disconnected" | "Offline";
type Transition = type Transition =
@ -9,6 +13,8 @@ type Transition =
apiUrl?: string; apiUrl?: string;
session: SessionPrivate; session: SessionPrivate;
configuration?: API.RevoltConfig; configuration?: API.RevoltConfig;
knowledge: "new" | "existing";
} }
| { | {
action: action:
@ -104,6 +110,17 @@ export default class Session {
} }
} }
private async continueLogin(data: Transition & { action: "LOGIN" }) {
try {
await this.client!.useExistingSession(data.session);
this.user_id = this.client!.user!._id;
state.auth.setSession(data.session);
} catch (err) {
this.state = "Ready";
throw err;
}
}
@action async emit(data: Transition) { @action async emit(data: Transition) {
console.info(`[FSM ${this.user_id ?? "Anonymous"}]`, data); console.info(`[FSM ${this.user_id ?? "Anonymous"}]`, data);
@ -118,14 +135,31 @@ export default class Session {
this.client!.configuration = data.configuration; this.client!.configuration = data.configuration;
} }
try { if (data.knowledge === "new") {
await this.client!.useExistingSession(data.session); await this.client!.fetchConfiguration();
this.user_id = this.client!.user!._id; this.client!.session = data.session;
} catch (err) { (this.client! as any).$updateHeaders();
this.state = "Ready";
throw err; const { onboarding } = await this.client!.api.get(
"/onboard/hello",
);
if (onboarding) {
__thisIsAHack({
id: "onboarding",
callback: async (username: string) =>
this.client!.completeOnboarding(
{ username },
false,
).then(() => this.continueLogin(data)),
});
return;
}
} }
this.continueLogin(data);
break; break;
} }
// Ready successfully received // Ready successfully received