about summary refs log tree commit diff
path: root/src/lib/hooks/useFollowDid.ts
diff options
context:
space:
mode:
authorEric Bailey <git@esb.lol>2023-09-20 21:16:11 -0500
committerGitHub <noreply@github.com>2023-09-20 19:16:11 -0700
commit6df1bcad31a4e58a7cdade6d19e75bd7c0b5ee9a (patch)
tree03e6167067c4ed45cf78d0e0101196bffe840f30 /src/lib/hooks/useFollowDid.ts
parent498c3e2c271524f7bac08df9d6cc4d72174a4521 (diff)
downloadvoidsky-6df1bcad31a4e58a7cdade6d19e75bd7c0b5ee9a.tar.zst
add suggested follow section to profile header (#1481)
* add suggested follow section to profile header

* fix button overflow

* don't even render on preview

* fix useFollowDid and FollowButton race condition

* add section header, close button, active state

* lighten icon
Diffstat (limited to 'src/lib/hooks/useFollowDid.ts')
-rw-r--r--src/lib/hooks/useFollowDid.ts46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/lib/hooks/useFollowDid.ts b/src/lib/hooks/useFollowDid.ts
new file mode 100644
index 000000000..223adb047
--- /dev/null
+++ b/src/lib/hooks/useFollowDid.ts
@@ -0,0 +1,46 @@
+import React from 'react'
+
+import {useStores} from 'state/index'
+import {FollowState} from 'state/models/cache/my-follows'
+
+export function useFollowDid({did}: {did: string}) {
+  const store = useStores()
+  const state = store.me.follows.getFollowState(did)
+
+  return {
+    state,
+    following: state === FollowState.Following,
+    toggle: React.useCallback(async () => {
+      if (state === FollowState.Following) {
+        try {
+          await store.agent.deleteFollow(store.me.follows.getFollowUri(did))
+          store.me.follows.removeFollow(did)
+          return {
+            state: FollowState.NotFollowing,
+            following: false,
+          }
+        } catch (e: any) {
+          store.log.error('Failed to delete follow', e)
+          throw e
+        }
+      } else if (state === FollowState.NotFollowing) {
+        try {
+          const res = await store.agent.follow(did)
+          store.me.follows.addFollow(did, res.uri)
+          return {
+            state: FollowState.Following,
+            following: true,
+          }
+        } catch (e: any) {
+          store.log.error('Failed to create follow', e)
+          throw e
+        }
+      }
+
+      return {
+        state: FollowState.Unknown,
+        following: false,
+      }
+    }, [store, did, state]),
+  }
+}