From c125c25c5cd3e1c1c678b2a4ca987b22ec4465be Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 10:56:38 +0200 Subject: [PATCH 1/2] fix(home): declined-payment banner and receipt quote the Available to spend tile's number (T-174) The tile follows the spend mode, so in Credit mode it counts the credit line. The banner under it and the "Not enough to pay" receipt read the backend's spendable figure, which counts cash only, and the two disagreed on one screen. Both now read the tile's own figure through useTileSpendable. While the tile is loading the banner waits; when the tile cannot say, the backend figure stands. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01RHaUoLe7mFtPNbjcA2pAk9 --- components/Activity/NotEnoughToPayCard.tsx | 5 +- components/Assets/__tests__/fixtures.ts | 1 + .../SpendMode/useSpendModeFigures.ts | 23 ++++++-- .../useDeclinedPaymentPrompt.test.tsx | 57 +++++++++++++++++++ hooks/useDeclinedPaymentPrompt.ts | 10 +++- hooks/useTileSpendable.ts | 35 ++++++++++++ lib/utils/__tests__/cardCurrency.test.ts | 51 +++++++++++++++++ lib/utils/cardCurrency.ts | 38 +++++++++++++ 8 files changed, 210 insertions(+), 10 deletions(-) create mode 100644 hooks/useTileSpendable.ts diff --git a/components/Activity/NotEnoughToPayCard.tsx b/components/Activity/NotEnoughToPayCard.tsx index d4d1c9295..dbf301630 100644 --- a/components/Activity/NotEnoughToPayCard.tsx +++ b/components/Activity/NotEnoughToPayCard.tsx @@ -12,8 +12,8 @@ import { USDC_STARGATE } from '@/constants/addresses'; import { SWAP_MODAL } from '@/constants/modals'; import { path } from '@/constants/path'; import { TRACKING_EVENTS } from '@/constants/tracking-events'; -import { useCardSpendable } from '@/hooks/useCardSpendable'; import { useEuroSpendEnablement } from '@/hooks/useEuroSpendEnablement'; +import { useTileSpendable } from '@/hooks/useTileSpendable'; import { useUnreachableBaseBalance } from '@/hooks/useUnreachableBaseBalance'; import { useUnusableEurc } from '@/hooks/useUnusableEurc'; import { track } from '@/lib/analytics'; @@ -80,7 +80,8 @@ const NotEnoughToPayCard = ({ transaction }: NotEnoughToPayCardProps) => { // cardholder who is done. Once the read lands, the receipt says what that balance says. const [turnedOnBase, setTurnedOnBase] = useState(false); - const { data: spendable, refetch: refetchSpendable } = useCardSpendable({ + // The home tile's number, as the banner that opens this receipt quotes it. + const { data: spendable, refetch: refetchSpendable } = useTileSpendable({ refetchInterval: covered ? false : UNCOVERED_POLL_MS, }); const { eurc } = useUnusableEurc(); diff --git a/components/Assets/__tests__/fixtures.ts b/components/Assets/__tests__/fixtures.ts index 478c7fd1a..c247dd735 100644 --- a/components/Assets/__tests__/fixtures.ts +++ b/components/Assets/__tests__/fixtures.ts @@ -100,6 +100,7 @@ export const spendFixture: SpendModeFigures = { borrowApy: '5.57%', borrowedProgress: 0.175, segmentValue: { cash: '$228.40', credit: '$1,650.00', smart: '$1,650.00' }, + availableUsd: 1650, hasPosition: true, canBorrow: true, showsBorrowPosition: true, diff --git a/components/Card/NewCardDetails/SpendMode/useSpendModeFigures.ts b/components/Card/NewCardDetails/SpendMode/useSpendModeFigures.ts index 3d1fbf040..e9c104c18 100644 --- a/components/Card/NewCardDetails/SpendMode/useSpendModeFigures.ts +++ b/components/Card/NewCardDetails/SpendMode/useSpendModeFigures.ts @@ -52,6 +52,14 @@ export interface SpendModeFigures { /** The figure under each segment name in the control. */ segmentValue: Record; + /** + * What the home Available to spend tile shows for the mode in force, in dollars, before it is + * formatted: the cash balance in Cash, the credit left in Credit, the larger of the two in Smart. + * + * For anything that has to quote the tile's number rather than work out one of its own (the + * declined-payment banner and receipt). Null while the tile is loading or reads "—". + */ + availableUsd: number | null; /** Whether there is a loan at all. What gates the Repay button. */ hasPosition: boolean; @@ -172,6 +180,13 @@ export const useSpendModeFigures = (): SpendModeFigures => { // The tile's own placeholder, on the figures that count Base and nowhere else: `isLoading` // also holds the Assets screen's Fuse-only credit line, which has nothing to wait for. const unknownWithoutBase = isBaseCreditUnavailable ? '—' : undefined; + const isLoading = + isRegistrationLoading || isBalanceLoading || isBaseAccessLoading || isBaseCreditLoading; + // The tile's figure for the mode in force, by the same rules as the labels below. + const smartMicro = cashMicro > spendableHeadroom ? cashMicro : spendableHeadroom; + const availableMicro = + mode === 'cash' ? cashMicro : mode === 'credit' ? spendableHeadroom : smartMicro; + const isAvailableKnown = !isLoading && (mode === 'cash' || !unknownWithoutBase); return { mode, @@ -201,10 +216,9 @@ export const useSpendModeFigures = (): SpendModeFigures => { // takes exactly one path, so the most Smart can fund is the larger of the two; the // two figures also overlap heavily, both deriving from the same balance, so adding // them would quote money the Safe does not have. - smart: - unknownWithoutBase ?? - formatMicroUsd(cashMicro > spendableHeadroom ? cashMicro : spendableHeadroom), + smart: unknownWithoutBase ?? formatMicroUsd(smartMicro), }, + availableUsd: isAvailableKnown ? Number(availableMicro) / Number(ONE_USD) : null, hasPosition: debt > 0n, canBorrow, @@ -215,8 +229,7 @@ export const useSpendModeFigures = (): SpendModeFigures => { fullyPriced: position?.fullyPriced ?? true, // Base too, so the tile doesn't land on the Fuse figure and then jump to the whole line. - isLoading: - isRegistrationLoading || isBalanceLoading || isBaseAccessLoading || isBaseCreditLoading, + isLoading, }; }, [ mode, diff --git a/hooks/__tests__/useDeclinedPaymentPrompt.test.tsx b/hooks/__tests__/useDeclinedPaymentPrompt.test.tsx index 2bb12a115..652d49b00 100644 --- a/hooks/__tests__/useDeclinedPaymentPrompt.test.tsx +++ b/hooks/__tests__/useDeclinedPaymentPrompt.test.tsx @@ -13,6 +13,13 @@ jest.mock('@/hooks/useCardTransactions', () => ({ jest.mock('@/hooks/useCardSpendable', () => ({ useCardSpendable: () => ({ data: mockSpendable }), })); +let mockTile: { availableUsd: number | null; isLoading: boolean }; +jest.mock('@/components/Card/NewCardDetails/SpendMode/useSpendModeFigures', () => ({ + useSpendModeFigures: () => mockTile, +})); +jest.mock('@/hooks/useMainCurrency', () => ({ + useCardDisplayCurrency: () => ({ inEuros: false, toDisplayAmount: (usd: number) => usd }), +})); jest.mock('@/store/useHomePromptStore', () => ({ useHomePromptStore: (select: (state: any) => any) => select({ dismissedDeclines: {} }), })); @@ -44,6 +51,7 @@ describe('useDeclinedPaymentPrompt', () => { beforeEach(() => { jest.useFakeTimers({ now: NOW }); mockSpendable = { available: { amount: '0', currency: 'usd' } }; + mockTile = { availableUsd: 0, isLoading: false }; }); afterEach(() => { @@ -76,4 +84,53 @@ describe('useDeclinedPaymentPrompt', () => { expect(prompt).toBeNull(); }); + + describe("the Available to spend tile's number (T-174)", () => { + beforeEach(() => { + mockTransactions = { pages: [{ data: [declined(new Date(NOW.getTime() - 2 * HOUR_MS))] }] }; + // Cash only, as the backend counts it. + mockSpendable = { available: { amount: '5', currency: 'usd' } }; + }); + + it('is what the banner quotes, not the cash-only figure', () => { + // Credit mode: the tile counts the credit line. + mockTile = { availableUsd: 12.5, isLoading: false }; + + act(() => { + root = create(); + }); + + expect(prompt?.available).toEqual({ amount: 12.5, currency: 'USD' }); + }); + + it('hides the banner once it covers the payment', () => { + mockTile = { availableUsd: 20, isLoading: false }; + + act(() => { + root = create(); + }); + + expect(prompt).toBeNull(); + }); + + it('waits for the tile rather than show one number and then another', () => { + mockTile = { availableUsd: null, isLoading: true }; + + act(() => { + root = create(); + }); + + expect(prompt).toBeNull(); + }); + + it('falls back to the cash figure when the tile cannot say', () => { + mockTile = { availableUsd: null, isLoading: false }; + + act(() => { + root = create(); + }); + + expect(prompt?.available).toEqual({ amount: 5, currency: 'USD' }); + }); + }); }); diff --git a/hooks/useDeclinedPaymentPrompt.ts b/hooks/useDeclinedPaymentPrompt.ts index e420fd5aa..11d52d920 100644 --- a/hooks/useDeclinedPaymentPrompt.ts +++ b/hooks/useDeclinedPaymentPrompt.ts @@ -1,7 +1,7 @@ import { useEffect, useMemo, useState } from 'react'; -import { useCardSpendable } from '@/hooks/useCardSpendable'; import { useCardTransactions } from '@/hooks/useCardTransactions'; +import { useTileSpendable } from '@/hooks/useTileSpendable'; import { CardTransaction, CardTransactionCategory } from '@/lib/types'; import { declinedPaymentFigures, Money } from '@/lib/utils/cardCurrency'; import { isInsufficientDecline } from '@/lib/utils/cardDeclineReason'; @@ -15,7 +15,10 @@ export interface DeclinedPaymentPrompt { /** Card transaction id — what the banner's "Fix it" opens. */ transactionId: string; payment: Money; - /** What the card can spend now, live — less than {@link payment}, or there is no banner. */ + /** + * What the card can spend now, live — the Available to spend tile's own number. Less than + * {@link payment}, or there is no banner. + */ available: Money; merchantName?: string; } @@ -44,7 +47,8 @@ export const useDeclinedPaymentPrompt = (enabled: boolean): DeclinedPaymentPromp const dismissedDeclines = useHomePromptStore(state => state.dismissedDeclines); // Same query keys the wallet screen already reads, so these are cache hits. const { data: transactions } = useCardTransactions({ enabled }); - const { data: spendable } = useCardSpendable({ enabled }); + // The tile's number, not the backend's cash-only one: the banner sits right under the tile. + const { data: spendable } = useTileSpendable({ enabled }); // When the decline on show left its day, set the moment it does. Home can stay mounted for // longer than that with nothing refetching, and the banner must not outlive its window just // because the data it was picked from did not change. diff --git a/hooks/useTileSpendable.ts b/hooks/useTileSpendable.ts new file mode 100644 index 000000000..863d3d4bd --- /dev/null +++ b/hooks/useTileSpendable.ts @@ -0,0 +1,35 @@ +import { useMemo } from 'react'; + +import { useSpendModeFigures } from '@/components/Card/NewCardDetails/SpendMode/useSpendModeFigures'; +import { useCardSpendable } from '@/hooks/useCardSpendable'; +import { useCardDisplayCurrency } from '@/hooks/useMainCurrency'; +import { withTileAvailable } from '@/lib/utils/cardCurrency'; + +/** + * {@link useCardSpendable} with the home Available to spend tile's number as what is available. + * + * What the declined-payment banner and its receipt read, so they say the same figure as the tile + * above them. The backend's own figure counts cash only, and in Credit mode the tile counts the + * credit line. See `withTileAvailable`. + * + * Nothing while the tile is still loading, so the banner does not show one number and then + * another. When the tile cannot say (it reads "—"), the backend's figure stands. + */ +export const useTileSpendable = (options: Parameters[0] = {}) => { + const { data: spendable, refetch } = useCardSpendable(options); + const { availableUsd, isLoading } = useSpendModeFigures(); + const { inEuros, toDisplayAmount } = useCardDisplayCurrency(); + + const data = useMemo(() => { + if (!spendable) return undefined; + if (availableUsd === null) return isLoading ? undefined : spendable; + return withTileAvailable(spendable, { + usd: availableUsd, + eur: inEuros ? toDisplayAmount(availableUsd) : undefined, + }); + }, [spendable, availableUsd, isLoading, inEuros, toDisplayAmount]); + + return { data, refetch }; +}; + +export default useTileSpendable; diff --git a/lib/utils/__tests__/cardCurrency.test.ts b/lib/utils/__tests__/cardCurrency.test.ts index db33a108a..7860618e7 100644 --- a/lib/utils/__tests__/cardCurrency.test.ts +++ b/lib/utils/__tests__/cardCurrency.test.ts @@ -9,6 +9,7 @@ import { shortfallOf, spendableDisplay, usdToEur, + withTileAvailable, } from '@/lib/utils/cardCurrency'; const EUR_SPENDABLE = { @@ -277,3 +278,53 @@ describe('euro main currency', () => { expect(formatEur(1000)).toBe('€1,000'); }); }); + +describe('withTileAvailable', () => { + // T-174: the banner said €10.02 under a tile reading €36.54. The backend counts cash only; in + // Credit mode the tile counts the credit line. + const CASH_ONLY = { + available: { amount: '11.38', currency: 'usd' as const }, + available_in_card_currency: { ...EUR_SPENDABLE.available_in_card_currency, amount: '10.02' }, + }; + + it("puts the tile's euro figure in place of the backend's, to the cent", () => { + const onTile = withTileAvailable(CASH_ONLY, { usd: 41.5, eur: 36.54 }); + + expect(onTile.available).toEqual({ amount: '41.5', currency: 'usd' }); + expect(onTile.available_in_card_currency).toEqual({ + ...CASH_ONLY.available_in_card_currency, + amount: '36.54', + }); + }); + + it("makes the declined receipt quote the tile's number", () => { + const figures = declinedPaymentFigures( + { + amount: '149', + currency: 'ILS', + spend_details: { card_amount: '39.80', card_currency: 'EUR' }, + }, + withTileAvailable(CASH_ONLY, { usd: 41.5, eur: 36.54 }), + ); + + expect(figures.available).toEqual({ amount: 36.54, currency: 'EUR' }); + expect(figures.comparable).toMatchObject({ payment: 39.8, available: 36.54, currency: 'EUR' }); + }); + + it("converts at the card's quote, rounded down, when the tile shows dollars", () => { + // 41.5 / 1.1358 = 36.538… + const onTile = withTileAvailable(CASH_ONLY, { usd: 41.5 }); + + expect(onTile.available_in_card_currency?.amount).toBe('36.53'); + }); + + it('stays in dollars when there is no card-currency quote', () => { + const onTile = withTileAvailable( + { available: { amount: '11.38', currency: 'usd' } }, + { usd: 41.5, eur: 36.54 }, + ); + + expect(onTile.available).toEqual({ amount: '41.5', currency: 'usd' }); + expect(onTile.available_in_card_currency).toBeUndefined(); + }); +}); diff --git a/lib/utils/cardCurrency.ts b/lib/utils/cardCurrency.ts index 9342a470b..98c86dae8 100644 --- a/lib/utils/cardCurrency.ts +++ b/lib/utils/cardCurrency.ts @@ -220,6 +220,44 @@ export const declinedPaymentFigures = ( return { payment, available }; }; +/** + * The backend's spendable figures with the home Available to spend tile's number in their place. + * + * The backend counts cash only. The tile follows the spend mode, so in Credit it counts the credit + * line too — and the declined-payment banner under it said "You have €10.02 available to spend" + * beside a tile reading €36.54. The banner and the receipt take this, so all three quote one + * number. + * + * `usd` is the tile's figure in dollars. `eur` is what the tile shows when it shows euros: used as + * it is for a euro card, so the two match to the cent; otherwise the dollars are converted at the + * card's own quote and rounded down, as the backend's figure is. The quote itself is kept. + */ +export const withTileAvailable = ( + spendable: CardSpendableResponse, + tile: { usd: number; eur?: number }, +): CardSpendableResponse => { + const usd = Math.max(0, tile.usd); + const inCard = spendable.available_in_card_currency; + const rate = inCard?.rate; + const isEuroCard = inCard?.currency?.trim().toUpperCase() === 'EUR'; + const inCardAmount = + isEuroCard && tile.eur !== undefined + ? Math.max(0, tile.eur) + : rate && rate > 0 + ? usdToEur(usd, rate) + : undefined; + + return { + ...spendable, + available: { amount: String(usd), currency: 'usd' }, + // Without a usable quote there is no figure in the card's currency to give: dollars it is. + available_in_card_currency: + inCard && inCardAmount !== undefined + ? { ...inCard, amount: inCardAmount.toFixed(2) } + : undefined, + }; +}; + /** Up to the cent: the gap is never under-stated. */ export const shortfallOf = (comparable: NonNullable) => Math.max( From ed0be37c250641e97472321d81c043f15c6fe982 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 14:57:28 +0200 Subject: [PATCH 2/2] fix(home): declined-payment figure survives a failed cash read and refreshes with the receipt's poll (T-174) - When the backend cash read failed, the banner and receipt still quote the Available to spend tile's number (in dollars) instead of hiding. While that read is still loading they wait, so the figure does not switch currency. - The receipt's poll and its refresh now also re-read the token balances the tile's cash figure comes from, so a top-up shows at the poll's pace. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01RHaUoP1xib2rgMcqyuXiqD --- .../useDeclinedPaymentPrompt.test.tsx | 63 ++++++++++++++++++- hooks/useBalances.ts | 25 ++++++++ hooks/useDeclineSpendable.ts | 34 +++++++--- 3 files changed, 113 insertions(+), 9 deletions(-) diff --git a/hooks/__tests__/useDeclinedPaymentPrompt.test.tsx b/hooks/__tests__/useDeclinedPaymentPrompt.test.tsx index b16a3156e..2813ac72b 100644 --- a/hooks/__tests__/useDeclinedPaymentPrompt.test.tsx +++ b/hooks/__tests__/useDeclinedPaymentPrompt.test.tsx @@ -1,6 +1,7 @@ import React from 'react'; import { DeclinedPaymentPrompt, useDeclinedPaymentPrompt } from '@/hooks/useDeclinedPaymentPrompt'; +import { useDeclineSpendable } from '@/hooks/useDeclineSpendable'; import { CardTransactionCategory } from '@/lib/types'; const { act, create } = jest.requireActual('react-test-renderer'); @@ -13,8 +14,22 @@ let mockDisplayCurrency: any; jest.mock('@/hooks/useCardTransactions', () => ({ useCardTransactions: () => ({ data: mockTransactions }), })); +let mockSpendableLoading: boolean; +const mockRefetchSpendable = jest.fn(); jest.mock('@/hooks/useCardSpendable', () => ({ - useCardSpendable: () => ({ data: mockSpendable }), + useCardSpendable: () => ({ + data: mockSpendable, + isLoading: mockSpendableLoading, + refetch: mockRefetchSpendable, + }), +})); +let mockBalancesRefresh: any; +const mockRefetchBalances = jest.fn(); +jest.mock('@/hooks/useBalances', () => ({ + useBalancesRefresh: (options: any) => { + mockBalancesRefresh = options; + return { refetch: mockRefetchBalances }; + }, })); let mockTile: { availableUsd: number | null; isLoading: boolean }; jest.mock('@/components/Card/NewCardDetails/SpendMode/useSpendModeFigures', () => ({ @@ -63,6 +78,9 @@ describe('useDeclinedPaymentPrompt', () => { beforeEach(() => { jest.useFakeTimers({ now: NOW }); mockSpendable = { available: { amount: '0', currency: 'usd' } }; + mockSpendableLoading = false; + mockRefetchSpendable.mockReset(); + mockRefetchBalances.mockReset(); mockRegistration = { registration: {}, mode: 'cash', isRegistered: true }; mockBaseCredit = { base: {}, readsBase: false, isBaseGateReady: true }; mockDisplayCurrency = { inEuros: false }; @@ -239,6 +257,49 @@ describe('useDeclinedPaymentPrompt', () => { expect(prompt).toBeNull(); }); + it('still quotes the tile, in dollars, when the backend cash read failed', () => { + mockSpendable = undefined; + mockTile = { availableUsd: 12.5, isLoading: false }; + + act(() => { + root = create(); + }); + + expect(prompt?.available).toEqual({ amount: 12.5, currency: 'USD' }); + }); + + it('waits for the backend cash read while it is still loading', () => { + mockSpendable = undefined; + mockSpendableLoading = true; + mockTile = { availableUsd: 12.5, isLoading: false }; + + act(() => { + root = create(); + }); + + expect(prompt).toBeNull(); + }); + + it("re-reads the tile's balances with the receipt's poll and its refresh", async () => { + let refetchCash: (() => Promise) | undefined; + const Receipt = () => { + const { refetchCash: current } = useDeclineSpendable({ refetchInterval: 15_000 }); + React.useEffect(() => { + refetchCash = current; + }, [current]); + return null; + }; + + act(() => { + root = create(); + }); + expect(mockBalancesRefresh).toEqual({ enabled: true, refetchInterval: 15_000 }); + + await refetchCash?.(); + expect(mockRefetchSpendable).toHaveBeenCalledTimes(1); + expect(mockRefetchBalances).toHaveBeenCalledTimes(1); + }); + it('falls back to the cash figure when the tile cannot say', () => { mockTile = { availableUsd: null, isLoading: false }; diff --git a/hooks/useBalances.ts b/hooks/useBalances.ts index cc3cc12db..cefbe98f7 100644 --- a/hooks/useBalances.ts +++ b/hooks/useBalances.ts @@ -1222,3 +1222,28 @@ export const useBalancesPolling = (): void => { notifyOnChangeProps: [], }); }; + +/** + * A faster re-read of the same balances for a screen that is waiting on a top-up: the declined + * receipt, whose Cash figure is the Available to spend tile's and so comes from these balances. + * The app-wide timer above can be a minute apart; this one runs at the screen's own pace, and only + * while that screen asks. Renders nothing. `refetch` reads them at once. + */ +export const useBalancesRefresh = ({ + enabled = true, + refetchInterval, +}: { enabled?: boolean; refetchInterval?: number } = {}) => { + const { user } = useUser(); + const options = tokenBalancesQueryOptions(user?.safeAddress); + + const { refetch } = useQuery({ + ...options, + ...balancesObserverOptions, + enabled: options.enabled && enabled && !!refetchInterval, + refetchInterval, + refetchIntervalInBackground: false, + notifyOnChangeProps: [], + }); + + return { refetch }; +}; diff --git a/hooks/useDeclineSpendable.ts b/hooks/useDeclineSpendable.ts index 3841fe961..92d9e949a 100644 --- a/hooks/useDeclineSpendable.ts +++ b/hooks/useDeclineSpendable.ts @@ -2,13 +2,14 @@ import { useCallback } from 'react'; import { useSpendModeFigures } from '@/components/Card/NewCardDetails/SpendMode/useSpendModeFigures'; import { ONE_USD } from '@/constants/cardSpendModule'; +import { useBalancesRefresh } from '@/hooks/useBalances'; import { useCardCreditPositions } from '@/hooks/useCardCreditPositions'; import { useCardSpendable } from '@/hooks/useCardSpendable'; import { useCardSpendRegistration } from '@/hooks/useCardSpendRegistration'; import { useCardDisplayCurrency } from '@/hooks/useMainCurrency'; import { declinedPaymentFigures, withTileAvailable } from '@/lib/utils/cardCurrency'; -import type { CardTransaction } from '@/lib/types'; +import type { CardSpendableResponse, CardTransaction } from '@/lib/types'; /** * Live spending power for the declined receipt and its Home banner. @@ -17,6 +18,9 @@ import type { CardTransaction } from '@/lib/types'; * and the receipt it opens quote the figure the tile shows — and neither says a payment is short * when the tile covers it. Nothing while the tile is still loading, so they do not show one number * and then another. When the tile cannot say (it reads "—"), the figures worked out here stand. + * + * The tile's cash comes from the token balances, not from the backend's spendable read, so the + * poll and `refetchCash` re-read both: a top-up shows on the receipt at the poll's pace. */ export const useDeclineSpendable = ({ enabled = true, @@ -26,7 +30,16 @@ export const useDeclineSpendable = ({ useCardSpendRegistration({ refetchInterval: enabled ? refetchInterval : undefined, }); - const { data: cash, refetch: refetchCash } = useCardSpendable({ enabled, refetchInterval }); + const { + data: cash, + isLoading: isCashLoading, + refetch: refetchSpendable, + } = useCardSpendable({ enabled, refetchInterval }); + const { refetch: refetchBalances } = useBalancesRefresh({ enabled, refetchInterval }); + const refetchCash = useCallback( + () => Promise.all([refetchSpendable(), refetchBalances()]), + [refetchSpendable, refetchBalances], + ); const hasMode = enabled && !!registration; const { base, readsBase, isBaseGateReady } = useCardCreditPositions({ enabled: hasMode && mode !== 'cash', @@ -56,12 +69,16 @@ export const useDeclineSpendable = ({ return declinedPaymentFigures(transaction, cash, { mode, availableUsd, displayUsdPerEur }); } if (!hasMode || mode === 'cash') { - const tileCash = - cash && - withTileAvailable(cash, { - usd: tileUsd, - eur: inEuros ? toDisplayAmount(tileUsd) : undefined, - }); + // Still being read: wait for the card's own quote rather than show dollars and then euros. + if (!cash && isCashLoading) return declinedPaymentFigures(transaction); + // The read failed: the tile's number still stands, in dollars, with no card quote. + const known: CardSpendableResponse = cash ?? { + available: { amount: '0', currency: 'usd' }, + }; + const tileCash = withTileAvailable(known, { + usd: tileUsd, + eur: inEuros ? toDisplayAmount(tileUsd) : undefined, + }); return declinedPaymentFigures(transaction, tileCash); } return declinedPaymentFigures(transaction, cash, { @@ -77,6 +94,7 @@ export const useDeclineSpendable = ({ isLoading, mode, cash, + isCashLoading, availableUsd, inEuros, usdPerEur,