mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 21:33:19 +00:00
refactor(costs): let the summary card tell an unknown balance itself
Keeps the Costs panel within its size budget.
This commit is contained in:
@@ -1084,11 +1084,9 @@ describe('CostsPanel — settle up', () => {
|
||||
render(<CostsPanel tripId={1} tripMembers={tripMembers} />)
|
||||
|
||||
await screen.findByText('Balances')
|
||||
// Settle up, Balances, Final budget and the owe and owed cards all hang off the one failed request.
|
||||
await waitFor(() => expect(screen.getAllByText('Unknown error')).toHaveLength(5))
|
||||
expect(screen.queryByText("Everyone's square")).toBeNull()
|
||||
expect(screen.queryByText("You're all settled up")).toBeNull()
|
||||
expect(screen.queryByText('Nothing owed to you')).toBeNull()
|
||||
for (const gone of ["Everyone's square", "You're all settled up", 'Nothing owed to you'])
|
||||
expect(screen.queryByText(gone)).toBeNull()
|
||||
})
|
||||
|
||||
it('FE-W5COSTS-013: the "you owe" card lists who I still have to pay', async () => {
|
||||
|
||||
@@ -77,7 +77,7 @@ export default function CostsPanel({ tripId, tripMembers = [] }: CostsPanelProps
|
||||
me, base, tripCurrency, ctx, settlement, settlementError, loadSettlement, balancesKnown, knownNames, splitting,
|
||||
search, setSearch, segment: filter, setSegment: setFilter, catFilter, setCatFilter, dayFilter, setDayFilter,
|
||||
totals, filtered, filteredSettlements, catBreakdown, countedCount, leftOutLabel, toggleLeftOutCat, countAllCats, exportCsv: handleExportCsv,
|
||||
deleteExpense: handleDelete, undoSettlement, settleFlow, settleAll, canSettle, setBase, baseOptions,
|
||||
deleteExpense: handleDelete, undoSettlement, settleFlow, settleAll, canSettle, setBase, baseOptions, settledUp,
|
||||
} = useCostsLedger({
|
||||
tripId, trip, budgetItems, memberCount: tripMembers.length, actions: { loadBudgetItems, deleteBudgetItem }, canEdit, t, toast,
|
||||
settlementOnTripChange: true, skipUnfinishedShares: true, csvCurrencyAsStored: true,
|
||||
@@ -288,22 +288,20 @@ export default function CostsPanel({ tripId, tripMembers = [] }: CostsPanelProps
|
||||
{!splitting ? <div style={{ marginBottom: 36 }}>{totalCard}</div> : (
|
||||
<div style={{ display: 'grid', gridTemplateColumns: 'repeat(4, minmax(0, 1fr))', gap: 16, marginBottom: 36 }} className="costs-summary">
|
||||
<SummaryCard label={t('costs.youOwe')} sub={t('costs.youOweSub')} amount={balancesKnown ? totals.owe : null} currency={base} locale={locale}
|
||||
icon={<ArrowDown size={18} />} tone="owe"
|
||||
foot={!balancesKnown ? balancesUnknown()
|
||||
: totals.owe > 0.01
|
||||
icon={<ArrowDown size={18} />} tone="owe" failed={settlementError}
|
||||
foot={totals.owe > 0.01
|
||||
? <FlowPills ids={(settlement?.flows || []).filter(f => f.from.user_id === me).map(f => f.to.user_id)} lead={t('costs.to')} Avatar={Avatar} name={personName} />
|
||||
: <span className="text-content-faint">{t('costs.allSettled')}</span>} />
|
||||
<SummaryCard label={t('costs.youreOwed')} sub={t('costs.youreOwedSub')} amount={balancesKnown ? totals.owed : null} currency={base} locale={locale}
|
||||
icon={<ArrowUp size={18} />} tone="owed"
|
||||
foot={!balancesKnown ? balancesUnknown()
|
||||
: totals.owed > 0.01
|
||||
icon={<ArrowUp size={18} />} tone="owed" failed={settlementError}
|
||||
foot={totals.owed > 0.01
|
||||
? <FlowPills ids={(settlement?.flows || []).filter(f => f.to.user_id === me).map(f => f.from.user_id)} lead={t('costs.from')} Avatar={Avatar} name={personName} />
|
||||
: <span className="text-content-faint">{t('costs.nothingOwed')}</span>} />
|
||||
<SummaryCard label={t('costs.outstanding')} sub={t('costs.outstandingSub')} amount={totals.outstanding} currency={base} locale={locale}
|
||||
icon={<AlertCircle size={18} />} tone="unfinished"
|
||||
foot={totals.outstandingCount > 0
|
||||
? <span><b>{totals.outstandingCount}</b> {t('costs.outstandingItems', { count: totals.outstandingCount })}</span>
|
||||
: balancesKnown && !settlement?.flows?.length && <span className="text-content-faint">{t('costs.allSettled')}</span>} />
|
||||
: settledUp && <span className="text-content-faint">{t('costs.allSettled')}</span>} />
|
||||
{totalCard}
|
||||
</div>)}
|
||||
|
||||
@@ -445,11 +443,6 @@ export default function CostsPanel({ tripId, tripMembers = [] }: CostsPanelProps
|
||||
</div>
|
||||
)
|
||||
|
||||
// The owe and owed cards say why their figure is missing instead of claiming zero.
|
||||
function balancesUnknown() {
|
||||
return <span className="text-content-faint">{t(settlementError ? 'common.unknownError' : 'common.loading')}</span>
|
||||
}
|
||||
|
||||
// Settle-up and Balances both come from the one settlement request, so they
|
||||
// share the notice that says the numbers are missing rather than zero.
|
||||
function loadFailed() {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { useTranslation } from '../../i18n';
|
||||
import { currencyDecimals, currencyLocale, formatMoney } from '../../utils/formatters';
|
||||
import { fs } from '../shared/DialogShell';
|
||||
|
||||
@@ -26,6 +27,7 @@ export default function SummaryCard({
|
||||
icon,
|
||||
foot,
|
||||
tone,
|
||||
failed = false,
|
||||
}: {
|
||||
label: string;
|
||||
sub: string;
|
||||
@@ -35,7 +37,10 @@ export default function SummaryCard({
|
||||
icon: React.ReactNode;
|
||||
foot: React.ReactNode;
|
||||
tone: 'owe' | 'owed' | 'total' | 'unfinished';
|
||||
/** With no amount: the read failed rather than still running. */
|
||||
failed?: boolean;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const color = TONE_COLOR[tone];
|
||||
// formatToParts keeps the design's "big integer + muted symbol/decimals" styling
|
||||
// while letting Intl place the symbol and pick separators per locale + currency.
|
||||
@@ -102,7 +107,11 @@ export default function SummaryCard({
|
||||
)}
|
||||
</div>
|
||||
<div className="mt-auto flex flex-wrap items-center gap-1.5 text-content-muted" style={fs(12.5, 'body')}>
|
||||
{foot}
|
||||
{amount == null ? (
|
||||
<span className="text-content-faint">{t(failed ? 'common.unknownError' : 'common.loading')}</span>
|
||||
) : (
|
||||
foot
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@@ -238,6 +238,7 @@ export function useCostsLedger({
|
||||
return flows.map((f) => ({ ...f, amount: Math.round(f.amount * displayPerTrip * unit) / unit }));
|
||||
}, [settlementError, settlement, settledIn, base, tripCurrency, displayPerTrip, flows]);
|
||||
const balancesKnown = shownFlows != null;
|
||||
const settledUp = balancesKnown && !settlement?.flows?.length;
|
||||
// `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.
|
||||
const totals = useMemo(
|
||||
@@ -367,6 +368,7 @@ export function useCostsLedger({
|
||||
loadSettlement,
|
||||
flows,
|
||||
balancesKnown,
|
||||
settledUp,
|
||||
canSettle,
|
||||
splitting,
|
||||
knownNames,
|
||||
|
||||
Reference in New Issue
Block a user