-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathcheck-domain.js
More file actions
76 lines (65 loc) · 1.74 KB
/
check-domain.js
File metadata and controls
76 lines (65 loc) · 1.74 KB
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
import { useState, useEffect, useMemo } from 'react'
import { isEnsDomainAvailable } from './aragonjs-wrapper'
import { useClientWeb3 } from './contexts/ClientWeb3Context'
import { useWallet } from './contexts/wallet'
const DOMAIN_CHECK = Symbol('DOMAIN_CHECK')
const DOMAIN_LOADING = Symbol('DOMAIN_LOADING')
const DOMAIN_ERROR = Symbol('DOMAIN_ERROR')
const DOMAIN_NONE = Symbol('DOMAIN_NONE')
function completeDomain(domain) {
return domain.endsWith('.eth') ? domain : `${domain}.aragonid.eth`
}
function useCheckDomain(domain, invertCheck = false) {
const [exists, setExists] = useState(false)
const [loading, setLoading] = useState(true)
const { networkType } = useWallet()
const { web3 } = useClientWeb3()
useEffect(() => {
setExists(false)
setLoading(true)
let cancelled = false
const check = async () => {
try {
const available = await isEnsDomainAvailable(
networkType,
web3,
completeDomain(domain)
)
if (!cancelled) {
setExists(available)
setLoading(false)
}
} catch (err) {
// retry every second
setTimeout(check, 1000)
}
}
// Only start checking after 300ms
setTimeout(() => {
if (!cancelled) {
check()
}
}, 300)
return () => {
cancelled = true
}
}, [domain, web3, networkType])
const domainStatus = useMemo(() => {
if (!domain) {
return DOMAIN_NONE
}
if (loading) {
return DOMAIN_LOADING
}
return invertCheck === exists ? DOMAIN_CHECK : DOMAIN_ERROR
}, [domain, exists, invertCheck, loading])
return domainStatus
}
export {
DOMAIN_CHECK,
DOMAIN_ERROR,
DOMAIN_LOADING,
DOMAIN_NONE,
useCheckDomain,
completeDomain,
}