Skip to content

Commit

Permalink
feat: add option to hide balance, closes leather-io#5096
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianocola committed Sep 3, 2024
1 parent ad2d69f commit 57ee6b0
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 32 deletions.
3 changes: 2 additions & 1 deletion src/app/components/account-total-balance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { styled } from 'leather-styles/jsx';
import { SkeletonLoader, shimmerStyles } from '@leather.io/ui';

import { useTotalBalance } from '@app/common/hooks/balance/use-total-balance';
import { HideableBalance } from '@app/components/balance/hideable-balance';

interface AccountTotalBalanceProps {
btcAddress: string;
Expand All @@ -26,7 +27,7 @@ export const AccountTotalBalance = memo(({ btcAddress, stxAddress }: AccountTota
textStyle="label.02"
data-state={isLoadingAdditionalData || isFetching ? 'loading' : undefined}
>
{totalUsdBalance}
<HideableBalance>{totalUsdBalance}</HideableBalance>
</styled.span>
</SkeletonLoader>
);
Expand Down
14 changes: 14 additions & 0 deletions src/app/components/balance/hideable-balance.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { type HTMLStyledProps, styled } from 'leather-styles/jsx';

import { useHideBalance } from '@app/store/settings/settings.selectors';

interface HideableBalanceProps extends HTMLStyledProps<'span'> {
children: React.ReactNode;
forceHidden?: boolean;
}

export function HideableBalance({ forceHidden, children, ...rest }: HideableBalanceProps) {
const hideBalance = useHideBalance();

return <styled.span {...rest}>{hideBalance || forceHidden ? '•••••' : children}</styled.span>;
}
15 changes: 10 additions & 5 deletions src/app/components/crypto-asset-item/crypto-asset-item.layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box, Flex, styled } from 'leather-styles/jsx';
import { Box, Flex } from 'leather-styles/jsx';

import type { Money } from '@leather.io/models';
import {
Expand All @@ -11,6 +11,8 @@ import {
} from '@leather.io/ui';
import { spamFilter } from '@leather.io/utils';

import { HideableBalance } from '@app/components/balance/hideable-balance';
import { useHideBalance } from '@app/store/settings/settings.selectors';
import { BasicTooltip } from '@app/ui/components/tooltip/basic-tooltip';

import { parseCryptoAssetBalance } from './crypto-asset-item.layout.utils';
Expand Down Expand Up @@ -43,24 +45,25 @@ export function CryptoAssetItemLayout({
titleLeft,
titleRightBulletInfo,
}: CryptoAssetItemLayoutProps) {
const hideBalance = useHideBalance();
const { availableBalanceString, dataTestId, formattedBalance } =
parseCryptoAssetBalance(availableBalance);

const titleRight = (
<SkeletonLoader width="126px" isLoading={isLoading}>
<BasicTooltip
asChild
label={formattedBalance.isAbbreviated ? availableBalanceString : undefined}
label={formattedBalance.isAbbreviated && !hideBalance ? availableBalanceString : undefined}
side="left"
>
<Flex alignItems="center" gap="space.02" textStyle="label.02">
<BulletSeparator>
<styled.span
<HideableBalance
data-state={isLoadingAdditionalData ? 'loading' : undefined}
className={shimmerStyles}
>
{formattedBalance.value} {balanceSuffix}
</styled.span>
</HideableBalance>
{titleRightBulletInfo}
</BulletSeparator>
</Flex>
Expand All @@ -76,7 +79,9 @@ export function CryptoAssetItemLayout({
data-state={isLoadingAdditionalData ? 'loading' : undefined}
className={shimmerStyles}
>
{availableBalance.amount.toNumber() > 0 ? fiatBalance : null}
<HideableBalance>
{availableBalance.amount.toNumber() > 0 ? fiatBalance : null}
</HideableBalance>
</Caption>
{captionRightBulletInfo}
</BulletSeparator>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Box, Flex, HStack, styled } from 'leather-styles/jsx';

import { InfoCircleIcon } from '@leather.io/ui';

import { HideableBalance } from '@app/components/balance/hideable-balance';
import { BasicTooltip } from '@app/ui/components/tooltip/basic-tooltip';

interface AvailableBalanceProps {
Expand All @@ -26,7 +27,7 @@ export function AvailableBalance({
</BasicTooltip>
</HStack>
<styled.span color="ink.text-subdued" mr="space.02" textStyle="caption.01">
{balance}
<HideableBalance>{balance}</HideableBalance>
</styled.span>
</Flex>
);
Expand Down
10 changes: 8 additions & 2 deletions src/app/components/transaction-item/transaction-item.layout.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ReactNode } from 'react';

import { HStack, styled } from 'leather-styles/jsx';
import { HStack } from 'leather-styles/jsx';

import { Caption, ItemLayout, Pressable } from '@leather.io/ui';

import { HideableBalance } from '@app/components/balance/hideable-balance';

interface TransactionItemLayoutProps {
openTxLink(): void;
rightElement?: ReactNode;
Expand Down Expand Up @@ -42,7 +44,11 @@ export function TransactionItemLayout({
</HStack>
}
titleRight={
rightElement ? rightElement : <styled.span textStyle="label.02">{txValue}</styled.span>
rightElement ? (
rightElement
) : (
<HideableBalance textStyle="label.02">{txValue}</HideableBalance>
)
}
/>
</Pressable>
Expand Down
21 changes: 21 additions & 0 deletions src/app/features/settings/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
ExitIcon,
ExpandIcon,
ExternalLinkIcon,
Eye1ClosedIcon,
Eye1Icon,
Flag,
GlobeTiltedIcon,
KeyIcon,
Expand All @@ -37,6 +39,8 @@ import { SignOut } from '@app/features/settings/sign-out/sign-out-confirm';
import { ThemeSheet } from '@app/features/settings/theme/theme-dialog';
import { useLedgerDeviceTargetId } from '@app/store/ledger/ledger.selectors';
import { useCurrentNetworkId } from '@app/store/networks/networks.selectors';
import { useToggleHideBalance } from '@app/store/settings/settings.actions';
import { useHideBalance } from '@app/store/settings/settings.selectors';

import { openFeedbackSheet } from '../feedback-button/feedback-button';
import { extractDeviceNameFromKnownTargetIds } from '../ledger/utils/generic-ledger-utils';
Expand Down Expand Up @@ -67,6 +71,9 @@ export function Settings({
const { walletType } = useWalletType();
const targetId = useLedgerDeviceTargetId();

const hideBalance = useHideBalance();
const toggleHideBalance = useToggleHideBalance();

const location = useLocation();

const { isPressed: showAdvancedMenuOptions } = useModifierKey('alt', 120);
Expand Down Expand Up @@ -189,6 +196,20 @@ export function Settings({
</Flex>
</Flag>
</DropdownMenu.Item>

<DropdownMenu.Item
data-testid={SettingsSelectors.ToggleTheme}
onSelect={() => {
void analytics.track('click_toggle_privacy');
toggleHideBalance();
}}
>
<Flag img={hideBalance ? <Eye1ClosedIcon /> : <Eye1Icon />}>
<Flex justifyContent="space-between" textStyle="label.02">
Toggle privacy
</Flex>
</Flag>
</DropdownMenu.Item>
</DropdownMenu.Group>
<Divider />
<DropdownMenu.Group>
Expand Down
6 changes: 6 additions & 0 deletions src/app/pages/home/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { ModalBackgroundWrapper } from '@app/routes/components/modal-background-
import { useCurrentAccountIndex } from '@app/store/accounts/account';
import { useCurrentAccountNativeSegwitAddressIndexZero } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';
import { useCurrentStacksAccount } from '@app/store/accounts/blockchain/stacks/stacks-account.hooks';
import { useToggleHideBalance } from '@app/store/settings/settings.actions';
import { useHideBalance } from '@app/store/settings/settings.selectors';
import { AccountCard } from '@app/ui/components/account/account.card';

import { AccountActions } from './components/account-actions';
Expand All @@ -30,6 +32,8 @@ export function Home() {
const navigate = useNavigate();
const account = useCurrentStacksAccount();
const currentAccountIndex = useCurrentAccountIndex();
const hideBalance = useHideBalance();
const toggleHideBalance = useToggleHideBalance();

const { data: name = '', isFetching: isFetchingBnsName } = useAccountDisplayName({
address: account?.address || '',
Expand Down Expand Up @@ -63,9 +67,11 @@ export function Home() {
name={name}
balance={totalUsdBalance}
toggleSwitchAccount={() => setIsShowingSwitchAccount(!isShowingSwitchAccount)}
toggleHideBlance={toggleHideBalance}
isFetchingBnsName={isFetchingBnsName}
isLoadingBalance={isLoading}
isLoadingAdditionalData={isLoadingAdditionalData}
hideBalance={hideBalance}
>
<AccountActions />
</AccountCard>
Expand Down
5 changes: 5 additions & 0 deletions src/app/store/settings/settings.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ export function useDismissMessage() {
const dispatch = useDispatch();
return (messageId: string) => dispatch(settingsActions.messageDismissed(messageId));
}

export function useToggleHideBalance() {
const dispatch = useDispatch();
return () => dispatch(settingsActions.toggleHideBlance());
}
6 changes: 6 additions & 0 deletions src/app/store/settings/settings.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ const selectDismissedMessageIds = createSelector(
export function useDismissedMessageIds() {
return useSelector(selectDismissedMessageIds);
}

const selectHideBalance = createSelector(selectSettings, state => state.hideBalance ?? false);

export function useHideBalance() {
return useSelector(selectHideBalance);
}
4 changes: 4 additions & 0 deletions src/app/store/settings/settings.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { UserSelectedTheme } from '@app/common/theme-provider';
interface InitialState {
userSelectedTheme: UserSelectedTheme;
dismissedMessages: string[];
hideBalance?: boolean;
}

const initialState: InitialState = {
Expand All @@ -26,5 +27,8 @@ export const settingsSlice = createSlice({
resetMessages(state) {
state.dismissedMessages = [];
},
toggleHideBlance(state) {
state.hideBalance = !state.hideBalance;
},
},
});
24 changes: 24 additions & 0 deletions src/app/ui/components/account/account.card.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function AccountCard() {
name="leather.btc"
balance="$1,000"
toggleSwitchAccount={() => null}
toggleHideBlance={() => null}
isLoadingBalance={false}
isFetchingBnsName={false}
>
Expand All @@ -44,6 +45,7 @@ export function AccountCardLoading() {
name="leather.btc"
balance="$1,000"
toggleSwitchAccount={() => null}
toggleHideBlance={() => null}
isLoadingBalance
isFetchingBnsName={false}
>
Expand All @@ -63,6 +65,7 @@ export function AccountCardBnsNameLoading() {
name="leather.btc"
balance="$1,000"
toggleSwitchAccount={() => null}
toggleHideBlance={() => null}
isLoadingBalance={false}
isFetchingBnsName
>
Expand All @@ -75,3 +78,24 @@ export function AccountCardBnsNameLoading() {
</Component>
);
}

export function AccountCardHiddenBalance() {
return (
<Component
name="leather.btc"
balance="$1,000"
toggleSwitchAccount={() => null}
toggleHideBlance={() => null}
isLoadingBalance={false}
isFetchingBnsName={false}
hideBalance
>
<Flex justify="space-between">
<IconButton icon={<ArrowUpIcon />} label="Send" />
<IconButton icon={<ArrowDownIcon />} label="Receive" />
<IconButton icon={<PlusIcon />} label="Buy" />
<IconButton icon={<ArrowsRepeatLeftRightIcon />} label="Swap" />
</Flex>
</Component>
);
}
Loading

0 comments on commit 57ee6b0

Please sign in to comment.