mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {makeAutoObservable} from 'mobx'
2import {
3 AppBskyGraphGetMutes as GetMutes,
4 AppBskyActorDefs as ActorDefs,
5} from '@atproto/api'
6import {RootStoreModel} from '../root-store'
7import {cleanError} from 'lib/strings/errors'
8import {bundleAsync} from 'lib/async/bundle'
9
10const PAGE_SIZE = 30
11
12export class MutedAccountsModel {
13 // state
14 isLoading = false
15 isRefreshing = false
16 hasLoaded = false
17 error = ''
18 hasMore = true
19 loadMoreCursor?: string
20
21 // data
22 mutes: ActorDefs.ProfileView[] = []
23
24 constructor(public rootStore: RootStoreModel) {
25 makeAutoObservable(
26 this,
27 {
28 rootStore: false,
29 },
30 {autoBind: true},
31 )
32 }
33
34 get hasContent() {
35 return this.mutes.length > 0
36 }
37
38 get hasError() {
39 return this.error !== ''
40 }
41
42 get isEmpty() {
43 return this.hasLoaded && !this.hasContent
44 }
45
46 // public api
47 // =
48
49 async refresh() {
50 return this.loadMore(true)
51 }
52
53 loadMore = bundleAsync(async (replace: boolean = false) => {
54 if (!replace && !this.hasMore) {
55 return
56 }
57 this._xLoading(replace)
58 try {
59 const res = await this.rootStore.agent.app.bsky.graph.getMutes({
60 limit: PAGE_SIZE,
61 cursor: replace ? undefined : this.loadMoreCursor,
62 })
63 if (replace) {
64 this._replaceAll(res)
65 } else {
66 this._appendAll(res)
67 }
68 this._xIdle()
69 } catch (e: any) {
70 this._xIdle(e)
71 }
72 })
73
74 // state transitions
75 // =
76
77 _xLoading(isRefreshing = false) {
78 this.isLoading = true
79 this.isRefreshing = isRefreshing
80 this.error = ''
81 }
82
83 _xIdle(err?: any) {
84 this.isLoading = false
85 this.isRefreshing = false
86 this.hasLoaded = true
87 this.error = cleanError(err)
88 if (err) {
89 this.rootStore.log.error('Failed to fetch user followers', err)
90 }
91 }
92
93 // helper functions
94 // =
95
96 _replaceAll(res: GetMutes.Response) {
97 this.mutes = []
98 this._appendAll(res)
99 }
100
101 _appendAll(res: GetMutes.Response) {
102 this.loadMoreCursor = res.data.cursor
103 this.hasMore = !!this.loadMoreCursor
104 this.mutes = this.mutes.concat(res.data.mutes)
105 }
106}