forked from DA0-DA0/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EntityDisplay.tsx
147 lines (135 loc) · 4.12 KB
/
EntityDisplay.tsx
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
/* eslint-disable i18next/no-literal-string */
import { fromBech32 } from '@cosmjs/encoding'
import { Check, CopyAllOutlined } from '@mui/icons-material'
import clsx from 'clsx'
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { EntityDisplayProps, EntityType } from '@dao-dao/types'
import {
concatAddressStartEnd,
getFallbackImage,
toAccessibleImageUrl,
} from '@dao-dao/utils'
import { useChainContext, useDaoNavHelpers, useDetectTruncate } from '../hooks'
import { ButtonLink } from './buttons'
import { IconButton } from './icon_buttons'
import { Tooltip } from './tooltip/Tooltip'
export const EntityDisplay = ({
address,
loadingEntity,
imageSize,
hideImage,
size = 'default',
className,
textClassName,
noCopy,
noUnderline,
showFullAddress,
noLink,
openInNewTab,
}: EntityDisplayProps) => {
const { t } = useTranslation()
const { getDaoPath } = useDaoNavHelpers()
const { config } = useChainContext()
imageSize ??= size === 'lg' ? 28 : 24
const [copied, setCopied] = useState(false)
// Unset copied after 2 seconds.
useEffect(() => {
const timeout = setTimeout(() => setCopied(false), 2000)
// Cleanup on unmount.
return () => clearTimeout(timeout)
}, [copied])
const href = loadingEntity.loading
? undefined
: loadingEntity.data.type === EntityType.Dao
? getDaoPath(loadingEntity.data.address)
: loadingEntity.data.type === EntityType.Wallet
? config?.explorerUrlTemplates.wallet.replace(
'REPLACE',
loadingEntity.data.address
)
: undefined
const { textRef, truncated } = useDetectTruncate()
// Use bech32 prefix length to determine how much to truncate from beginning.
let prefixLength
try {
prefixLength = fromBech32(address).prefix.length
} catch (e) {
// Conservative estimate.
prefixLength = 6
}
// If name exists, use it. Otherwise, fallback to address, potentially
// truncated.
const textDisplay =
!loadingEntity.loading && loadingEntity.data.name
? loadingEntity.data.type === EntityType.Module
? t('title.chainModule.' + loadingEntity.data.name)
: loadingEntity.data.name
: showFullAddress
? address
: concatAddressStartEnd(address, prefixLength + 3, 3)
return (
<div
className={clsx('flex min-w-0 flex-row items-center gap-2', className)}
>
<Tooltip
title={
// Show text display tooltip if text is truncated.
truncated ? textDisplay : undefined
}
>
<ButtonLink
className={clsx(loadingEntity.loading && 'animate-pulse')}
containerClassName="min-w-0"
href={noLink ? undefined : href}
onClick={(e) => !noLink && e.stopPropagation()}
openInNewTab={openInNewTab}
variant={noUnderline || noLink || !href ? 'none' : 'underline'}
>
{!hideImage && (
<div
className="shrink-0 rounded-full bg-cover bg-center"
style={{
backgroundImage: `url(${
loadingEntity.loading
? getFallbackImage(address)
: toAccessibleImageUrl(loadingEntity.data.imageUrl)
})`,
width: imageSize,
height: imageSize,
}}
></div>
)}
<p
className={clsx(
'min-w-0 truncate',
{
'text-sm': size === 'default',
'text-lg': size === 'lg',
},
textClassName
)}
ref={textRef}
>
{textDisplay}
</p>
</ButtonLink>
</Tooltip>
{!noCopy && (
<Tooltip title={t('button.copyAddress')}>
<IconButton
Icon={copied ? Check : CopyAllOutlined}
iconClassName="text-icon-tertiary"
onClick={(e) => {
e.stopPropagation()
navigator.clipboard.writeText(address)
setCopied(true)
}}
size="xs"
variant="ghost"
/>
</Tooltip>
)}
</div>
)
}