about summary refs log tree commit diff
path: root/src/state/models
diff options
context:
space:
mode:
Diffstat (limited to 'src/state/models')
-rw-r--r--src/state/models/user-followers-view.ts62
1 files changed, 32 insertions, 30 deletions
diff --git a/src/state/models/user-followers-view.ts b/src/state/models/user-followers-view.ts
index 6cb8dfeb6..9daaf35a4 100644
--- a/src/state/models/user-followers-view.ts
+++ b/src/state/models/user-followers-view.ts
@@ -5,9 +5,9 @@ import {
 } from '@atproto/api'
 import {RootStoreModel} from './root-store'
 
-export type FollowerItem = GetFollowers.Follower & {
-  _reactKey: string
-}
+const PAGE_SIZE = 30
+
+export type FollowerItem = GetFollowers.Follower
 
 export class UserFollowersViewModel {
   // state
@@ -16,6 +16,9 @@ export class UserFollowersViewModel {
   hasLoaded = false
   error = ''
   params: GetFollowers.QueryParams
+  hasMore = true
+  loadMoreCursor?: string
+  private _loadMorePromise: Promise<void> | undefined
 
   // data
   subject: ActorRef.WithInfo = {
@@ -55,16 +58,17 @@ export class UserFollowersViewModel {
   // 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 UserFollowersViewModel {
   // 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.getFollowers(
-        this.params,
-      )
-      this._replaceAll(res)
+      const params = Object.assign({}, this.params, {
+        limit: PAGE_SIZE,
+        before: this.loadMoreCursor,
+      })
+      if (this.isRefreshing) {
+        this.followers = []
+      }
+      const res = await this.rootStore.api.app.bsky.graph.getFollowers(params)
+      await this._appendAll(res)
       this._xIdle()
     } catch (e: any) {
-      this._xIdle(`Failed to load feed: ${e.toString()}`)
-    }
-  }
-
-  private _replaceAll(res: GetFollowers.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.followers.length = 0
-    let counter = 0
-    for (const item of res.data.followers) {
-      this._append({_reactKey: `item-${counter++}`, ...item})
+      this._xIdle(e)
     }
   }
 
-  private _append(item: FollowerItem) {
-    this.followers.push(item)
+  private async _appendAll(res: GetFollowers.Response) {
+    this.loadMoreCursor = res.data.cursor
+    this.hasMore = !!this.loadMoreCursor
+    this.followers = this.followers.concat(res.data.followers)
   }
 }