about summary refs log tree commit diff
path: root/src/state/models/me.ts
blob: 3fd5db9ac4909c85d2d02ca0f009fe71a67f5a6b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import {makeAutoObservable, runInAction} from 'mobx'
import {RootStoreModel} from './root-store'

export class MeModel {
  did?: string
  name?: string
  displayName?: string
  description?: string

  constructor(public rootStore: RootStoreModel) {
    makeAutoObservable(this, {rootStore: false}, {autoBind: true})
  }

  async load() {
    const sess = this.rootStore.session
    if (sess.isAuthed) {
      const userDb = this.rootStore.api.mockDb.mainUser
      this.did = userDb.did
      this.name = userDb.name
      const profile = await this.rootStore.api
        .repo(this.did, true)
        .collection('blueskyweb.xyz:Profiles')
        .get('Profile', 'profile')
        .catch(_ => undefined)
      runInAction(() => {
        if (profile?.valid) {
          this.displayName = profile.value.displayName
          this.description = profile.value.description
        } else {
          this.displayName = ''
          this.description = ''
        }
      })
    } else {
      this.did = undefined
      this.name = undefined
      this.displayName = undefined
      this.description = undefined
    }
  }
}