Skip to content

Commit

Permalink
Refactor useSendAccountBalance. Prefe isLoading to isPending
Browse files Browse the repository at this point in the history
  • Loading branch information
youngkidwarrior committed Aug 2, 2024
1 parent 959101c commit bb8291a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
6 changes: 3 additions & 3 deletions packages/app/features/home/screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ function HomeSearchBar() {
export function HomeScreen() {
const media = useMedia()
const [queryParams] = useRootScreenParams()
const { data: sendAccount, isLoading: sendAccountLoading } = useSendAccount()
const { data: sendAccount, isLoading: isSendAccountLoading } = useSendAccount()
const { search } = queryParams
const { balances } = useSendAccountBalances()
const { balances, isLoading: isBalanceLoading } = useSendAccountBalances()
const usdcBalance = balances?.[0]?.result
const canSend = !!sendAccount && usdcBalance && usdcBalance > 0n

Expand All @@ -169,7 +169,7 @@ export function HomeScreen() {
<AnimatePresence>
{(() => {
switch (true) {
case sendAccountLoading:
case isSendAccountLoading || isBalanceLoading:
return (
<Stack f={1} h={'100%'} ai={'center'} jc={'center'}>
<Spinner size="large" />
Expand Down
15 changes: 7 additions & 8 deletions packages/app/utils/useSendAccountBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
usdcAbi,
} from '@my/wagmi'
import { useBalance, useReadContracts } from 'wagmi'
import { useSendAccounts } from './send-accounts'
import { useSendAccount } from './send-accounts'
import { useTokenPrices } from './useTokenPrices'

const usdcBaseContract = {
Expand All @@ -23,10 +23,9 @@ const sendBaseContract = {

export const useSendAccountBalances = () => {
const { data: tokenPrices } = useTokenPrices()
const { data: sendAccounts } = useSendAccounts()
const sendAccount = sendAccounts?.[0]
const { data: sendAccount } = useSendAccount()

const { data: tokenBalances, isPending: isPendingTokenBalances } = useReadContracts({
const { data: tokenBalances, isPending: isLoadingTokenBalances } = useReadContracts({
query: { enabled: !!sendAccount },
contracts: [
{
Expand All @@ -42,14 +41,14 @@ export const useSendAccountBalances = () => {
],
})

const { data: ethBalanceOnBase, isPending: isPendingEthBalanceOnBase } = useBalance({
const { data: ethBalanceOnBase, isLoading: isLoadingEthBalanceOnBase } = useBalance({
address: sendAccount?.address,
query: { enabled: !!sendAccount },
chainId: baseMainnet.id,
})

const isPending = isPendingTokenBalances || isPendingEthBalanceOnBase
const balances = isPending
const isLoading = isLoadingTokenBalances || isLoadingEthBalanceOnBase
const balances = isLoading
? undefined
: {
eth: ethBalanceOnBase,
Expand All @@ -66,5 +65,5 @@ export const useSendAccountBalances = () => {
(Number(ethBalanceOnBase?.value ?? 0n) / 10 ** 18) * tokenPrices.ethereum.usd
const totalBalance = usdcBalanceInUsd + sendBalanceInUsd + ethBalanceInUsd

return { balances, totalBalance, isPending }
return { balances, totalBalance, isLoading }
}

0 comments on commit bb8291a

Please sign in to comment.