|
| 1 | +import React, { useEffect, useState } from 'react' |
| 2 | +import styled from '@emotion/styled' |
| 3 | +import { useQuery } from 'react-apollo' |
| 4 | + |
| 5 | +import { GET_DOMAINS_OWNED_BY_ADDRESS_FROM_SUBGRAPH } from '../../graphql/queries' |
| 6 | +import DomainItem from '../DomainItem/ChildDomainItem' |
| 7 | +import { decryptName } from '../../api/labels' |
| 8 | +import AddressContainer from '../Basic/MainContainer' |
| 9 | +import DefaultTopBar from '../Basic/TopBar' |
| 10 | +import { Title } from '../Typography/Basic' |
| 11 | +import { ExternalButtonLink as DefaultExternalButtonLink } from '../Forms/Button' |
| 12 | +import { getEtherScanAddr } from '../../utils/utils' |
| 13 | +import Loader from '../Loader' |
| 14 | + |
| 15 | +const NoDomainsContainer = styled('div')` |
| 16 | + display: flex; |
| 17 | + padding: 40px; |
| 18 | + flex-direction: column; |
| 19 | + justify-content: center; |
| 20 | + align-items: center; |
| 21 | + background: white; |
| 22 | + box-shadow: 3px 4px 6px 0 rgba(229, 236, 241, 0.3); |
| 23 | + border-radius: 6px; |
| 24 | + margin-bottom: 40px; |
| 25 | +
|
| 26 | + h2 { |
| 27 | + color: #adbbcd; |
| 28 | + font-weight: 100; |
| 29 | + margin-bottom: 0; |
| 30 | + padding: 0; |
| 31 | + margin-top: 20px; |
| 32 | + text-align: center; |
| 33 | + max-width: 500px; |
| 34 | + } |
| 35 | +
|
| 36 | + p { |
| 37 | + color: #2b2b2b; |
| 38 | + font-size: 18px; |
| 39 | + font-weight: 300; |
| 40 | + margin-top: 20px; |
| 41 | + line-height: 1.3em; |
| 42 | + text-align: center; |
| 43 | + max-width: 400px; |
| 44 | + } |
| 45 | +` |
| 46 | + |
| 47 | +const TopBar = styled(DefaultTopBar)` |
| 48 | + margin-bottom: 40px; |
| 49 | +` |
| 50 | + |
| 51 | +const ExternalButtonLink = styled(DefaultExternalButtonLink)` |
| 52 | + margin-left: 40px; |
| 53 | +` |
| 54 | + |
| 55 | +const DomainsContainer = styled('div')` |
| 56 | + margin-top: 20px; |
| 57 | + padding-bottom: 30px; |
| 58 | + padding-left: 40px; |
| 59 | + padding-right: 40px; |
| 60 | +` |
| 61 | + |
| 62 | +function hasNoDomains(data) { |
| 63 | + return ( |
| 64 | + (data.account && |
| 65 | + data.account.domains && |
| 66 | + data.account.domains.length === 0) || |
| 67 | + data.account === null |
| 68 | + ) |
| 69 | +} |
| 70 | + |
| 71 | +function filterOutReverse(domains) { |
| 72 | + return domains.filter(domain => domain.parent.name !== 'addr.reverse') |
| 73 | +} |
| 74 | + |
| 75 | +function DomainList({ domains, address }) { |
| 76 | + const { loading, data, error } = useQuery( |
| 77 | + GET_DOMAINS_OWNED_BY_ADDRESS_FROM_SUBGRAPH, |
| 78 | + { variables: { id: address } } |
| 79 | + ) |
| 80 | + |
| 81 | + if (error) { |
| 82 | + return 'Error getting domains' |
| 83 | + } |
| 84 | + |
| 85 | + if (loading) { |
| 86 | + return <Loader withWrap large /> |
| 87 | + } |
| 88 | + |
| 89 | + if (hasNoDomains(data)) { |
| 90 | + return ( |
| 91 | + <NoDomainsContainer> |
| 92 | + <h2>This address does not own any domains</h2> |
| 93 | + </NoDomainsContainer> |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + return ( |
| 98 | + <DomainsContainer> |
| 99 | + {filterOutReverse(data.account.domains).map(domain => ( |
| 100 | + <DomainItem name={decryptName(domain.name)} owner={address} /> |
| 101 | + ))} |
| 102 | + </DomainsContainer> |
| 103 | + ) |
| 104 | +} |
| 105 | + |
| 106 | +export default function Address({ address }) { |
| 107 | + let [etherScanAddr, setEtherScanAddr] = useState(null) |
| 108 | + |
| 109 | + useEffect(() => { |
| 110 | + getEtherScanAddr().then(setEtherScanAddr) |
| 111 | + }, []) |
| 112 | + |
| 113 | + return ( |
| 114 | + <AddressContainer> |
| 115 | + <TopBar> |
| 116 | + <Title>{address}</Title> |
| 117 | + </TopBar> |
| 118 | + {etherScanAddr && ( |
| 119 | + <ExternalButtonLink |
| 120 | + type="primary" |
| 121 | + target="_blank" |
| 122 | + href={`${etherScanAddr}/address/${address}`} |
| 123 | + > |
| 124 | + View on EtherScan |
| 125 | + </ExternalButtonLink> |
| 126 | + )} |
| 127 | + <DomainList address={address} /> |
| 128 | + </AddressContainer> |
| 129 | + ) |
| 130 | +} |
0 commit comments