From 189d0b5ff91c23852ae7c4b3a5c33c98a3deb398 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 24 Dec 2021 12:00:24 +0000 Subject: [PATCH] fix(mobx): properly persist login state --- src/context/index.tsx | 6 +++++- src/context/revoltjs/SyncManager.tsx | 5 ++++- src/mobx/State.ts | 3 ++- src/mobx/stores/Auth.ts | 1 + src/pages/RevoltApp.tsx | 1 - 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/context/index.tsx b/src/context/index.tsx index 40238b4c..8201f603 100644 --- a/src/context/index.tsx +++ b/src/context/index.tsx @@ -11,6 +11,7 @@ import Locale from "./Locale"; import Theme from "./Theme"; import Intermediate from "./intermediate/Intermediate"; import Client from "./revoltjs/RevoltClient"; +import SyncManager from "./revoltjs/SyncManager"; /** * This component provides all of the application's context layers. @@ -29,7 +30,10 @@ export default function Context({ children }: { children: Children }) { - {children} + + {children} + + diff --git a/src/context/revoltjs/SyncManager.tsx b/src/context/revoltjs/SyncManager.tsx index cec9cd5c..aa23ca6f 100644 --- a/src/context/revoltjs/SyncManager.tsx +++ b/src/context/revoltjs/SyncManager.tsx @@ -15,7 +15,9 @@ export default function SyncManager() { // Sync settings from Revolt. useEffect(() => { - state.sync.pull(client); + if (client) { + state.sync.pull(client); + } }, [client]); // Keep data synced. @@ -23,6 +25,7 @@ export default function SyncManager() { // Take data updates from Revolt. useEffect(() => { + if (!client) return; function onPacket(packet: ClientboundNotification) { if (packet.type === "UserSettingsUpdate") { state.sync.apply(packet.update); diff --git a/src/mobx/State.ts b/src/mobx/State.ts index 5c4a6efe..b3e861cc 100644 --- a/src/mobx/State.ts +++ b/src/mobx/State.ts @@ -112,7 +112,7 @@ export default class State { * Register reaction listeners for persistent data stores. * @returns Function to dispose of listeners */ - registerListeners(client: Client) { + registerListeners(client?: Client) { const listeners = this.persistent.map(([id, store]) => { return reaction( () => stringify(store.toJSON()), @@ -120,6 +120,7 @@ export default class State { try { await localforage.setItem(id, JSON.parse(value)); if (id === "sync") return; + if (!client) return; const revision = +new Date(); switch (id) { diff --git a/src/mobx/stores/Auth.ts b/src/mobx/stores/Auth.ts index baee76b8..973c262d 100644 --- a/src/mobx/stores/Auth.ts +++ b/src/mobx/stores/Auth.ts @@ -85,6 +85,7 @@ export default class Auth implements Store, Persistent { @action setSession(session: Session) { this.sessions.set(session.user_id, { session }); this.current = session.user_id; + console.log(this.sessions, this.current); } /** diff --git a/src/pages/RevoltApp.tsx b/src/pages/RevoltApp.tsx index 4438e4f3..bd82df73 100644 --- a/src/pages/RevoltApp.tsx +++ b/src/pages/RevoltApp.tsx @@ -129,7 +129,6 @@ export default function App() { - );