|
| 1 | +import { CopyButton } from '@/features/common/components/copy-button' |
1 | 2 | import { cn } from '@/features/common/utils' |
2 | 3 | import { TemplatedNavLink } from '@/features/routing/components/templated-nav-link/templated-nav-link' |
3 | 4 | import { Urls } from '@/routes/urls' |
4 | 5 | import { ellipseAddress } from '@/utils/ellipse-address' |
5 | 6 | import { fixedForwardRef } from '@/utils/fixed-forward-ref' |
6 | | -import { PropsWithChildren } from 'react' |
| 7 | +import { PropsWithChildren, useCallback } from 'react' |
| 8 | +import { toast } from 'react-toastify' |
7 | 9 |
|
8 | 10 | type Props = PropsWithChildren<{ |
9 | 11 | address: string |
10 | 12 | short?: boolean |
11 | 13 | className?: string |
| 14 | + showCopyButton?: boolean |
12 | 15 | }> |
13 | 16 |
|
14 | 17 | export const AccountLink = fixedForwardRef( |
15 | | - ({ address, short, className, children, ...rest }: Props, ref?: React.LegacyRef<HTMLAnchorElement>) => { |
| 18 | + ({ address, short, className, children, showCopyButton, ...rest }: Props, ref?: React.LegacyRef<HTMLAnchorElement>) => { |
| 19 | + const copyClipboard = useCallback(async () => { |
| 20 | + await navigator.clipboard.writeText(address) |
| 21 | + toast.success('Address copied to clipboard') |
| 22 | + }, [address]) |
16 | 23 | return ( |
17 | | - <TemplatedNavLink |
18 | | - className={cn(!children && 'text-primary underline', className)} |
19 | | - urlTemplate={Urls.Explore.Account.ByAddress} |
20 | | - urlParams={{ address }} |
21 | | - ref={ref} |
22 | | - {...rest} |
23 | | - > |
24 | | - {children ? children : short ? <abbr title={address}>{ellipseAddress(address)}</abbr> : address} |
25 | | - </TemplatedNavLink> |
| 24 | + <div className={cn('inline-flex gap-2 items-center')}> |
| 25 | + <TemplatedNavLink |
| 26 | + className={cn(!children && 'text-primary underline', className)} |
| 27 | + urlTemplate={Urls.Explore.Account.ByAddress} |
| 28 | + urlParams={{ address }} |
| 29 | + ref={ref} |
| 30 | + {...rest} |
| 31 | + > |
| 32 | + {children ? children : short ? <abbr title={address}>{ellipseAddress(address)}</abbr> : address} |
| 33 | + </TemplatedNavLink> |
| 34 | + {showCopyButton && <CopyButton onClick={copyClipboard} />} |
| 35 | + </div> |
26 | 36 | ) |
27 | 37 | } |
28 | 38 | ) |
0 commit comments