-
Notifications
You must be signed in to change notification settings - Fork 153
DRAFT: add asset context menu #6161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
fabric-8
wants to merge
8
commits into
dev
Choose a base branch
from
asset-actions
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
73cae7b
feat: add asset context menu
fabric-8 cfe0d01
chore: minor style fixes
fabric-8 9c904ad
chore: rename menu
fabric-8 2b3ef4f
chore: revert unrelated changes
fabric-8 f88f59b
Fix swap navigation in asset dropdown menu
fabric-8 605ee44
chore: make swaps work, address feedback
fabric-8 81ef0b3
chore: add radix context menu
fabric-8 c29215c
chore: fix errors
fabric-8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import React from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
import * as ContextMenu from '@radix-ui/react-context-menu'; | ||
import { css } from 'leather-styles/css'; | ||
import { HStack } from 'leather-styles/jsx'; | ||
|
||
import { ArrowsRepeatLeftRightIcon, InboxIcon, PaperPlaneIcon } from '@leather.io/ui'; | ||
|
||
import { analytics } from '@shared/utils/analytics'; | ||
|
||
import { copyToClipboard } from '@app/common/utils/copy-to-clipboard'; | ||
import { useToast } from '@app/features/toasts/use-toast'; | ||
import { useConfigSwapsEnabled } from '@app/query/common/remote-config/remote-config.query'; | ||
|
||
interface AssetContextMenuProps { | ||
assetSymbol: string; | ||
contractId?: string; | ||
address: string; | ||
children: React.ReactNode; | ||
} | ||
|
||
const menuStyles = css({ | ||
backgroundColor: 'ink.background-primary', | ||
borderRadius: 'sm', | ||
boxShadow: 'menu', | ||
border: '1px solid', | ||
borderColor: 'ink.border-default', | ||
minWidth: '140px', | ||
overflow: 'hidden', | ||
padding: 'space.01', | ||
}); | ||
|
||
const menuItemStyles = css({ | ||
opacity: 1, | ||
width: '100%', | ||
cursor: 'pointer', | ||
display: 'block', | ||
outline: 'none', | ||
borderRadius: 'xs', | ||
_hover: { | ||
backgroundColor: 'ink.component-background-hover', | ||
}, | ||
'&[data-disabled]': { | ||
opacity: 0.5, | ||
cursor: 'not-allowed', | ||
_hover: { | ||
backgroundColor: 'transparent', | ||
}, | ||
}, | ||
}); | ||
|
||
const menuItemContentStyles = css({ | ||
width: '100%', | ||
px: 'space.03', | ||
py: 'space.02', | ||
gap: 'space.02', | ||
}); | ||
|
||
interface AssetContextMenuItemProps { | ||
icon: React.ReactNode; | ||
label: string; | ||
onClick?(): void; | ||
disabled?: boolean; | ||
} | ||
|
||
function AssetContextMenuItem({ icon, label, onClick, disabled }: AssetContextMenuItemProps) { | ||
return ( | ||
<ContextMenu.Item className={menuItemStyles} onClick={onClick} disabled={disabled}> | ||
<HStack className={menuItemContentStyles}> | ||
{icon} | ||
{label} | ||
</HStack> | ||
</ContextMenu.Item> | ||
); | ||
} | ||
|
||
export function AssetContextMenu({ | ||
assetSymbol, | ||
contractId, | ||
address, | ||
children, | ||
}: AssetContextMenuProps) { | ||
const navigate = useNavigate(); | ||
const toast = useToast(); | ||
const isSwapEnabled = useConfigSwapsEnabled(); | ||
|
||
const handleReceiveClick = async () => { | ||
// Track analytics based on asset type | ||
const lowerCaseSymbol = assetSymbol.toLowerCase(); | ||
|
||
if (lowerCaseSymbol === 'btc') { | ||
// For BTC, we need to include the type parameter | ||
void analytics.track('copy_btc_address_to_clipboard', { type: 'btc' }); | ||
} else { | ||
// For STX and other tokens, no second parameter is needed | ||
void analytics.track('copy_stx_address_to_clipboard'); | ||
} | ||
|
||
await copyToClipboard(address); | ||
toast.success('Address copied to clipboard'); | ||
}; | ||
|
||
return ( | ||
<ContextMenu.Root> | ||
<ContextMenu.Trigger asChild>{children}</ContextMenu.Trigger> | ||
<ContextMenu.Portal> | ||
<ContextMenu.Content | ||
className={menuStyles} | ||
style={{ | ||
zIndex: 999, | ||
}} | ||
onPointerDownOutside={e => { | ||
e.preventDefault(); | ||
}} | ||
> | ||
<ContextMenu.Group> | ||
<AssetContextMenuItem | ||
icon={<PaperPlaneIcon variant="small" />} | ||
label="Send" | ||
onClick={() => { | ||
navigate(`/send/${assetSymbol}${contractId ? `/${contractId}` : ''}`); | ||
}} | ||
/> | ||
<AssetContextMenuItem | ||
icon={<InboxIcon variant="small" />} | ||
label="Receive" | ||
onClick={handleReceiveClick} | ||
/> | ||
<AssetContextMenuItem | ||
icon={<ArrowsRepeatLeftRightIcon variant="small" />} | ||
label="Swap" | ||
onClick={() => { | ||
if (!isSwapEnabled) return; | ||
|
||
// Normalize the asset symbol for URL construction | ||
// This handles special characters and ensures consistent casing | ||
const normalizedSymbol = assetSymbol.toLowerCase().trim(); | ||
|
||
// Determine chain based on normalized asset symbol | ||
const chain = normalizedSymbol === 'btc' ? 'bitcoin' : 'stacks'; | ||
|
||
// Construct the URL with the normalized symbol | ||
const url = `/swap/${chain}/${normalizedSymbol}`; | ||
|
||
navigate(url); | ||
}} | ||
disabled={!isSwapEnabled} | ||
/> | ||
</ContextMenu.Group> | ||
</ContextMenu.Content> | ||
</ContextMenu.Portal> | ||
</ContextMenu.Root> | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fabric-8, the way we have our UI library set up is that we export pre-styled radix components via
@leather.io/ui
. We should do the same here.First, creating a new context menu component in mono/packages/ui, adding styling, exporting, then consuming here. Let me know if you need any guidance doing this, though if you look at some of the other components wrapping with
forwardRef
it's pretty straightforward