|
| 1 | +import { FC, useState, useRef, MouseEvent } from 'react'; |
| 2 | + |
| 3 | +import { Copy, External, ToastSuccess } from '@lidofinance/lido-ui'; |
| 4 | +import { |
| 5 | + AddressBadge, |
| 6 | + AddressBadgeProps, |
| 7 | + AddressLinkEtherscan, |
| 8 | + ButtonLink, |
| 9 | +} from 'shared/components'; |
| 10 | + |
| 11 | +import { |
| 12 | + ActionGroup, |
| 13 | + ActionWrapper, |
| 14 | + PopoverContent, |
| 15 | + PopoverWrapper, |
| 16 | +} from './styles'; |
| 17 | + |
| 18 | +import { truncateAddress } from 'utils/truncate-address'; |
| 19 | +import { Address } from 'viem'; |
| 20 | + |
| 21 | +export const AddressWithPopover: FC<AddressBadgeProps> = (props) => { |
| 22 | + const { address, onToggle, onClick, bgColor, crossedText } = props; |
| 23 | + const [showPopover, showPopoverVisibility] = useState(false); |
| 24 | + const badgeRef = useRef<HTMLDivElement>(null); |
| 25 | + |
| 26 | + const handleShowPopover = () => { |
| 27 | + showPopoverVisibility(true); |
| 28 | + }; |
| 29 | + |
| 30 | + const handleCopyLink = () => { |
| 31 | + if (!address) return; |
| 32 | + void navigator.clipboard.writeText(address); |
| 33 | + ToastSuccess(`Address ${truncateAddress({ address })} have been copied`); |
| 34 | + }; |
| 35 | + |
| 36 | + const handleClosePopover = () => { |
| 37 | + showPopoverVisibility(false); |
| 38 | + }; |
| 39 | + |
| 40 | + const handleClick = (event: MouseEvent<HTMLDivElement>) => { |
| 41 | + handleShowPopover(); |
| 42 | + onClick?.(event); |
| 43 | + }; |
| 44 | + |
| 45 | + return ( |
| 46 | + <> |
| 47 | + <AddressBadge |
| 48 | + ref={badgeRef} |
| 49 | + crossedText={crossedText} |
| 50 | + bgColor={bgColor} |
| 51 | + address={address} |
| 52 | + isActive={showPopover} |
| 53 | + onToggle={onToggle} |
| 54 | + onClick={handleClick} |
| 55 | + /> |
| 56 | + {!!badgeRef?.current && ( |
| 57 | + <PopoverWrapper |
| 58 | + anchorRef={{ |
| 59 | + current: badgeRef.current, |
| 60 | + }} |
| 61 | + backdrop |
| 62 | + offset="xs" |
| 63 | + onClose={handleClosePopover} |
| 64 | + placement="topLeft" |
| 65 | + open={showPopover} |
| 66 | + > |
| 67 | + <PopoverContent> |
| 68 | + <AddressBadge address={address} symbols={21} /> |
| 69 | + <ActionGroup> |
| 70 | + <ActionWrapper> |
| 71 | + <Copy fill="var(--lido-color-primary)" /> |
| 72 | + <ButtonLink onClick={handleCopyLink}>Copy address</ButtonLink> |
| 73 | + </ActionWrapper> |
| 74 | + <ActionWrapper> |
| 75 | + <External fill="var(--lido-color-primary)" /> |
| 76 | + {address && ( |
| 77 | + <AddressLinkEtherscan address={address as Address} /> |
| 78 | + )} |
| 79 | + </ActionWrapper> |
| 80 | + </ActionGroup> |
| 81 | + </PopoverContent> |
| 82 | + </PopoverWrapper> |
| 83 | + )} |
| 84 | + </> |
| 85 | + ); |
| 86 | +}; |
0 commit comments