about summary refs log tree commit diff
path: root/src/state/models/feeds/algo/saved.ts
diff options
context:
space:
mode:
authorAnsh Nanda <anshnanda10@gmail.com>2023-05-16 23:38:34 -0700
committerAnsh Nanda <anshnanda10@gmail.com>2023-05-16 23:38:34 -0700
commit3501fda015051848bb27e26360b57ade6af3407f (patch)
treeb16009c28899ae6bf69e7bc9f9a7b1b2eafaad54 /src/state/models/feeds/algo/saved.ts
parent99d66679b3fdba70d58142584e49b08537d9297b (diff)
downloadvoidsky-3501fda015051848bb27e26360b57ade6af3407f.tar.zst
allow changing pinned feed order on web
Diffstat (limited to 'src/state/models/feeds/algo/saved.ts')
-rw-r--r--src/state/models/feeds/algo/saved.ts39
1 files changed, 38 insertions, 1 deletions
diff --git a/src/state/models/feeds/algo/saved.ts b/src/state/models/feeds/algo/saved.ts
index 97d75820d..cb2015ccb 100644
--- a/src/state/models/feeds/algo/saved.ts
+++ b/src/state/models/feeds/algo/saved.ts
@@ -1,4 +1,4 @@
-import {makeAutoObservable} from 'mobx'
+import {makeAutoObservable, runInAction} from 'mobx'
 import {AppBskyFeedGetSavedFeeds as GetSavedFeeds} from '@atproto/api'
 import {RootStoreModel} from '../../root-store'
 import {bundleAsync} from 'lib/async/bundle'
@@ -103,6 +103,43 @@ export class SavedFeedsModel {
     return this.pinned.find(f => f.data.uri === feed.data.uri) ? true : false
   }
 
+  movePinnedItem(item: AlgoItemModel, direction: 'up' | 'down') {
+    if (this.pinned.length < 2) {
+      throw new Error('Array must have at least 2 items')
+    }
+    const index = this.pinned.indexOf(item)
+    if (index === -1) {
+      throw new Error('Item not found in array')
+    }
+
+    const len = this.pinned.length
+
+    runInAction(() => {
+      if (direction === 'up') {
+        if (index === 0) {
+          // Remove the item from the first place and put it at the end
+          this.pinned.push(this.pinned.shift()!)
+        } else {
+          // Swap the item with the one before it
+          const temp = this.pinned[index]
+          this.pinned[index] = this.pinned[index - 1]
+          this.pinned[index - 1] = temp
+        }
+      } else if (direction === 'down') {
+        if (index === len - 1) {
+          // Remove the item from the last place and put it at the start
+          this.pinned.unshift(this.pinned.pop()!)
+        } else {
+          // Swap the item with the one after it
+          const temp = this.pinned[index]
+          this.pinned[index] = this.pinned[index + 1]
+          this.pinned[index + 1] = temp
+        }
+      }
+      // this.pinned = [...this.pinned]
+    })
+  }
+
   // public api
   // =