forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import React from 'react'
2import {msg} from '@lingui/macro'
3import {useLingui} from '@lingui/react'
4
5import {type LogEvents} from '#/lib/statsig/statsig'
6import {logger} from '#/logger'
7import {type Shadow} from '#/state/cache/types'
8import {useProfileFollowMutationQueue} from '#/state/queries/profile'
9import {useRequireAuth} from '#/state/session'
10import * as Toast from '#/view/com/util/Toast'
11import type * as bsky from '#/types/bsky'
12
13export function useFollowMethods({
14 profile,
15 logContext,
16}: {
17 profile: Shadow<bsky.profile.AnyProfileView>
18 logContext: LogEvents['profile:follow']['logContext'] &
19 LogEvents['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 = React.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.`), 'xmark')
36 }
37 }
38 })
39 }, [_, queueFollow, requireAuth])
40
41 const unfollow = React.useCallback(() => {
42 requireAuth(async () => {
43 try {
44 await queueUnfollow()
45 } catch (e: any) {
46 logger.error(`useFollowMethods: failed to unfollow`, {
47 message: String(e),
48 })
49 if (e?.name !== 'AbortError') {
50 Toast.show(_(msg`An issue occurred, please try again.`), 'xmark')
51 }
52 }
53 })
54 }, [_, queueUnfollow, requireAuth])
55
56 return {
57 follow,
58 unfollow,
59 }
60}