diff options
Diffstat (limited to 'src/state')
-rw-r--r-- | src/state/index.ts | 8 | ||||
-rw-r--r-- | src/state/models/root-store.ts | 11 | ||||
-rw-r--r-- | src/state/models/session.ts | 21 |
3 files changed, 37 insertions, 3 deletions
diff --git a/src/state/index.ts b/src/state/index.ts index 7c97ce294..496f3631d 100644 --- a/src/state/index.ts +++ b/src/state/index.ts @@ -1,5 +1,9 @@ import {onSnapshot} from 'mobx-state-tree' -import {RootStoreModel, RootStore} from './models/root-store' +import { + RootStoreModel, + RootStore, + createDefaultRootStore, +} from './models/root-store' import {Environment} from './env' import * as storage from './storage' @@ -15,7 +19,7 @@ export async function setupState() { rootStore = RootStoreModel.create(data, env) } catch (e) { console.error('Failed to load state from storage', e) - rootStore = RootStoreModel.create({}, env) + rootStore = RootStoreModel.create(createDefaultRootStore(), env) } // track changes & save to storage diff --git a/src/state/models/root-store.ts b/src/state/models/root-store.ts index 164dfcced..143c59ea1 100644 --- a/src/state/models/root-store.ts +++ b/src/state/models/root-store.ts @@ -4,12 +4,21 @@ import {Instance, SnapshotOut, types} from 'mobx-state-tree' import {createContext, useContext} from 'react' +import {SessionModel, createDefaultSession} from './session' -export const RootStoreModel = types.model('RootStore').props({}) +export const RootStoreModel = types.model('RootStore').props({ + session: SessionModel, +}) export interface RootStore extends Instance<typeof RootStoreModel> {} export interface RootStoreSnapshot extends SnapshotOut<typeof RootStoreModel> {} +export function createDefaultRootStore() { + return { + session: createDefaultSession(), + } +} + // react context & hook utilities const RootStoreContext = createContext<RootStore>({} as RootStore) export const RootStoreProvider = RootStoreContext.Provider diff --git a/src/state/models/session.ts b/src/state/models/session.ts new file mode 100644 index 000000000..675feb8bc --- /dev/null +++ b/src/state/models/session.ts @@ -0,0 +1,21 @@ +import {Instance, SnapshotOut, types} from 'mobx-state-tree' + +export const SessionModel = types + .model('Session') + .props({ + isAuthed: types.boolean, + }) + .actions(self => ({ + setAuthed: (v: boolean) => { + self.isAuthed = v + }, + })) + +export interface Session extends Instance<typeof SessionModel> {} +export interface SessionSnapshot extends SnapshotOut<typeof SessionModel> {} + +export function createDefaultSession() { + return { + isAuthed: false, + } +} |