forked from DA0-DA0/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
useFollowingDaos.ts
177 lines (155 loc) · 4.8 KB
/
useFollowingDaos.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import uniq from 'lodash.uniq'
import { useCallback, useState } from 'react'
import toast from 'react-hot-toast'
import { useTranslation } from 'react-i18next'
import { useSetRecoilState } from 'recoil'
import { refreshFollowingDaosAtom } from '@dao-dao/state'
import { useCachedLoading } from '@dao-dao/stateless'
import { LoadingData } from '@dao-dao/types'
import {
FOLLOWING_DAOS_PREFIX,
KVPK_API_BASE,
processError,
} from '@dao-dao/utils'
import {
followingDaosSelector,
temporaryFollowingDaosAtom,
} from '../recoil/selectors/dao/following'
import { useCfWorkerAuthPostRequest } from './useCfWorkerAuthPostRequest'
import { useWallet } from './useWallet'
export type UseFollowingDaosReturn = {
daos: LoadingData<string[]>
refreshFollowing: () => void
isFollowing: (coreAddress: string) => any
setFollowing: (coreAddressOrAddresses: string | string[]) => Promise<boolean>
setUnfollowing: (
coreAddressOrAddresses: string | string[]
) => Promise<boolean>
updatingFollowing: boolean
ready: boolean
}
export const useFollowingDaos = (chainId: string): UseFollowingDaosReturn => {
const { t } = useTranslation()
const { isWalletConnected, isWalletConnecting, hexPublicKey } = useWallet({
chainId,
loadAccount: true,
})
// Following API doesn't update right away, so this serves to keep track of
// all successful updates for the current session. This will be reset on page
// refresh.
const setTemporary = useSetRecoilState(
temporaryFollowingDaosAtom(hexPublicKey.loading ? '' : hexPublicKey.data)
)
const followingDaosLoading = useCachedLoading(
!hexPublicKey.loading
? followingDaosSelector({
chainId,
walletPublicKey: hexPublicKey.data,
})
: undefined,
[]
)
const setRefreshFollowingDaos = useSetRecoilState(refreshFollowingDaosAtom)
const refreshFollowing = useCallback(
() => setRefreshFollowingDaos((id) => id + 1),
[setRefreshFollowingDaos]
)
const isFollowing = useCallback(
(coreAddress: string) =>
!followingDaosLoading.loading &&
followingDaosLoading.data.includes(coreAddress),
[followingDaosLoading]
)
const [updating, setUpdating] = useState(false)
const { ready, postRequest } = useCfWorkerAuthPostRequest(
KVPK_API_BASE,
'Update Following'
)
const setFollowing = useCallback(
async (coreAddressOrAddresses: string | string[]) => {
if (!ready) {
toast.error(t('error.logInToFollow'))
return false
}
if (updating) {
return false
}
setUpdating(true)
try {
const daos = [coreAddressOrAddresses].flat()
await postRequest('/setMany', {
data: daos.map((coreAddress) => ({
key: FOLLOWING_DAOS_PREFIX + `${chainId}:${coreAddress}`,
value: 1,
})),
})
setTemporary((prev) => ({
following: uniq([...prev.following, ...daos]),
unfollowing: prev.unfollowing.filter(
(address) => !daos.includes(address)
),
}))
refreshFollowing()
return true
} catch (err) {
console.error(err)
toast.error(processError(err))
return false
} finally {
setUpdating(false)
}
},
[chainId, postRequest, ready, refreshFollowing, setTemporary, t, updating]
)
const setUnfollowing = useCallback(
async (coreAddressOrAddresses: string | string[]) => {
if (!ready) {
toast.error(t('error.logInToFollow'))
return false
}
if (updating) {
return false
}
setUpdating(true)
try {
const daos = [coreAddressOrAddresses].flat()
await postRequest('/setMany', {
data: daos.map((coreAddress) => ({
key: FOLLOWING_DAOS_PREFIX + `${chainId}:${coreAddress}`,
value: null,
})),
})
setTemporary((prev) => ({
following: prev.following.filter(
(address) => !daos.includes(address)
),
unfollowing: uniq([...prev.unfollowing, ...daos]),
}))
refreshFollowing()
return true
} catch (err) {
console.error(err)
toast.error(processError(err))
return false
} finally {
setUpdating(false)
}
},
[chainId, postRequest, ready, refreshFollowing, setTemporary, t, updating]
)
return {
daos: followingDaosLoading,
refreshFollowing,
isFollowing,
setFollowing,
setUnfollowing,
updatingFollowing:
// If wallet connecting, following is not yet loaded.
isWalletConnecting ||
// Updating if wallet connected and following is loading or update is in
// progress or hex public key not yet loaded.
(isWalletConnected &&
(!hexPublicKey || followingDaosLoading.loading || updating)),
ready,
}
}