Bluesky app fork with some witchin' additions 馃挮
witchsky.app
bluesky
fork
client
1import {useCallback} from 'react'
2import {msg} from '@lingui/core/macro'
3import {useLingui} from '@lingui/react'
4
5import {logger} from '#/logger'
6import {type Shadow} from '#/state/cache/types'
7import {useProfileFollowMutationQueue} from '#/state/queries/profile'
8import {useRequireAuth} from '#/state/session'
9import * as Toast from '#/components/Toast'
10import {type Metrics} from '#/analytics/metrics'
11import type * as bsky from '#/types/bsky'
12
13export function useFollowMethods({
14 profile,
15 logContext,
16}: {
17 profile: Shadow<bsky.profile.AnyProfileView>
18 logContext: Metrics['profile:follow']['logContext'] &
19 Metrics['profile:unfollow']['logContext']
20}) {
21 const {_} = useLingui()
22 const requireAuth = useRequireAuth()
23 const [queueFollow, queueUnfollow] = useProfileFollowMutationQueue(
24 profile,
25 logContext,
26 )
27
28 const follow = useCallback(() => {
29 requireAuth(async () => {
30 try {
31 await queueFollow()
32 } catch (e: any) {
33 logger.error(`useFollowMethods: failed to follow`, {message: String(e)})
34 if (e?.name !== 'AbortError') {
35 Toast.show(_(msg`An issue occurred, please try again.`), {
36 type: 'error',
37 })
38 }
39 }
40 })
41 }, [_, queueFollow, requireAuth])
42
43 const unfollow = useCallback(() => {
44 requireAuth(async () => {
45 try {
46 await queueUnfollow()
47 } catch (e: any) {
48 logger.error(`useFollowMethods: failed to unfollow`, {
49 message: String(e),
50 })
51 if (e?.name !== 'AbortError') {
52 Toast.show(_(msg`An issue occurred, please try again.`), {
53 type: 'error',
54 })
55 }
56 }
57 })
58 }, [_, queueUnfollow, requireAuth])
59
60 return {
61 follow,
62 unfollow,
63 }
64}