diff --git a/client/src/components/Budget/useCostsLedger.test.ts b/client/src/components/Budget/useCostsLedger.test.ts
index f0b6db478..f8c06efa8 100644
--- a/client/src/components/Budget/useCostsLedger.test.ts
+++ b/client/src/components/Budget/useCostsLedger.test.ts
@@ -16,7 +16,7 @@ import type { CostsSettlementResponse } from './costsModel';
import { useCostsLedger } from './useCostsLedger';
import { useFreezeMissingRates } from './useFreezeMissingRates';
-// FE-BUDGET-LEDGER-001 to FE-BUDGET-LEDGER-022
+// FE-BUDGET-LEDGER-001 to FE-BUDGET-LEDGER-023
const fx = vi.hoisted(() => ({
// One stable converter, the way useExchangeRates hands out a memoised one: USD is half a euro.
@@ -394,24 +394,52 @@ describe('useCostsLedger: another currency for this trip (#1683)', () => {
expect(createMock()).not.toHaveBeenCalled();
localStorage.removeItem('trek:costs-currency:7');
});
- it('FE-BUDGET-LEDGER-022: the owe and owed cards count only flows read in the currency shown', async () => {
+ it('FE-BUDGET-LEDGER-022: the owe and owed cards count flows in the currency shown, and are unknown otherwise', async () => {
localStorage.removeItem('trek:costs-currency:7');
const { result } = setup();
await waitFor(() => expect(result.current.totals.owe).toBe(15));
expect(result.current.totals.owed).toBe(5);
+ expect(result.current.balancesKnown).toBe(true);
settlementMock().mockRejectedValueOnce(new Error('Settlement not available offline'));
act(() => result.current.setBase('JPY'));
await waitFor(() => expect(result.current.settlementError).toBe(true));
- expect(result.current.totals.owe).toBe(0);
- expect(result.current.totals.owed).toBe(0);
+ expect(result.current.balancesKnown).toBe(false);
+ // Flows answered in the trip currency go over at the rate the read sent along.
settlementMock().mockResolvedValueOnce({ ...SETTLEMENT, currency: 'EUR' });
act(() => result.current.loadSettlement());
await waitFor(() => expect(result.current.settlementError).toBe(false));
- expect(result.current.totals.owe).toBe(0);
+ expect(result.current.balancesKnown).toBe(true);
+ expect(result.current.totals.owe).toBe(17);
+ expect(result.current.totals.owed).toBe(6);
+ expect(result.current.canSettle).toBe(false);
settlementMock().mockResolvedValueOnce({ ...SETTLEMENT, currency: 'JPY' });
act(() => result.current.loadSettlement());
await waitFor(() => expect(result.current.totals.owe).toBe(15));
expect(result.current.totals.owed).toBe(5);
+ expect(result.current.canSettle).toBe(true);
+ localStorage.removeItem('trek:costs-currency:7');
+ });
+
+ it('FE-BUDGET-LEDGER-023: the balances are unknown while the first read runs and in a currency with no rate', async () => {
+ localStorage.removeItem('trek:costs-currency:7');
+ let answer: (s: CostsSettlementResponse) => void = () => undefined;
+ settlementMock().mockImplementationOnce(
+ () =>
+ new Promise((resolve) => {
+ answer = resolve;
+ })
+ );
+ const { result } = setup();
+ await waitFor(() => expect(settlementMock()).toHaveBeenCalledTimes(1));
+ expect(result.current.balancesKnown).toBe(false);
+ await act(async () => answer(SETTLEMENT));
+ await waitFor(() => expect(result.current.balancesKnown).toBe(true));
+ // An answer in neither the trip nor the display currency cannot be converted.
+ settlementMock().mockResolvedValueOnce({ ...SETTLEMENT, currency: 'GBP' });
+ act(() => result.current.setBase('JPY'));
+ await waitFor(() => expect(result.current.settlement).toEqual({ ...SETTLEMENT, currency: 'GBP' }));
+ expect(result.current.balancesKnown).toBe(false);
+ expect(result.current.totals.owe).toBe(0);
localStorage.removeItem('trek:costs-currency:7');
});
});
diff --git a/client/src/components/Budget/useCostsLedger.ts b/client/src/components/Budget/useCostsLedger.ts
index 2911e9364..c0c4c0d28 100644
--- a/client/src/components/Budget/useCostsLedger.ts
+++ b/client/src/components/Budget/useCostsLedger.ts
@@ -11,6 +11,7 @@ import type { TripStoreState } from '../../store/tripStore';
import { useServerPing } from '../../sync/useServerPing';
import type { BudgetItem, Trip } from '../../types';
import { downloadBlob } from '../../utils/fileDownload';
+import { currencyDecimals } from '../../utils/formatters';
import { localToday } from '../Planner/today';
import { COST_CAT_META } from './costsCategories';
import {
@@ -226,12 +227,22 @@ export function useCostsLedger({
const activeSegment = splitting ? segment : 'all';
// Names for people the expenses still name after they left the trip (#2604).
const knownNames = useMemo(() => knownPersonNames(budgetItems, settlement), [budgetItems, settlement]);
+ // The owe and owed cards print in the display currency. Flows the server answered in
+ // the trip currency go over at the rate the read sent along; while a read runs, after
+ // one failed or in any other currency the balances are unknown, which is not zero.
+ const shownFlows = useMemo(() => {
+ if (settlementError || !settlement) return null;
+ if (settledIn === base) return flows;
+ if (settledIn !== tripCurrency || displayPerTrip == null) return null;
+ const unit = 10 ** currencyDecimals(base);
+ return flows.map((f) => ({ ...f, amount: Math.round(f.amount * displayPerTrip * unit) / unit }));
+ }, [settlementError, settlement, settledIn, base, tripCurrency, displayPerTrip, flows]);
+ const balancesKnown = shownFlows != null;
// `ctx` carries the rates. They can land after the expenses, and without it in the
// deps the cards kept the sums they were first added up with while the rows moved on.
- // The owe and owed cards print in the display currency too, so they count the same flows.
const totals = useMemo(
- () => computeTotals(budgetItems, flowsInBase ? flows : [], ctx, shareOf, leftOutCats),
- [budgetItems, flowsInBase, flows, ctx, shareOf, leftOutCats]
+ () => computeTotals(budgetItems, shownFlows ?? [], ctx, shareOf, leftOutCats),
+ [budgetItems, shownFlows, ctx, shareOf, leftOutCats]
);
const filtered = useMemo(
() =>
@@ -355,6 +366,7 @@ export function useCostsLedger({
settlementError,
loadSettlement,
flows,
+ balancesKnown,
canSettle,
splitting,
knownNames,
diff --git a/client/src/mobile/screens/trip/tabs/MCostsTab.tsx b/client/src/mobile/screens/trip/tabs/MCostsTab.tsx
index 216e75d91..f0d1a8282 100644
--- a/client/src/mobile/screens/trip/tabs/MCostsTab.tsx
+++ b/client/src/mobile/screens/trip/tabs/MCostsTab.tsx
@@ -11,6 +11,7 @@ import { openFile } from '../../../../utils/fileDownload'
import MCostSheet from '../sheets/MCostSheet'
import { ReceiptPreviewModal } from '../../../../components/Budget/ReceiptPreviewModal'
import { useCostsLedger } from '../../../../components/Budget/useCostsLedger'
+import { UNKNOWN_AMOUNT } from '../../../../components/Budget/CostsSummaryCard'
import { useExpenseLinks } from '../../../../components/Budget/useExpenseLinks'
import { useCostsMode } from '../../../../components/Budget/useCostsMode'
import { CostsCurrencyPicker } from '../../../../components/Budget/CostsCurrencyPicker'
@@ -48,7 +49,7 @@ export default function MCostsTab({ planner, shell }: MTabScreenProps) {
// The display and trip currencies, the server-computed settlement, totals,
// filters and the ledger writes: the same ledger the desktop panel runs on.
const {
- me, base, tripCurrency, ctx, settlement, settlementError, loadSettlement, flows, splitting, knownNames,
+ me, base, tripCurrency, ctx, settlement, settlementError, loadSettlement, flows, balancesKnown, splitting, knownNames,
search, setSearch, segment, setSegment, catFilter, setCatFilter, dayFilter, setDayFilter,
totals, filtered, filteredSettlements, catBreakdown, leftOutLabel, toggleLeftOutCat, countAllCats, exportCsv: handleExportCsv,
deleteExpense, undoSettlement: handleUndoSettlement, setBase, baseOptions,
@@ -170,8 +171,8 @@ export default function MCostsTab({ planner, shell }: MTabScreenProps) {
{t('costs.youOwe')}
{t('costs.youOweSub')}
-
0.5 ? STATUS_COLOR.danger : 'var(--m-ink)' }}>
- {formatMoney(totals.owe, base, locale)}
+
0.5 ? STATUS_COLOR.danger : 'var(--m-ink)' }}>
+ {balancesKnown ? formatMoney(totals.owe, base, locale) : UNKNOWN_AMOUNT}
@@ -180,8 +181,8 @@ export default function MCostsTab({ planner, shell }: MTabScreenProps) {
{t('costs.youreOwed')}
{t('costs.youreOwedSub')}
-
0.5 ? STATUS_COLOR.confirmed : 'var(--m-ink)' }}>
- {formatMoney(totals.owed, base, locale)}
+
0.5 ? STATUS_COLOR.confirmed : 'var(--m-ink)' }}>
+ {balancesKnown ? formatMoney(totals.owed, base, locale) : UNKNOWN_AMOUNT}