Skip to content

Commit

Permalink
feat: bring back address copy button (ava-labs#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh0w authored and Charon-Fan committed Dec 26, 2024
1 parent ece94e3 commit 94e8895
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
62 changes: 58 additions & 4 deletions src/pages/Accounts/components/AccountItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {
Checkbox,
Collapse,
CopyIcon,
Grow,
IconButton,
Stack,
Tooltip,
Typography,
Expand All @@ -15,6 +18,7 @@ import {
useState,
} from 'react';
import { useTranslation } from 'react-i18next';
import { NetworkVMType } from '@avalabs/core-chains-sdk';

import { useBalancesContext } from '@src/contexts/BalancesProvider';
import { useAccountsContext } from '@src/contexts/AccountsProvider';
Expand Down Expand Up @@ -154,6 +158,22 @@ export const AccountItem = forwardRef(
useAccountRename(account);
const { prompt: promptRemove, renderDialog: removeDialog } =
useAccountRemoval(toBeRemoved);
const handleCopyClick = useCallback(
(ev) => {
ev.stopPropagation();
if (!address || !network?.vmName) {
return;
}
const eventName = getCopyEventNameByNetworkType(network.vmName);

navigator.clipboard.writeText(address);
toast.success('Copied!', { duration: 1000 });
capture(eventName, {
type: account.type,
});
},
[address, account.type, capture, network?.vmName, toast]
);

return (
<>
Expand Down Expand Up @@ -216,8 +236,7 @@ export const AccountItem = forwardRef(
backgroundColor: isActive ? 'grey.700' : 'grey.800',
color: 'grey.50',
},
pt: 0.5,
pb: 1,
py: 0.75,
pl: 2,
pr: 1,
}}
Expand All @@ -243,15 +262,38 @@ export const AccountItem = forwardRef(
isActive={isActive}
/>
{address && (
<Stack direction="row">
<Tooltip title={address} disableInteractive>
<Stack
sx={{
flexDirection: 'row',
gap: 1,
alignItems: 'center',
}}
>
<Tooltip
title={address}
slotProps={{
popper: {
onClick: (ev) => ev.stopPropagation(),
},
}}
>
<Typography
variant="caption"
color={isActive ? 'text.primary' : 'text.secondary'}
>
{truncateAddress(address)}
</Typography>
</Tooltip>
<Grow in={cardHovered || isActive}>
<IconButton
color="primary"
size="small"
sx={{ p: 0.5 }}
onClick={handleCopyClick}
>
<CopyIcon size={12} />
</IconButton>
</Grow>
</Stack>
)}
</Stack>
Expand Down Expand Up @@ -299,3 +341,15 @@ export const AccountItem = forwardRef(
);

AccountItem.displayName = 'AccountItem';

const getCopyEventNameByNetworkType = (type: NetworkVMType) => {
// We remap BTC and EVM because we used those in older event names
const normalizedType =
type === NetworkVMType.BITCOIN
? 'Btc'
: type === NetworkVMType.EVM
? 'Eth'
: type;

return `AccountSelector${normalizedType}AddressCopied`;
};
1 change: 1 addition & 0 deletions src/pages/Accounts/components/AccountName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export default function AccountName({
<Grow {...commonTransitionProps} in={cardHovered && !isManageMode}>
<IconButton
size="small"
sx={{ p: 0.25 }}
onClick={(e) => {
e.stopPropagation();
promptRename();
Expand Down

0 comments on commit 94e8895

Please sign in to comment.