forked from DA0-DA0/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
useEntity.ts
66 lines (60 loc) · 1.91 KB
/
useEntity.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
import { constSelector, useRecoilValue } from 'recoil'
import { DaoCoreV2Selectors } from '@dao-dao/state'
import { useCachedLoadable } from '@dao-dao/stateless'
import { Entity, EntityType, LoadingData, WithChainId } from '@dao-dao/types'
import {
CHAIN_BECH32_PREFIX,
getFallbackImage,
isValidContractAddress,
isValidWalletAddress,
} from '@dao-dao/utils'
import { walletProfileDataSelector } from '../recoil'
export type UseEntityOptions = WithChainId<{
address: string
}>
export const useEntity = ({
address,
chainId,
}: UseEntityOptions): LoadingData<Entity> => {
// Try to load config assuming the address is a DAO.
const daoConfig = useCachedLoadable(
address && isValidContractAddress(address, CHAIN_BECH32_PREFIX)
? DaoCoreV2Selectors.configSelector({
contractAddress: address,
chainId,
params: [],
})
: undefined
)
const walletProfileData = useRecoilValue(
address && isValidWalletAddress(address, CHAIN_BECH32_PREFIX)
? walletProfileDataSelector({
address,
chainId,
})
: constSelector(undefined)
)
return daoConfig.state !== 'hasValue' &&
(!walletProfileData || walletProfileData.loading)
? { loading: true }
: {
loading: false,
data: {
type:
daoConfig.state === 'hasValue' ? EntityType.Dao : EntityType.Wallet,
address,
name:
daoConfig.state === 'hasValue'
? daoConfig.contents.name
: walletProfileData && !walletProfileData.loading
? walletProfileData.profile.name
: null,
imageUrl:
(daoConfig.state === 'hasValue'
? daoConfig.contents.image_url
: walletProfileData && !walletProfileData.loading
? walletProfileData.profile.imageUrl
: undefined) || getFallbackImage(address),
},
}
}