-
Notifications
You must be signed in to change notification settings - Fork 22
/
useLoadingDaos.ts
109 lines (100 loc) · 2.91 KB
/
useLoadingDaos.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
import { useQueries, useQueryClient } from '@tanstack/react-query'
import { constSelector, useRecoilValueLoadable, waitForAll } from 'recoil'
import { daoQueries } from '@dao-dao/state/query'
import { followingDaosSelector } from '@dao-dao/state/recoil'
import {
DaoInfo,
DaoSource,
LoadingData,
StatefulDaoCardProps,
} from '@dao-dao/types'
import { makeCombineQueryResultsIntoLoadingData } from '@dao-dao/utils'
import { daoQueries as statefulDaoQueries } from '../queries/dao'
import { useQueryLoadingData } from './query/useQueryLoadingData'
import { useProfile } from './useProfile'
export const useLoadingDaos = (
daos: LoadingData<DaoSource[]>,
alphabetize = false
): LoadingData<DaoInfo[]> => {
const queryClient = useQueryClient()
return useQueries({
queries: daos.loading
? []
: daos.data.map(({ chainId, coreAddress }) =>
statefulDaoQueries.info(queryClient, {
chainId,
coreAddress,
})
),
combine: makeCombineQueryResultsIntoLoadingData<DaoInfo>({
transform: (infos) =>
infos.sort((a, b) => (alphabetize ? a.name.localeCompare(b.name) : 0)),
}),
})
}
export const useLoadingFeaturedDaoCards = (
/**
* If passed, only load DAOs from this chain. Otherwise, load from all chains.
*/
chainId?: string
): LoadingData<StatefulDaoCardProps[]> => {
const featuredDaos = useQueryLoadingData(
daoQueries.listFeatured(),
[] as DaoSource[]
)
const daos = useLoadingDaos(
featuredDaos.loading
? { loading: true }
: !featuredDaos.data
? { loading: false, data: [] }
: {
loading: false,
data: featuredDaos.data.filter(
(featured) => !chainId || featured.chainId === chainId
),
}
)
return daos.loading
? daos
: {
loading: false,
updating: daos.updating,
data: daos.data.map(
(info): StatefulDaoCardProps => ({
info,
})
),
}
}
export const useLoadingFollowingDaos = (
// If passed, will only load DAOs from this chain. Otherwise, will load from
// all chains.
chainId?: string
): LoadingData<DaoInfo[]> => {
const { uniquePublicKeys } = useProfile()
const followingDaosLoading = useRecoilValueLoadable(
!uniquePublicKeys.loading
? waitForAll(
uniquePublicKeys.data.map(({ publicKey }) =>
followingDaosSelector({
walletPublicKey: publicKey,
})
)
)
: constSelector([])
)
return useLoadingDaos(
followingDaosLoading.state === 'loading'
? { loading: true }
: followingDaosLoading.state === 'hasError'
? { loading: false, data: [] }
: {
loading: false,
data: followingDaosLoading.contents.flatMap((following) =>
following.filter((f) => !chainId || f.chainId === chainId)
),
},
// Alphabetize.
true
)
}