about summary refs log tree commit diff
path: root/src/state/models/user-follows-view.ts
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2023-01-19 16:39:40 -0600
committerPaul Frazee <pfrazee@gmail.com>2023-01-19 16:39:40 -0600
commitc11bfc7541e6f3e375b9143dc552343dacbd2e49 (patch)
treee22b7eb6e06732f7eb2fbd20e61f6e21b9637302 /src/state/models/user-follows-view.ts
parentaec0f1c3baf4eabefd3d0e561cd89f0217926705 (diff)
downloadvoidsky-c11bfc7541e6f3e375b9143dc552343dacbd2e49.tar.zst
Fixes to follows listing
Diffstat (limited to 'src/state/models/user-follows-view.ts')
-rw-r--r--src/state/models/user-follows-view.ts64
1 files changed, 33 insertions, 31 deletions
diff --git a/src/state/models/user-follows-view.ts b/src/state/models/user-follows-view.ts
index 341f0f806..40f12b43e 100644
--- a/src/state/models/user-follows-view.ts
+++ b/src/state/models/user-follows-view.ts
@@ -1,13 +1,13 @@
 import {makeAutoObservable} from 'mobx'
 import {
-  AppBskyGraphGetFollows as GetFollows,
+  AppBskyGraphGetFollowers as GetFollows,
   AppBskyActorRef as ActorRef,
 } from '@atproto/api'
 import {RootStoreModel} from './root-store'
 
-export type FollowItem = GetFollows.Follow & {
-  _reactKey: string
-}
+const PAGE_SIZE = 30
+
+export type FollowItem = GetFollows.Follow
 
 export class UserFollowsViewModel {
   // state
@@ -16,6 +16,9 @@ export class UserFollowsViewModel {
   hasLoaded = false
   error = ''
   params: GetFollows.QueryParams
+  hasMore = true
+  loadMoreCursor?: string
+  private _loadMorePromise: Promise<void> | undefined
 
   // data
   subject: ActorRef.WithInfo = {
@@ -55,16 +58,17 @@ export class UserFollowsViewModel {
   // public api
   // =
 
-  async setup() {
-    await this._fetch()
-  }
-
   async refresh() {
-    await this._fetch(true)
+    return this.loadMore(true)
   }
 
-  async loadMore() {
-    // TODO
+  async loadMore(isRefreshing = false) {
+    if (this._loadMorePromise) {
+      return this._loadMorePromise
+    }
+    this._loadMorePromise = this._loadMore(isRefreshing)
+    await this._loadMorePromise
+    this._loadMorePromise = undefined
   }
 
   // state transitions
@@ -89,32 +93,30 @@ export class UserFollowsViewModel {
   // loader functions
   // =
 
-  private async _fetch(isRefreshing = false) {
+  private async _loadMore(isRefreshing = false) {
+    if (!this.hasMore) {
+      return
+    }
     this._xLoading(isRefreshing)
     try {
-      const res = await this.rootStore.api.app.bsky.graph.getFollows(
-        this.params,
-      )
-      this._replaceAll(res)
+      const params = Object.assign({}, this.params, {
+        limit: PAGE_SIZE,
+        before: this.loadMoreCursor,
+      })
+      if (this.isRefreshing) {
+        this.follows = []
+      }
+      const res = await this.rootStore.api.app.bsky.graph.getFollows(params)
+      await this._appendAll(res)
       this._xIdle()
     } catch (e: any) {
-      this._xIdle(`Failed to load feed: ${e.toString()}`)
-    }
-  }
-
-  private _replaceAll(res: GetFollows.Response) {
-    this.subject.did = res.data.subject.did
-    this.subject.handle = res.data.subject.handle
-    this.subject.displayName = res.data.subject.displayName
-    this.subject.avatar = res.data.subject.avatar
-    this.follows.length = 0
-    let counter = 0
-    for (const item of res.data.follows) {
-      this._append({_reactKey: `item-${counter++}`, ...item})
+      this._xIdle(e)
     }
   }
 
-  private _append(item: FollowItem) {
-    this.follows.push(item)
+  private async _appendAll(res: GetFollows.Response) {
+    this.loadMoreCursor = res.data.cursor
+    this.hasMore = !!this.loadMoreCursor
+    this.follows = this.follows.concat(res.data.follows)
   }
 }