diff --git a/external/lang b/external/lang index def08f21..50838167 160000 --- a/external/lang +++ b/external/lang @@ -1 +1 @@ -Subproject commit def08f210e9edc4f203cb38611fd270761102860 +Subproject commit 50838167d7d253de9d08715e6a6070c3ddc9fcc2 diff --git a/src/context/index.tsx b/src/context/index.tsx index 1785174b..5b30422c 100644 --- a/src/context/index.tsx +++ b/src/context/index.tsx @@ -20,7 +20,7 @@ const uiContext = { Link, Text: Text as any, Trigger: ContextMenuTrigger, - emitAction: () => {}, + emitAction: () => void {}, }; /** diff --git a/src/context/revoltjs/RevoltClient.tsx b/src/context/revoltjs/RevoltClient.tsx index f16f3ac1..b76f4585 100644 --- a/src/context/revoltjs/RevoltClient.tsx +++ b/src/context/revoltjs/RevoltClient.tsx @@ -9,6 +9,7 @@ import { Preloader } from "@revoltchat/ui"; import { useApplicationState } from "../../mobx/State"; +import { clientController } from "../../controllers/client/ClientController"; import { modalController } from "../../controllers/modals/ModalController"; import { registerEvents } from "./events"; import { takeError } from "./util"; @@ -36,8 +37,8 @@ type Props = { }; export default observer(({ children }: Props) => { - const state = useApplicationState(); - const [client, setClient] = useState(null!); + // const state = useApplicationState(); + /*const [client, setClient] = useState(null!); const [status, setStatus] = useState(ClientStatus.LOADING); const [loaded, setLoaded] = useState(false); @@ -84,16 +85,24 @@ export default observer(({ children }: Props) => { }, [state.auth.getSession()]); useEffect(() => registerEvents(state, setStatus, client), [client]); - useEffect(() => state.registerListeners(client), [client]); if (!loaded || status === ClientStatus.LOADING) { return ; + }*/ + + const session = clientController.getActiveSession(); + if (!session?.ready) { + return ; } + const client = session.client!; + const state = useApplicationState(); + useEffect(() => state.registerListeners(client), [state, client]); + return ( - - - + + + void {}}> {children} diff --git a/src/controllers/client/ClientController.tsx b/src/controllers/client/ClientController.tsx index 25fc536c..56200d97 100644 --- a/src/controllers/client/ClientController.tsx +++ b/src/controllers/client/ClientController.tsx @@ -1,4 +1,4 @@ -import { action, makeAutoObservable, ObservableMap } from "mobx"; +import { action, computed, makeAutoObservable, ObservableMap } from "mobx"; import type { Nullable } from "revolt.js"; import Auth from "../../mobx/stores/Auth"; @@ -30,20 +30,35 @@ class ClientController { @action hydrate(auth: Auth) { for (const entry of auth.getAccounts()) { const session = new Session(); + this.sessions.set(entry.session._id!, session); session.emit({ action: "LOGIN", session: entry.session, }); } + + this.current = this.sessions.keys().next().value ?? null; } - getActiveSession() { - return this.sessions; + @computed getActiveSession() { + return this.sessions.get(this.current!); } - isLoggedIn() { + @computed isLoggedIn() { return this.current === null; } + + @action logout(user_id: string) { + const session = this.sessions.get(user_id); + if (session) { + this.sessions.delete(user_id); + if (user_id === this.current) { + this.current = this.sessions.keys().next().value ?? null; + } + + session.destroy(); + } + } } export const clientController = new ClientController(); diff --git a/src/controllers/client/Session.tsx b/src/controllers/client/Session.tsx index ec9c7e45..1189e059 100644 --- a/src/controllers/client/Session.tsx +++ b/src/controllers/client/Session.tsx @@ -1,4 +1,4 @@ -import { action, makeAutoObservable } from "mobx"; +import { action, computed, makeAutoObservable } from "mobx"; import { Client } from "revolt.js"; type State = "Ready" | "Connecting" | "Online" | "Disconnected" | "Offline"; @@ -34,6 +34,17 @@ export default class Session { window.addEventListener("offline", this.onOffline); } + /** + * Initiate logout and destroy client. + */ + @action destroy() { + if (this.client) { + this.client.logout(false); + this.state = "Ready"; + this.client = null; + } + } + private onOnline() { this.emit({ action: "ONLINE", @@ -90,6 +101,8 @@ export default class Session { } @action async emit(data: Transition) { + console.info("Handle event:", data); + switch (data.action) { // Login with session case "LOGIN": { @@ -161,4 +174,12 @@ export default class Session { } } } + + /** + * Whether we are ready to render. + * @returns Boolean + */ + @computed get ready() { + return this.client?.user; + } } diff --git a/src/mobx/stores/Auth.ts b/src/mobx/stores/Auth.ts index 1d63e7be..33799f7e 100644 --- a/src/mobx/stores/Auth.ts +++ b/src/mobx/stores/Auth.ts @@ -2,6 +2,7 @@ import { action, computed, makeAutoObservable, ObservableMap } from "mobx"; import { mapToRecord } from "../../lib/conversion"; +import { clientController } from "../../controllers/client/ClientController"; import Persistent from "../interfaces/Persistent"; import Store from "../interfaces/Store"; @@ -110,7 +111,8 @@ export default class Auth implements Store, Persistent { * Check whether we are currently logged in. * @returns Whether we are logged in */ - /*@computed isLoggedIn() { - return this.current !== null; - }*/ + @computed isLoggedIn() { + // ! FIXME: temp proxy info + return clientController.getActiveSession()?.ready; + } }