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: problem with endless loading of balances and names #5296

Merged
merged 1 commit into from
Apr 24, 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
25 changes: 13 additions & 12 deletions src/app/common/hooks/account/use-account-names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import { isUndefined } from '@shared/utils';

import { parseIfValidPunycode } from '@app/common/utils';
import { getAutogeneratedAccountDisplayName } from '@app/common/utils/get-account-display-name';
import {
useCurrentAccountNames,
useGetAccountNamesByAddressQuery,
} from '@app/query/stacks/bns/bns.hooks';
import { useCurrentAccountNames } from '@app/query/stacks/bns/bns.hooks';
import { useGetBnsNamesOwnedByAddress } from '@app/query/stacks/bns/bns.query';
import { useCurrentStacksAccount } from '@app/store/accounts/blockchain/stacks/stacks-account.hooks';

export function useCurrentAccountDisplayName() {
Expand All @@ -23,12 +21,15 @@ export function useCurrentAccountDisplayName() {
}

export function useAccountDisplayName({ address, index }: { index: number; address: string }) {
const { data: names = [], isLoading } = useGetAccountNamesByAddressQuery(address);
return useMemo(() => {
const name = names[0] || getAutogeneratedAccountDisplayName(index);
return {
name,
isLoading,
};
}, [names, index, isLoading]);
const query = useGetBnsNamesOwnedByAddress(address, {
select: resp => {
const names = resp.names ?? [];
return names[0] || getAutogeneratedAccountDisplayName(index);
},
});

return {
...query,
data: query.data || getAutogeneratedAccountDisplayName(index),
};
Comment on lines 23 to +34
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only need one of these two techniques right? select or replacing data?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

problem here is again in ledger mode, when query is not enabled, select doesn't work

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but if you do it on the data prop, it works for both cases, and the select isn't needed?

Copy link
Contributor Author

@alter-eggo alter-eggo Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean remove select and
data: (query.data?.names ?? [])[0] || getAutogeneratedAccountDisplayName(index) ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just seems strange to me you're repeating the logic twice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can try it in ledger bitcoin only mode

}
4 changes: 3 additions & 1 deletion src/app/common/hooks/balance/btc/use-btc-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function useBtcAssetBalance(btcAddress: string) {
const {
btcBalance: btcAssetBalance,
isLoading,
isFetching,
isInitialLoading,
} = useNativeSegwitBalance(btcAddress);

Expand All @@ -28,8 +29,9 @@ export function useBtcAssetBalance(btcAddress: string) {
baseCurrencyAmountInQuote(btcAssetBalance.balance, btcMarketData)
),
isLoading,
isFetching,
isInitialLoading,
}),
[btcAddress, btcAssetBalance, btcMarketData, isLoading, isInitialLoading]
[btcAddress, btcAssetBalance, btcMarketData, isLoading, isInitialLoading, isFetching]
);
}
11 changes: 10 additions & 1 deletion src/app/common/hooks/balance/use-total-balance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ export function useTotalBalance({ btcAddress, stxAddress }: UseTotalBalanceArgs)
const stxMarketData = useCryptoCurrencyMarketDataMeanAverage('STX');

// get stx balance
const { data: balances, isLoading, isInitialLoading } = useStacksAccountBalances(stxAddress);
const {
data: balances,
isLoading,
isInitialLoading,
isFetching: isFetchingStacksBalance,
} = useStacksAccountBalances(stxAddress);
const stxBalance = balances ? balances.stx.balance : createMoney(0, 'STX');

// get btc balance
const {
btcAvailableAssetBalance,
isLoading: isLoadingBtcBalance,
isFetching: isFetchingBtcBalance,
isInitialLoading: isInititalLoadingBtcBalance,
} = useBtcAssetBalance(btcAddress);

Expand All @@ -44,6 +50,7 @@ export function useTotalBalance({ btcAddress, stxAddress }: UseTotalBalanceArgs)
),
isLoading: isLoading || isLoadingBtcBalance,
isInitialLoading: isInitialLoading || isInititalLoadingBtcBalance,
isFetching: isFetchingStacksBalance || isFetchingBtcBalance,
};
}, [
btcAvailableAssetBalance.balance,
Expand All @@ -54,5 +61,7 @@ export function useTotalBalance({ btcAddress, stxAddress }: UseTotalBalanceArgs)
stxBalance,
isLoading,
isLoadingBtcBalance,
isFetchingStacksBalance,
isFetchingBtcBalance,
]);
}
4 changes: 2 additions & 2 deletions src/app/components/account-total-balance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface AccountTotalBalanceProps {
}

export const AccountTotalBalance = memo(({ btcAddress, stxAddress }: AccountTotalBalanceProps) => {
const { totalUsdBalance, isLoading, isInitialLoading } = useTotalBalance({
const { totalUsdBalance, isFetching, isInitialLoading } = useTotalBalance({
btcAddress,
stxAddress,
});
Expand All @@ -25,7 +25,7 @@ export const AccountTotalBalance = memo(({ btcAddress, stxAddress }: AccountTota
<styled.span
className={shimmerStyles}
textStyle="label.02"
data-state={isLoading ? 'loading' : undefined}
data-state={isFetching ? 'loading' : undefined}
>
{totalUsdBalance}
</styled.span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface StacksCryptoAssetsProps {
}
export function StacksCryptoAssets({ account }: StacksCryptoAssetsProps) {
const { data: names = [] } = useCurrentAccountNames();

const stacksNftsMetadataResp = useStacksNonFungibleTokensMetadata(account);
const analytics = useAnalytics();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const SwitchAccountListItem = memo(
'SWITCH_ACCOUNTS' + stxAddress || btcAddress
);
const { handleSwitchAccount } = useSwitchAccount(handleClose);
const { name, isLoading: isLoadingBnsName } = useAccountDisplayName({
const { data: name = '', isFetching: isFetchingBnsName } = useAccountDisplayName({
address: stxAddress,
index,
});
Expand All @@ -43,7 +43,7 @@ export const SwitchAccountListItem = memo(
return (
<AccountListItemLayout
accountAddresses={<AcccountAddresses index={index} />}
accountName={<AccountNameLayout isLoading={isLoadingBnsName}>{name}</AccountNameLayout>}
accountName={<AccountNameLayout isLoading={isFetchingBnsName}>{name}</AccountNameLayout>}
avatar={
<AccountAvatarItem
index={index}
Expand Down
3 changes: 2 additions & 1 deletion src/app/pages/choose-account/components/accounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ interface ChooseAccountItemProps extends FlexProps {
}
const ChooseAccountItem = memo(
({ account, isLoading, onSelectAccount }: ChooseAccountItemProps) => {
const { name } = useAccountDisplayName(account);
const { data: name = '' } = useAccountDisplayName(account);

const btcAddress = useNativeSegwitAccountIndexAddressIndexZero(account.index);

const accountSlug = useMemo(() => slugify(`Account ${account?.index + 1}`), [account?.index]);
Expand Down
4 changes: 2 additions & 2 deletions src/app/pages/home/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function Home() {
const navigate = useNavigate();
const account = useCurrentStacksAccount();

const { name, isLoading: isLoadingBnsName } = useAccountDisplayName({
const { data: name = '', isFetching: isFetchingBnsName } = useAccountDisplayName({
address: account?.address || '',
index: account?.index || 0,
});
Expand Down Expand Up @@ -56,7 +56,7 @@ export function Home() {
/>
}
toggleSwitchAccount={() => setIsShowingSwitchAccount(!isShowingSwitchAccount)}
isLoadingBnsName={isLoadingBnsName}
isFetchingBnsName={isFetchingBnsName}
isLoadingBalance={isInitialLoading}
>
<AccountActions />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const AccountListItem = memo(({ index, stacksAccount, onClose }: AccountL
BitcoinSendFormValues | StacksSendFormValues
>();
const stacksAddress = stacksAccount?.address || '';
const { name } = useAccountDisplayName({ address: stacksAddress, index });
const { data: name = '' } = useAccountDisplayName({ address: stacksAddress, index });

const bitcoinSigner = useNativeSegwitSigner(index);
const bitcoinAddress = bitcoinSigner?.(0).address || '';
Expand Down
7 changes: 1 addition & 6 deletions src/app/query/bitcoin/address/utxos-by-address.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,16 @@ function useFilterInscriptionsByAddress(address: string) {
const {
data: inscriptionsList,
hasNextPage: hasMoreInscriptionsToLoad,
isLoading: isLoadingInscriptions,
isInitialLoading: isInitialLoadingInscriptions,
} = useInscriptionsByAddressQuery(address);

const filterOutInscriptions = useCallback(
(utxos: UtxoResponseItem[]) => {
// While infinite query checks if has more data to load, or Stamps
// are loading, assume nothing is spendable
if (hasMoreInscriptionsToLoad || isLoadingInscriptions) return [];

const inscriptions = inscriptionsList?.pages.flatMap(page => page.results) ?? [];

return filterUtxosWithInscriptions(inscriptions, utxos);
},
[hasMoreInscriptionsToLoad, inscriptionsList?.pages, isLoadingInscriptions]
[inscriptionsList?.pages]
);

return {
Expand Down
2 changes: 2 additions & 0 deletions src/app/query/bitcoin/balance/btc-balance.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function useGetBitcoinBalanceByAddress(address: string) {
data: utxos,
isInitialLoading,
isLoading,
isFetching,
} = useNativeSegwitUtxosByAddress({
address,
filterInscriptionUtxos: true,
Expand All @@ -33,5 +34,6 @@ export function useGetBitcoinBalanceByAddress(address: string) {
balance,
isInitialLoading,
isLoading,
isFetching,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { useGetBitcoinBalanceByAddress } from './btc-balance.hooks';

// Balance is derived from a single query in address reuse mode
export function useNativeSegwitBalance(address: string) {
const { balance, isInitialLoading, isLoading } = useGetBitcoinBalanceByAddress(address);
const { balance, isInitialLoading, isLoading, isFetching } =
useGetBitcoinBalanceByAddress(address);

const wrappedBalance = useMemo(
() => createBitcoinCryptoCurrencyAssetTypeWrapper(balance),
Expand All @@ -18,6 +19,7 @@ export function useNativeSegwitBalance(address: string) {
btcBalance: wrappedBalance,
isInitialLoading,
isLoading,
isFetching,
};
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one could also be a single query being returned with a select

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it couldn't... actually not that easy. the query useGetUtxosByAddressQuery is wrapped several times before this exact hook


Expand Down
4 changes: 0 additions & 4 deletions src/app/query/stacks/bns/bns.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,3 @@ export function useCurrentAccountNames() {
},
});
}

export function useGetAccountNamesByAddressQuery(address: string) {
return useGetBnsNamesOwnedByAddress(address, { select: resp => resp.names ?? [] });
}
6 changes: 3 additions & 3 deletions src/app/ui/components/account/account.card.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function AccountCard() {
switchAccount={<></>}
toggleSwitchAccount={() => null}
isLoadingBalance={false}
isLoadingBnsName={false}
isFetchingBnsName={false}
>
<Flex justify="space-between">
<IconButton icon={<ArrowUpIcon />} label="Send" />
Expand All @@ -42,7 +42,7 @@ export function AccountCardLoading() {
switchAccount={<></>}
toggleSwitchAccount={() => null}
isLoadingBalance
isLoadingBnsName={false}
isFetchingBnsName={false}
>
<Flex justify="space-between">
<IconButton icon={<ArrowUpIcon />} label="Send" />
Expand All @@ -62,7 +62,7 @@ export function AccountCardBnsNameLoading() {
switchAccount={<></>}
toggleSwitchAccount={() => null}
isLoadingBalance={false}
isLoadingBnsName
isFetchingBnsName
>
<Flex justify="space-between">
<IconButton icon={<ArrowUpIcon />} label="Send" />
Expand Down
6 changes: 3 additions & 3 deletions src/app/ui/components/account/account.card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface AccountCardProps {
children: ReactNode;
switchAccount: ReactNode;
toggleSwitchAccount(): void;
isLoadingBnsName: boolean;
isFetchingBnsName: boolean;
isLoadingBalance: boolean;
}

Expand All @@ -24,7 +24,7 @@ export function AccountCard({
switchAccount,
toggleSwitchAccount,
children,
isLoadingBnsName,
isFetchingBnsName,
isLoadingBalance,
}: AccountCardProps) {
return (
Expand All @@ -44,7 +44,7 @@ export function AccountCard({
>
<Flex>
<AccountNameLayout
isLoading={isLoadingBnsName}
isLoading={isFetchingBnsName}
data-testid={SettingsSelectors.CurrentAccountDisplayName}
textStyle="label.01"
>
Expand Down
Loading