Skip to content
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

fix: asset row skeleton loader #5467

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
"@leather-wallet/constants": "0.6.3",
"@leather-wallet/crypto": "1.0.1",
"@leather-wallet/models": "0.8.0",
"@leather-wallet/query": "0.8.6",
"@leather-wallet/query": "0.8.7",
"@leather-wallet/tokens": "0.5.2",
"@leather-wallet/utils": "0.8.1",
"@ledgerhq/hw-transport-webusb": "6.27.19",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 8 additions & 12 deletions src/app/common/hooks/balance/use-total-balance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ export function useTotalBalance({ btcAddress, stxAddress }: UseTotalBalanceArgs)
const stxBalance = balance ? balance.totalBalance : createMoney(0, 'STX');

// get btc balance
const {
balance: btcBalance,
isLoading: isLoadingBtcBalance,
isFetching: isFetchingBtcBalance,
isInitialLoading: isInititalLoadingBtcBalance,
} = useBtcCryptoAssetBalanceNativeSegwit(btcAddress);
const { balance: btcBalance, query: btcQueryResult } =
useBtcCryptoAssetBalanceNativeSegwit(btcAddress);

return useMemo(() => {
// calculate total balance
Expand All @@ -46,20 +42,20 @@ export function useTotalBalance({ btcAddress, stxAddress }: UseTotalBalanceArgs)
totalBalance,
totalBalance.amount.isGreaterThanOrEqualTo(100_000) ? 0 : 2
),
isLoading: isLoading || isLoadingBtcBalance,
isInitialLoading: isInitialLoading || isInititalLoadingBtcBalance,
isFetching: isFetchingStacksBalance || isFetchingBtcBalance,
isLoading: isLoading || btcQueryResult.isLoading,
isInitialLoading: isInitialLoading || btcQueryResult.isInitialLoading,
isFetching: isFetchingStacksBalance || btcQueryResult.isFetching,
};
}, [
stxBalance,
stxMarketData,
btcBalance.availableBalance,
btcMarketData,
isLoading,
isLoadingBtcBalance,
btcQueryResult.isLoading,
btcQueryResult.isInitialLoading,
btcQueryResult.isFetching,
isInitialLoading,
isInititalLoadingBtcBalance,
isFetchingStacksBalance,
isFetchingBtcBalance,
]);
}
39 changes: 39 additions & 0 deletions src/app/components/crypto-asset-item/crypto-asset-item-error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { QueryObserverResult } from '@tanstack/react-query';
import { Box, styled } from 'leather-styles/jsx';

import { ItemLayout } from '@app/ui/components/item-layout/item-layout';

interface CryptoAssetItemErrorProps {
caption: string;
icon: React.ReactNode;
onRefetch?(): Promise<QueryObserverResult<unknown, unknown>>;
title: string;
}
export function CryptoAssetItemError({
caption,
icon,
onRefetch,
title,
}: CryptoAssetItemErrorProps) {
return (
<Box my="space.02">
<ItemLayout
flagImg={icon}
titleLeft={title}
captionLeft={caption}
titleRight={
<styled.span color="ink.text-subdued" textStyle="label.02">
Unable to load
</styled.span>
}
captionRight={
onRefetch && (
<styled.button lineHeight="20px" onClick={onRefetch}>
<styled.span textStyle="caption.01">Retry</styled.span>
</styled.button>
)
}
/>
</Box>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Box, Circle } from 'leather-styles/jsx';

import { ItemLayout } from '@app/ui/components/item-layout/item-layout';
import { SkeletonLoader } from '@app/ui/components/skeleton-loader/skeleton-loader';

export function CryptoAssetItemPlaceholder() {
return (
<Box my="space.02">
<ItemLayout
flagImg={<Circle bgColor="ink.text-non-interactive" size="36px" />}
titleLeft={<SkeletonLoader isLoading height="20px" width="126px" />}
captionLeft={<SkeletonLoader isLoading height="20px" width="78px" />}
titleRight={<SkeletonLoader isLoading width="126px" />}
captionRight={<SkeletonLoader isLoading width="78px" />}
/>
</Box>
);
}
12 changes: 10 additions & 2 deletions src/app/components/loaders/btc-balance-loader.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import type { BtcCryptoAssetBalance } from '@leather-wallet/models';
import { isFetchedWithSuccess, isInitializingData } from '@leather-wallet/query';

import { useBtcCryptoAssetBalanceNativeSegwit } from '@app/query/bitcoin/balance/btc-balance-native-segwit.hooks';
import { BtcAvatarIcon } from '@app/ui/components/avatar/btc-avatar-icon';

import { CryptoAssetItemError } from '../crypto-asset-item/crypto-asset-item-error';
import { CryptoAssetItemPlaceholder } from '../crypto-asset-item/crypto-asset-item-placeholder';

interface BtcBalanceLoaderProps {
address: string;
children(balance: BtcCryptoAssetBalance, isInitialLoading: boolean): React.ReactNode;
}
export function BtcBalanceLoader({ address, children }: BtcBalanceLoaderProps) {
const { balance, isInitialLoading } = useBtcCryptoAssetBalanceNativeSegwit(address);
return children(balance, isInitialLoading);
const { balance, query: result } = useBtcCryptoAssetBalanceNativeSegwit(address);
if (isInitializingData(result)) return <CryptoAssetItemPlaceholder />;
if (!isFetchedWithSuccess(result))
return <CryptoAssetItemError caption="BTC" icon={<BtcAvatarIcon />} title="Bitcoin" />;
return children(balance, result.isInitialLoading);
}
25 changes: 22 additions & 3 deletions src/app/components/loaders/stx-balance-loader.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
import type { StxCryptoAssetBalance } from '@leather-wallet/models';
import { useStxCryptoAssetBalance } from '@leather-wallet/query';
import {
isErrorTooManyRequests,
isFetchedWithSuccess,
isInitializingData,
useStxCryptoAssetBalance,
} from '@leather-wallet/query';

import { isFetchedWithSuccess } from '@app/query/query-config';
import { StxAvatarIcon } from '@app/ui/components/avatar/stx-avatar-icon';

import { CryptoAssetItemError } from '../crypto-asset-item/crypto-asset-item-error';
import { CryptoAssetItemPlaceholder } from '../crypto-asset-item/crypto-asset-item-placeholder';

interface StxBalanceLoaderProps {
address: string;
children(balance: StxCryptoAssetBalance, isInitialLoading: boolean): React.ReactNode;
}
export function StxBalanceLoader({ address, children }: StxBalanceLoaderProps) {
const result = useStxCryptoAssetBalance(address);
if (!isFetchedWithSuccess(result)) return null;
if (isInitializingData(result)) return <CryptoAssetItemPlaceholder />;
if (isErrorTooManyRequests(result))
return (
<CryptoAssetItemError
caption="STX"
icon={<StxAvatarIcon />}
onRefetch={() => result.refetch()}
title="Stacks"
/>
);
if (!isFetchedWithSuccess(result))
return <CryptoAssetItemError caption="STX" icon={<StxAvatarIcon />} title="Stacks" />;
const { data: balance, isInitialLoading } = result;
return children(balance, isInitialLoading);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface Brc20TokenAssetListProps {
}
export function Brc20TokenAssetList({ tokens, variant }: Brc20TokenAssetListProps) {
const navigate = useNavigate();
const { balance, isInitialLoading } = useCurrentBtcCryptoAssetBalanceNativeSegwit();
const { balance, query: result } = useCurrentBtcCryptoAssetBalanceNativeSegwit();

const hasPositiveBtcBalanceForFees =
variant === 'interactive' && balance.availableBalance.amount.isGreaterThan(0);
Expand All @@ -49,7 +49,7 @@ export function Brc20TokenAssetList({ tokens, variant }: Brc20TokenAssetListProp
availableBalance={token.balance.availableBalance}
captionLeft={token.info.name.toUpperCase()}
icon={<Brc20AvatarIcon />}
isLoading={isInitialLoading}
isLoading={result.isInitialLoading}
key={token.info.symbol}
onSelectAsset={
hasPositiveBtcBalanceForFees ? () => navigateToBrc20SendForm(token) : undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useIsFetching } from '@tanstack/react-query';

import { QueryPrefixes } from '@leather-wallet/query';
import { sumNumbers } from '@leather-wallet/utils';

import { QueryPrefixes } from '@app/query/query-prefixes';

function areAnyQueriesFetching(...args: number[]) {
return sumNumbers(args).toNumber() > 0;
}
Expand Down
17 changes: 5 additions & 12 deletions src/app/query/bitcoin/balance/btc-balance-native-segwit.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,24 @@ function createBtcCryptoAssetBalance(balance: Money): BtcCryptoAssetBalance {
export function useBtcCryptoAssetBalanceNativeSegwit(address: string) {
const runesEnabled = useRunesEnabled();

const {
data: utxos,
isInitialLoading,
isLoading,
isFetching,
} = useNativeSegwitUtxosByAddress({
const query = useNativeSegwitUtxosByAddress({
address,
filterInscriptionUtxos: true,
filterPendingTxsUtxos: true,
filterRunesUtxos: runesEnabled,
});

const balance = useMemo(() => {
if (isUndefined(utxos))
if (isUndefined(query.data))
return createBtcCryptoAssetBalance(createMoney(new BigNumber(0), 'BTC'));
return createBtcCryptoAssetBalance(
createMoney(sumNumbers(utxos.map(utxo => utxo.value)), 'BTC')
createMoney(sumNumbers(query.data.map(utxo => utxo.value)), 'BTC')
);
}, [utxos]);
}, [query.data]);

return {
balance,
isInitialLoading,
isLoading,
isFetching,
query,
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/query/bitcoin/ordinals/brc20/brc20-tokens.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import {
createBrc20TransferInscription,
encodeBrc20TransferInscription,
isFetchedWithSuccess,
useAverageBitcoinFeeRates,
useCalculateBitcoinFiatValue,
useConfigOrdinalsbot,
Expand All @@ -17,7 +18,6 @@ import {
} from '@leather-wallet/query';
import { createMoney, unitToFractionalUnit } from '@leather-wallet/utils';

import { isFetchedWithSuccess } from '@app/query/query-config';
import { useAppDispatch } from '@app/store';
import { useCurrentAccountIndex } from '@app/store/accounts/account';
import { useCurrentAccountNativeSegwitIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';
Expand Down
8 changes: 0 additions & 8 deletions src/app/query/query-config.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/app/query/query-prefixes.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/app/ui/components/skeleton-loader/skeleton-loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ interface SkeletonLoaderProps {
width?: string;
height?: string;
}

export function SkeletonLoader({
children,
width = '30px',
Expand Down
Loading