about summary refs log tree commit diff
path: root/src/state/models/feed-view.ts
blob: 1fc507a70f5e62b348921159e9bef134d372bba7 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import {makeAutoObservable, runInAction} from 'mobx'
import {bsky} from '@adxp/mock-api'
import {RootStoreModel} from './root-store'

export class FeedViewItemModel implements bsky.FeedView.FeedItem {
  key: string = ''
  uri: string = ''
  author: bsky.FeedView.User = {did: '', name: '', displayName: ''}
  repostedBy?: bsky.FeedView.User
  record: Record<string, unknown> = {}
  embed?:
    | bsky.FeedView.RecordEmbed
    | bsky.FeedView.ExternalEmbed
    | bsky.FeedView.UnknownEmbed
  replyCount: number = 0
  repostCount: number = 0
  likeCount: number = 0
  indexedAt: string = ''

  constructor(key: string, v: bsky.FeedView.FeedItem) {
    makeAutoObservable(this)
    this.key = key
    Object.assign(this, v)
  }
}

export class FeedViewModel implements bsky.FeedView.Response {
  state = 'idle'
  error = ''
  params: bsky.FeedView.Params
  feed: FeedViewItemModel[] = []

  constructor(public rootStore: RootStoreModel, params: bsky.FeedView.Params) {
    makeAutoObservable(
      this,
      {rootStore: false, params: false},
      {autoBind: true},
    )
    this.params = params
  }

  get hasContent() {
    return this.feed.length !== 0
  }

  get hasError() {
    return this.error !== ''
  }

  get isLoading() {
    return this.state === 'loading'
  }

  get isEmpty() {
    return !this.hasContent && !this.hasError && !this.isLoading
  }

  async fetch() {
    if (this.hasContent) {
      await this.updateContent()
    } else {
      await this.initialLoad()
    }
  }

  async initialLoad() {
    this.state = 'loading'
    this.error = ''
    try {
      const res = (await this.rootStore.api.mainPds.view(
        'blueskyweb.xyz:FeedView',
        this.params,
      )) as bsky.FeedView.Response
      this._replaceAll(res)
      runInAction(() => {
        this.state = 'idle'
      })
    } catch (e: any) {
      runInAction(() => {
        this.state = 'error'
        this.error = `Failed to load feed: ${e.toString()}`
      })
    }
  }

  async updateContent() {
    // TODO: refetch and update items
  }

  private _replaceAll(res: bsky.FeedView.Response) {
    this.feed.length = 0
    let counter = 0
    for (const item of res.feed) {
      // TODO: validate .record
      this.feed.push(new FeedViewItemModel(`item-${counter++}`, item))
    }
  }
}