diff options
author | Paul Frazee <pfrazee@gmail.com> | 2022-12-05 13:25:04 -0600 |
---|---|---|
committer | Paul Frazee <pfrazee@gmail.com> | 2022-12-05 13:25:04 -0600 |
commit | f27e32e54c3e6a6f7d156cf6c23c11778a7dd316 (patch) | |
tree | 484dbd281d938082c0de8ec3ba346ed8af839429 /src/state/models/me.ts | |
parent | 59363181e1c72dcec3a86cf155f12a556e569b8f (diff) | |
download | voidsky-f27e32e54c3e6a6f7d156cf6c23c11778a7dd316.tar.zst |
Ensure the UI always renders, even in bad network conditions (close #6)
Diffstat (limited to 'src/state/models/me.ts')
-rw-r--r-- | src/state/models/me.ts | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/src/state/models/me.ts b/src/state/models/me.ts index e3405b80d..fde387ebe 100644 --- a/src/state/models/me.ts +++ b/src/state/models/me.ts @@ -2,6 +2,7 @@ import {makeAutoObservable, runInAction} from 'mobx' import {RootStoreModel} from './root-store' import {MembershipsViewModel} from './memberships-view' import {NotificationsViewModel} from './notifications-view' +import {isObj, hasProp} from '../lib/type-guards' export class MeModel { did?: string @@ -13,7 +14,11 @@ export class MeModel { notifications: NotificationsViewModel constructor(public rootStore: RootStoreModel) { - makeAutoObservable(this, {rootStore: false}, {autoBind: true}) + makeAutoObservable( + this, + {rootStore: false, serialize: false, hydrate: false}, + {autoBind: true}, + ) this.notifications = new NotificationsViewModel(this.rootStore, {}) } @@ -26,9 +31,42 @@ export class MeModel { this.memberships = undefined } + serialize(): unknown { + return { + did: this.did, + handle: this.handle, + displayName: this.displayName, + description: this.description, + } + } + + hydrate(v: unknown) { + if (isObj(v)) { + let did, handle, displayName, description + if (hasProp(v, 'did') && typeof v.did === 'string') { + did = v.did + } + if (hasProp(v, 'handle') && typeof v.handle === 'string') { + handle = v.handle + } + if (hasProp(v, 'displayName') && typeof v.displayName === 'string') { + displayName = v.displayName + } + if (hasProp(v, 'description') && typeof v.description === 'string') { + description = v.description + } + if (did && handle) { + this.did = did + this.handle = handle + this.displayName = displayName + this.description = description + } + } + } + async load() { const sess = this.rootStore.session - if (sess.isAuthed && sess.data) { + if (sess.hasSession && sess.data) { this.did = sess.data.did || '' this.handle = sess.data.handle const profile = await this.rootStore.api.app.bsky.actor.getProfile({ |