Skip to content

Commit

Permalink
fix: nested button tooltip error, closes #4835
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarkhanzadian committed Jan 25, 2024
1 parent 4ec0cd9 commit 5b87e3d
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 104 deletions.
11 changes: 11 additions & 0 deletions src/app/common/hooks/use-brc20-tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useGetBrc20TokensQuery } from '@app/query/bitcoin/ordinals/brc20/brc20-tokens.query';

export function useBrc20Tokens() {
const { data: allBrc20TokensResponse } = useGetBrc20TokensQuery();
const brc20Tokens = allBrc20TokensResponse?.pages
.flatMap(page => page.brc20Tokens)
.filter(token => token.length > 0)
.flatMap(token => token);

return brc20Tokens;
}
2 changes: 1 addition & 1 deletion src/app/common/theme-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function ThemeSwitcherProvider({ children }: ThemeSwitcherProviderProps)
return (
<ThemeContext.Provider value={{ theme, userSelectedTheme, setUserSelectedTheme }}>
<RadixTheme appearance={theme}>
<RadixTooltip.Provider>{children}</RadixTooltip.Provider>
<RadixTooltip.Provider delayDuration={300}>{children}</RadixTooltip.Provider>
</RadixTheme>
</ThemeContext.Provider>
);
Expand Down
13 changes: 4 additions & 9 deletions src/app/components/brc20-tokens-loader.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import {
Brc20Token,
useGetBrc20TokensQuery,
} from '@app/query/bitcoin/ordinals/brc20/brc20-tokens.query';
import { useBrc20Tokens } from '@app/common/hooks/use-brc20-tokens';
import { Brc20Token } from '@app/query/bitcoin/ordinals/brc20/brc20-tokens.query';

interface Brc20TokensLoaderProps {
children(brc20Tokens: Brc20Token[]): React.JSX.Element;
}

export function Brc20TokensLoader({ children }: Brc20TokensLoaderProps) {
const { data: allBrc20TokensResponse } = useGetBrc20TokensQuery();
const brc20Tokens = allBrc20TokensResponse?.pages
.flatMap(page => page.brc20Tokens)
.filter(token => token.length > 0)
.flatMap(token => token);
const brc20Tokens = useBrc20Tokens();

if (!brc20Tokens) return null;
return children(brc20Tokens);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import { Brc20TokenAssetItem } from '@app/components/crypto-assets/bitcoin/brc20
import { useNativeSegwitBalance } from '@app/query/bitcoin/balance/btc-native-segwit-balance.hooks';
import { Brc20Token } from '@app/query/bitcoin/ordinals/brc20/brc20-tokens.query';
import { useCurrentAccountNativeSegwitAddressIndexZero } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';
import { BasicTooltip } from '@app/ui/components/tooltip/basic-tooltip';

import { Brc20AssetListLayout } from './components/brc20-token-asset-list.layout';

export function Brc20TokenAssetList(props: { brc20Tokens?: Brc20Token[] }) {
const navigate = useNavigate();
Expand All @@ -27,23 +24,13 @@ export function Brc20TokenAssetList(props: { brc20Tokens?: Brc20Token[] }) {
}

if (!props.brc20Tokens?.length) return null;

return (
<Brc20AssetListLayout>
{props.brc20Tokens?.map(token => (
<BasicTooltip
disabled={hasPositiveBtcBalanceForFees}
key={token.tick}
side="top"
label={'Not enough BTC in balance'}
>
<Brc20TokenAssetItem
token={token}
isPressable={hasPositiveBtcBalanceForFees}
onClick={hasPositiveBtcBalanceForFees ? () => navigateToBrc20SendForm(token) : noop}
/>
</BasicTooltip>
))}
</Brc20AssetListLayout>
);
return props.brc20Tokens.map(token => (
<Brc20TokenAssetItem
token={token}
isPressable={hasPositiveBtcBalanceForFees}
onClick={hasPositiveBtcBalanceForFees ? () => navigateToBrc20SendForm(token) : noop}
displayNotEnoughBalance={!hasPositiveBtcBalanceForFees}
key={token.tick}
/>
));
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,55 @@ interface Brc20TokenAssetItemLayoutProps extends BoxProps {
isPressable?: boolean;
onClick?(): void;
title: string;
displayNotEnoughBalance?: boolean;
}
export function Brc20TokenAssetItemLayout({
balance,
caption,
isPressable,
onClick,
title,
displayNotEnoughBalance,
}: Brc20TokenAssetItemLayoutProps) {
const [component, bind] = usePressable(isPressable);

const formattedBalance = formatBalance(balance.amount.toString());

return (
<Flex onClick={isPressable ? onClick : undefined} {...bind}>
<Flag align="middle" img={<Brc20TokenIcon />} spacing="space.04" width="100%">
<HStack alignItems="center" justifyContent="space-between" width="100%">
<styled.span
maxWidth="150px"
overflow="hidden"
textAlign="left"
textOverflow="ellipsis"
textStyle="label.01"
whiteSpace="nowrap"
>
{title}
</styled.span>
<BasicTooltip
label={formattedBalance.isAbbreviated ? balance.amount.toString() : undefined}
side="left"
>
<styled.span data-testid={title} textStyle="label.01">
{formattedBalance.value}
<BasicTooltip
disabled={!displayNotEnoughBalance}
side="top"
label={'Not enough BTC in balance'}
asChild
>
<Flex onClick={isPressable ? onClick : undefined} {...bind}>
<Flag align="middle" img={<Brc20TokenIcon />} spacing="space.04" width="100%">
<HStack alignItems="center" justifyContent="space-between" width="100%">
<styled.span
maxWidth="150px"
overflow="hidden"
textAlign="left"
textOverflow="ellipsis"
textStyle="label.01"
whiteSpace="nowrap"
>
{title}
</styled.span>
</BasicTooltip>
</HStack>
<HStack alignItems="center" justifyContent="space-between" height="1.25rem" width="100%">
<AssetCaption caption={caption} />
</HStack>
{component}
</Flag>
</Flex>
<BasicTooltip
label={formattedBalance.isAbbreviated ? balance.amount.toString() : undefined}
side="left"
>
<styled.span data-testid={title} textStyle="label.01">
{formattedBalance.value}
</styled.span>
</BasicTooltip>
</HStack>
<HStack alignItems="center" justifyContent="space-between" height="1.25rem" width="100%">
<AssetCaption caption={caption} />
</HStack>
{component}
</Flag>
</Flex>
</BasicTooltip>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ interface Brc20TokenAssetItemProps {
token: Brc20Token;
isPressable?: boolean;
onClick?(): void;
displayNotEnoughBalance?: boolean;
}
export function Brc20TokenAssetItem({ token, isPressable, onClick }: Brc20TokenAssetItemProps) {
export function Brc20TokenAssetItem({
token,
isPressable,
onClick,
displayNotEnoughBalance,
}: Brc20TokenAssetItemProps) {
return (
<Brc20TokenAssetItemLayout
balance={createMoney(Number(token.overall_balance), token.tick, 0)}
caption="BRC-20"
title={token.tick}
isPressable={isPressable}
onClick={onClick}
displayNotEnoughBalance={displayNotEnoughBalance}
/>
);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import type { AllTransferableCryptoAssetBalances } from '@shared/models/crypto-asset-balance.model';
import { StacksFungibleTokenAsset } from '@shared/models/crypto-asset.model';

import { useWalletType } from '@app/common/use-wallet-type';
import { Brc20TokenAssetList } from '@app/components/crypto-assets/bitcoin/brc20-token-asset-list/brc20-token-asset-list';
import { Brc20Token } from '@app/query/bitcoin/ordinals/brc20/brc20-tokens.query';

import { CryptoAssetListItem } from './crypto-asset-list-item';
import { CryptoAssetListLayout } from './crypto-asset-list.layout';

interface CryptoAssetListProps {
cryptoAssetBalances: AllTransferableCryptoAssetBalances[];
onItemClick(cryptoAssetBalance: AllTransferableCryptoAssetBalances): void;
brc20Tokens?: Brc20Token[];
}
export function CryptoAssetList({ cryptoAssetBalances, onItemClick }: CryptoAssetListProps) {
export function CryptoAssetList({
cryptoAssetBalances,
onItemClick,
brc20Tokens,
}: CryptoAssetListProps) {
const { whenWallet } = useWalletType();

return (
<CryptoAssetListLayout>
{cryptoAssetBalances.map(cryptoAssetBalance => (
Expand All @@ -21,6 +32,12 @@ export function CryptoAssetList({ cryptoAssetBalances, onItemClick }: CryptoAsse
}
/>
))}
{brc20Tokens
? whenWallet({
software: <Brc20TokenAssetList brc20Tokens={brc20Tokens} />,
ledger: null,
})
: null}
</CryptoAssetListLayout>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function TaprootBalanceDisplayer({ onSelectRetrieveBalance }: TaprootBala
if (!isRecoverFeatureEnabled) return null;
if (balance.amount.isLessThanOrEqualTo(0)) return null;
return (
<BasicTooltip label={taprootSpendNotSupportedYetMsg}>
<BasicTooltip label={taprootSpendNotSupportedYetMsg} asChild>
<Link onClick={() => onSelectRetrieveBalance()} textStyle="caption.02" variant="text">
{formatMoney(balance)}
</Link>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ export function BitcoinContractLockAmount({
</styled.span>
</HStack>
<HStack alignItems="center" justifyContent="space-between" mt="space.02">
{/** TODO: We need to persist the tooltip after it is clicked.
Current implementation of radix-ui tooltip doesn't allow that, ref: https://github.com/radix-ui/primitives/issues/2029 */}
{subtitle ? (
<BasicTooltip
disabled={!hoverLabel}
label={hasCopied ? 'Copied!' : hoverLabel}
side="bottom"
asChild
>
<styled.button
_hover={{ cursor: 'pointer' }}
Expand Down
13 changes: 3 additions & 10 deletions src/app/pages/send/choose-crypto-asset/choose-crypto-asset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import { useNavigate } from 'react-router-dom';
import { AllTransferableCryptoAssetBalances } from '@shared/models/crypto-asset-balance.model';
import { RouteUrls } from '@shared/route-urls';

import { useBrc20Tokens } from '@app/common/hooks/use-brc20-tokens';
import { useRouteHeader } from '@app/common/hooks/use-route-header';
import { useAllTransferableCryptoAssetBalances } from '@app/common/hooks/use-transferable-asset-balances.hooks';
import { useWalletType } from '@app/common/use-wallet-type';
import { Brc20TokensLoader } from '@app/components/brc20-tokens-loader';
import { Brc20TokenAssetList } from '@app/components/crypto-assets/bitcoin/brc20-token-asset-list/brc20-token-asset-list';
import { ChooseCryptoAssetLayout } from '@app/components/crypto-assets/choose-crypto-asset/choose-crypto-asset.layout';
import { CryptoAssetList } from '@app/components/crypto-assets/choose-crypto-asset/crypto-asset-list';
import { ModalHeader } from '@app/components/modal-header';
Expand All @@ -17,6 +16,7 @@ import { useCheckLedgerBlockchainAvailable } from '@app/store/accounts/blockchai

export function ChooseCryptoAsset() {
const allTransferableCryptoAssetBalances = useAllTransferableCryptoAssetBalances();
const brc20Tokens = useBrc20Tokens();

const { whenWallet } = useWalletType();
const navigate = useNavigate();
Expand Down Expand Up @@ -49,21 +49,14 @@ export function ChooseCryptoAsset() {
<ChooseCryptoAssetLayout title="choose asset to send">
<CryptoAssetList
onItemClick={cryptoAssetBalance => navigateToSendForm(cryptoAssetBalance)}
brc20Tokens={brc20Tokens}
cryptoAssetBalances={allTransferableCryptoAssetBalances.filter(asset =>
whenWallet({
ledger: checkBlockchainAvailable(asset.blockchain),
software: true,
})
)}
/>
{whenWallet({
software: (
<Brc20TokensLoader>
{brc20Tokens => <Brc20TokenAssetList brc20Tokens={brc20Tokens} />}
</Brc20TokensLoader>
),
ledger: null,
})}
</ChooseCryptoAssetLayout>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export function RecipientAddressDisplayer({ address }: RecipientAddressDisplayer
>
{address}
</styled.span>
<BasicTooltip label={hasCopied ? 'Copied!' : 'Copy address'} side="right">
{/** TODO: We need to persist the tooltip after it is clicked.
Current implementation of radix-ui tooltip doesn't allow that, ref: https://github.com/radix-ui/primitives/issues/2029 */}
<BasicTooltip label={hasCopied ? 'Copied!' : 'Copy address'} side="right" asChild>
<styled.button
_hover={{ cursor: 'pointer' }}
data-testid={SendCryptoAssetSelectors.RecipientBnsAddressCopyToClipboard}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,14 @@ export function BitcoinSendMaxButton({
if (sendMaxBalance === '0') return <Box height="32px" />;

return (
<BasicTooltip label={sendMaxTooltipLabel} side="bottom">
<Box>
<Link
data-testid={SendCryptoAssetSelectors.SendMaxBtn}
onClick={() => onSendMax()}
{...props}
>
{isSendingMax ? 'Sending max' : 'Send max'}
</Link>
</Box>
<BasicTooltip label={sendMaxTooltipLabel} side="bottom" asChild>
<Link
data-testid={SendCryptoAssetSelectors.SendMaxBtn}
onClick={() => onSendMax()}
{...props}
>
{isSendingMax ? 'Sending max' : 'Send max'}
</Link>
</BasicTooltip>
);
}
7 changes: 5 additions & 2 deletions src/app/ui/components/link/link.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ForwardedRef, forwardRef } from 'react';

import { styled } from 'leather-styles/jsx';
import { type LinkVariantProps, link as linkRecipe } from 'leather-styles/recipes/link';

Expand All @@ -6,16 +8,17 @@ const StyledLink = styled('a');
type LinkProps = Omit<React.ComponentProps<typeof StyledLink>, keyof LinkVariantProps> &
LinkVariantProps;

export function Link(props: LinkProps) {
export const Link = forwardRef((props: LinkProps, ref: ForwardedRef<HTMLAnchorElement>) => {
const { children, fullWidth, invert, size, variant, ...rest } = props;

return (
<StyledLink
ref={ref}
className={linkRecipe({ fullWidth, invert, size, variant })}
cursor="pointer"
{...rest}
>
{children}
</StyledLink>
);
}
});
5 changes: 3 additions & 2 deletions src/app/ui/components/tooltip/basic-tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ interface BasicTooltipProps {
label?: string;
disabled?: boolean;
side?: RadixTooltip.TooltipContentProps['side'];
asChild?: boolean;
}

export function BasicTooltip({ children, label, disabled, side }: BasicTooltipProps) {
export function BasicTooltip({ children, label, disabled, side, asChild }: BasicTooltipProps) {
const isDisabled = !label || disabled;
return (
<Tooltip.Root>
<Tooltip.Trigger>{children}</Tooltip.Trigger>
<Tooltip.Trigger asChild={asChild}>{children}</Tooltip.Trigger>
<Tooltip.Portal>
<Tooltip.Content hidden={isDisabled} side={side} sideOffset={5}>
{label}
Expand Down
Loading

0 comments on commit 5b87e3d

Please sign in to comment.