-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathProfileButton.tsx
More file actions
482 lines (446 loc) · 13.9 KB
/
ProfileButton.tsx
File metadata and controls
482 lines (446 loc) · 13.9 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import { useMemo } from 'react'
import { useTranslation } from 'react-i18next'
import { useCopyToClipboard } from 'react-use'
import styled, { css } from 'styled-components'
import { Address, isAddress } from 'viem'
import { useChainId } from 'wagmi'
import { getProtocolType } from '@ensdomains/ensjs/utils'
import {
CopySVG,
Dropdown,
RecordItem,
Typography,
UpRightArrowSVG,
VerticalDotsSVG,
} from '@ensdomains/thorin'
import { DropdownItem } from '@ensdomains/thorin/dist/types/components/molecules/Dropdown/Dropdown'
import { DynamicAddressIcon } from '@app/assets/address/DynamicAddressIcon'
import { dynamicAddressIcons } from '@app/assets/address/dynamicAddressIcons'
import { DynamicSocialIcon, socialIconTypes } from '@app/assets/social/DynamicSocialIcon'
import { DynamicVerificationIcon } from '@app/assets/verification/DynamicVerificationIcon'
import { VerificationBadgeAccountTooltipContent } from '@app/components/@molecules/VerificationBadge/components/VerificationBadgeAccountTooltipContent'
import { VerificationBadgeVerifierTooltipContent } from '@app/components/@molecules/VerificationBadge/components/VerificationBadgeVerifierTooltipContent'
import { VerificationBadge } from '@app/components/@molecules/VerificationBadge/VerificationBadge'
import { useCoinChain } from '@app/hooks/chain/useCoinChain'
import { usePrimaryName } from '@app/hooks/ensjs/public/usePrimaryName'
import { getDestination } from '@app/routes'
import { VerificationProtocol } from '@app/transaction-flow/input/VerifyProfile/VerifyProfile-flow'
import { useBreakpoint } from '@app/utils/BreakpointProvider'
import { getContentHashLink } from '@app/utils/contenthash'
import { getSocialData } from '@app/utils/getSocialData'
import { makeEtherscanLink, shortenAddress } from '@app/utils/utils'
import { getVerifierData } from '@app/utils/verification/getVerifierData'
import { isVerificationProtocol } from '@app/utils/verification/isVerificationProtocol'
import { isDeceptiveUrl } from '@app/utils/security/validateUrl'
const StyledAddressIcon = styled(DynamicAddressIcon)(
({ theme }) => css`
width: ${theme.space['5']};
height: ${theme.space['5']};
`,
)
// Icon components defined outside of parent components
const UpRightArrowIcon = () => <UpRightArrowSVG height={16} width={16} />
const CopyIcon = () => <CopySVG height={16} width={16} />
// Icon component for social icons with props
const SocialIcon = ({ color, icon }: { color?: string; icon: keyof typeof socialIconTypes }) => (
<DynamicSocialIcon height={20} width={20} fill={color} name={icon} />
)
// Icon component for styled address icon
const StyledAddressIconComponent = ({ name }: { name: string }) => <StyledAddressIcon name={name} />
// Icon component for verification icon
const VerificationIcon = ({
name,
}: {
name: Parameters<typeof DynamicVerificationIcon>[0]['name']
}) => <DynamicVerificationIcon name={name} />
export const SocialProfileButton = ({
iconKey,
value,
normalisedKey,
isVerified,
verifiers,
}: {
iconKey: string
normalisedKey: string
value: string
isVerified?: boolean
verifiers?: VerificationProtocol[]
}) => {
const breakpoints = useBreakpoint()
const socialData = getSocialData(normalisedKey, value)
const IconComponent = useMemo(
() => () =>
socialData ? (
<SocialIcon
color={socialData.color}
icon={socialData.icon as keyof typeof socialIconTypes}
/>
) : null,
[socialData],
)
if (!socialData) return null
// Check if the URL is deceptive
const isDeceptive = socialData.type === 'link' && isDeceptiveUrl(socialData.urlFormatter)
return (
<VerificationBadge
isVerified={isVerified}
showBadge={isVerified}
type="record"
tooltipContent={<VerificationBadgeAccountTooltipContent verifiers={verifiers} />}
>
<RecordItem
icon={IconComponent}
size={breakpoints.sm ? 'large' : 'small'}
inline
data-testid={`social-profile-button-${iconKey}`}
value={socialData.value}
{...(socialData.type === 'link' && !isDeceptive
? { as: 'a' as const, link: socialData.urlFormatter }
: { as: 'button' as const })}
style={isDeceptive ? {
textDecoration: 'line-through',
opacity: 0.5,
cursor: 'not-allowed'
} : {}}
title={isDeceptive ? 'This URL contains deceptive patterns and has been disabled for security' : undefined}
>
{socialData.value}
</RecordItem>
</VerificationBadge>
)
}
export const AddressProfileButton = ({
iconKey: _iconKey,
value: address,
}: {
iconKey: string
value: string
}) => {
const breakpoints = useBreakpoint()
const iconKey = _iconKey.toLowerCase()
const [, copy] = useCopyToClipboard()
const coinChainResults = useCoinChain({ coinName: iconKey })
const { data } = coinChainResults
const defaultBlockExplorer = data?.blockExplorers?.default
const IconComponent = useMemo(
() => () => <StyledAddressIconComponent name={iconKey} />,
[iconKey],
)
const items = [
iconKey === 'eth'
? {
icon: UpRightArrowIcon,
label: 'View address',
href: getDestination(`/${address}`) as string,
}
: undefined,
{
icon: CopyIcon,
label: 'Copy address',
onClick: () => copy(address),
},
defaultBlockExplorer
? {
icon: UpRightArrowIcon,
label: `View on ${defaultBlockExplorer?.name}`,
href: `${defaultBlockExplorer?.url}/address/${address}`,
}
: undefined,
].filter((item) => item !== undefined) as DropdownItem[]
return iconKey in dynamicAddressIcons ? (
<Dropdown width={200} items={items} direction="up">
<RecordItem
data-testid={`address-profile-button-${iconKey}`}
postfixIcon={VerticalDotsSVG}
icon={IconComponent}
value={address}
size={breakpoints.sm ? 'large' : 'small'}
inline
>
{shortenAddress(
address,
undefined,
breakpoints.sm ? undefined : 4,
breakpoints.sm ? undefined : 3,
)}
</RecordItem>
</Dropdown>
) : null
}
const OtherContainer = styled.div(
({ theme }) => css`
background-color: ${theme.colors.greyPrimary};
padding: 0 ${theme.space['1.25']};
border-radius: ${theme.radii.checkbox};
height: ${theme.space['5']};
display: flex;
align-items: center;
`,
)
const OtherContainerAddressPrefix = styled(Typography)(
({ theme }) => css`
color: ${theme.colors.backgroundPrimary};
font-size: ${theme.fontSizes.extraSmall};
`,
)
const OtherContainerTextPrefix = styled(Typography)(
({ theme }) => css`
padding-left: ${theme.space['0.5']};
`,
)
export const OtherProfileButton = ({
iconKey,
value,
type = 'text',
name,
}: {
iconKey: string
value: string
type?: 'text' | 'address' | 'contenthash'
name: string
}) => {
const chainId = useChainId()
const breakpoints = useBreakpoint()
const isLink =
value?.startsWith('http://') || value?.startsWith('https://') || type === 'contenthash'
const formattedValue = useMemo(() => {
if (breakpoints.sm) {
if (type === 'address') {
return shortenAddress(value)
}
return value?.length > 15 ? `${value.slice(0, 15)}...` : value
}
return value?.length > 5 ? `${value.slice(0, 5)}...` : value
}, [type, value, breakpoints])
const linkProps = useMemo(() => {
if (!isLink) return {}
if (type === 'contenthash') {
const decodedContentHash = getProtocolType(value)
if (!decodedContentHash) return {}
const _link = getContentHashLink({ name, chainId, decodedContentHash })
if (!_link) return {}
// Check if contenthash link is deceptive
if (isDeceptiveUrl(_link)) return {}
return {
as: 'a',
link: _link,
} as const
}
// Check if regular URL is deceptive
if (isDeceptiveUrl(value)) return {}
return {
as: 'a',
link: value,
} as const
}, [isLink, type, value, name, chainId])
// Determine if the URL is deceptive for styling
const isDeceptive = isLink && !linkProps.link && !linkProps.as
return (
<RecordItem
{...linkProps}
value={value}
inline
size={breakpoints.sm ? 'large' : 'small'}
keyLabel={
type === 'address' ? (
<OtherContainer>
<OtherContainerAddressPrefix fontVariant="extraSmall">
{iconKey}
</OtherContainerAddressPrefix>
</OtherContainer>
) : (
<OtherContainerTextPrefix color="grey">{iconKey}</OtherContainerTextPrefix>
)
}
data-testid={`other-profile-button-${iconKey}`}
style={isDeceptive ? {
textDecoration: 'line-through',
opacity: 0.5,
cursor: 'not-allowed'
} : {}}
title={isDeceptive ? 'This URL contains deceptive patterns and has been disabled for security' : undefined}
>
{formattedValue}
</RecordItem>
)
}
export const OwnerProfileButton = ({
iconKey: label,
value: addressOrNameOrDate,
timestamp,
}: {
iconKey: string
value: string
timestamp?: number
}) => {
const { t } = useTranslation('common')
const breakpoints = useBreakpoint()
const dataType = useMemo(() => {
if (!addressOrNameOrDate)
// eslint-disable-next-line no-nested-ternary
return label === 'name.expiry'
? 'noExpiry'
: label === 'name.parent'
? 'noParent'
: 'notOwned'
if (label === 'name.expiry') return 'expiry'
if (isAddress(addressOrNameOrDate)) return 'address'
const isTLD = addressOrNameOrDate.split('.').length === 1
return isTLD ? 'tld' : 'name'
}, [addressOrNameOrDate, label])
const primary = usePrimaryName({
address: addressOrNameOrDate as Address,
enabled: dataType === 'address',
})
const { link, ...recordItemPartialProps } = useMemo(() => {
const base = {
keyLabel: t(label).toLocaleLowerCase(),
value: addressOrNameOrDate,
target: undefined,
as: 'button',
} as const
if (dataType === 'expiry')
return {
...base,
link: undefined,
children: addressOrNameOrDate,
} as const
if (dataType === 'noExpiry')
return {
...base,
link: undefined,
children: t('name.noExpiry').toLocaleLowerCase(),
} as const
if (dataType === 'notOwned')
return {
...base,
link: undefined,
children: t('name.notOwned').toLocaleLowerCase(),
} as const
if (dataType === 'noParent')
return { ...base, link: undefined, children: t('name.noParent').toLocaleLowerCase() }
if (dataType === 'address')
return {
...base,
link: primary.data?.name
? (getDestination(`/profile/${primary.data?.name}`) as string)
: (getDestination(`/address/${addressOrNameOrDate}`) as string),
children:
primary.data?.beautifiedName ||
(breakpoints.sm ? shortenAddress(addressOrNameOrDate) : addressOrNameOrDate.slice(0, 5)),
} as const
return {
...base,
link: getDestination(`/profile/${addressOrNameOrDate}`) as string,
children: addressOrNameOrDate,
} as const
}, [
dataType,
addressOrNameOrDate,
label,
breakpoints,
primary.data?.name,
primary.data?.beautifiedName,
t,
])
const [, copy] = useCopyToClipboard()
const items = [
link
? {
icon: UpRightArrowIcon,
label: 'View profile',
href: link,
}
: undefined,
primary.data?.name
? {
icon: CopyIcon,
label: 'Copy name',
onClick: () => copy(primary.data?.name!),
}
: undefined,
...(dataType === 'address'
? ([
{
icon: UpRightArrowIcon,
label: 'View address',
href: getDestination(`/${addressOrNameOrDate}`) as string,
},
{
icon: CopyIcon,
label: 'Copy address',
onClick: () => copy(addressOrNameOrDate),
},
{
icon: UpRightArrowIcon,
label: 'View on Etherscan',
href: makeEtherscanLink(addressOrNameOrDate, 'mainnet', 'address'),
},
] as DropdownItem[])
: []),
].filter((item) => item !== undefined) as DropdownItem[]
if (dataType === 'expiry') {
return (
<RecordItem
{...recordItemPartialProps}
data-testid={`owner-profile-button-${label}`}
data-timestamp={timestamp}
inline
size={breakpoints.sm ? 'large' : 'small'}
/>
)
}
return (
<Dropdown width={200} items={items} direction="up">
<RecordItem
{...recordItemPartialProps}
postfixIcon={VerticalDotsSVG}
data-testid={`owner-profile-button-${label}`}
data-timestamp={timestamp}
inline
size={breakpoints.sm ? 'large' : 'small'}
/>
</Dropdown>
)
}
export const VerificationProfileButton = ({
iconKey,
value,
isVerified,
showBadge,
}: {
iconKey: string
value: string
isVerified?: boolean
showBadge?: boolean
}) => {
const breakpoints = useBreakpoint()
const IconComponent = useMemo(
() => () => (
<VerificationIcon name={iconKey as Parameters<typeof DynamicVerificationIcon>[0]['name']} />
),
[iconKey],
)
if (!isVerificationProtocol(iconKey)) return null
const verificationData = getVerifierData(iconKey, value)
return verificationData ? (
<VerificationBadge
showBadge={showBadge}
isVerified={isVerified}
type="personhood"
tooltipContent={<VerificationBadgeVerifierTooltipContent isVerified={!!isVerified} />}
>
<RecordItem
icon={IconComponent}
size={breakpoints.sm ? 'large' : 'small'}
inline
data-testid={`verification-profile-button-${iconKey}`}
value={verificationData.value}
as="a"
link={verificationData.urlFormatter}
>
{verificationData.label}
</RecordItem>
</VerificationBadge>
) : null
}