Skip to content

Commit 72448a2

Browse files
Fix Your Spend section disappearing after deleting a split expense offline
1 parent 4e6f872 commit 72448a2

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

src/libs/actions/IOU/SplitTransactionUpdate.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,10 +1792,23 @@ function updateSplitTransactions({
17921792
// (e.g. split to a lower amount). Home reads that total from the cached snapshot, which is only refreshed
17931793
// online, so patch the same-currency delta optimistically. `changesInReportTotal` is the signed report-total
17941794
// delta (new split total minus the pre-split total) in the transaction currency.
1795+
// The split also changes how many reimbursable transactions the report holds (e.g. one expense becomes several).
1796+
// `count` drives the Home "Awaiting approval" row visibility, so it must move in lockstep with the membership
1797+
// change — otherwise a later deletion of one split child decrements a stale `count` to 0 and hides the row even
1798+
// though split children remain. The replaced set is the original transaction (creation of splits) or the existing
1799+
// split children (re-splitting); the new set is `splits`. A `reimbursable !== false` transaction counts (matching
1800+
// the snapshot query convention).
1801+
const reimbursableSplitsCount = splits.filter((split) => split.reimbursable !== false).length;
1802+
const previousReimbursableCount = isCreationOfSplits
1803+
? Number(originalTransaction?.reimbursable !== false)
1804+
: originalChildTransactions.filter((childTransaction) => childTransaction?.reimbursable !== false).length;
1805+
const reimbursableCountDiff = reimbursableSplitsCount - previousReimbursableCount;
1806+
17951807
const yourSpendSplitUpdates = getYourSpendSnapshotSplitUpdates({
17961808
iouReport: expenseReport,
17971809
originalTransaction,
17981810
reimbursableDiff: changesInReportTotal,
1811+
reimbursableCountDiff,
17991812
currentUserAccountID: currentUserPersonalDetails.accountID,
18001813
});
18011814
onyxData.optimisticData?.push(...yourSpendSplitUpdates.optimisticData);

src/libs/actions/IOU/YourSpendSnapshotUpdate.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -468,19 +468,31 @@ type GetYourSpendSnapshotSplitUpdatesParams = {
468468
// Signed change to the report's reimbursable total, in the transaction/report currency
469469
// (new split total minus the pre-split total). Negative when the expense is split to a lower amount.
470470
reimbursableDiff: number;
471+
// Signed change to the number of reimbursable transactions in the report caused by the split
472+
// (e.g. splitting one expense into two reimbursable children is +1). `count` drives the Home row's
473+
// visibility, so it must move in lockstep with the membership change even when `reimbursableDiff` is 0
474+
// (an equal-amount split leaves the total unchanged but still adds transactions).
475+
reimbursableCountDiff: number;
471476
currentUserAccountID: number;
472477
};
473478

474479
/**
475-
* Optimistically patches the "Awaiting approval" snapshot total when a SUBMITTED expense is split and the resulting
476-
* reimbursable total changes (e.g. split to a lower amount). The report stays in the awaiting-approval section, so this
477-
* is a same-section amount delta rather than a state move. Home reads totals from `snapshot.search.total`, which is only
478-
* refreshed online. Mirrors the amount-edit path: same-currency only (the currency guard in
479-
* `buildSnapshotTotalUpdatesForHash` skips a mismatch, since FX conversion isn't available offline).
480+
* Optimistically patches the "Awaiting approval" snapshot when a SUBMITTED expense is split. A split keeps the report
481+
* in the awaiting-approval section but changes both its reimbursable total (e.g. split to a lower amount) and the number
482+
* of reimbursable transactions it contains (one expense becomes several). Home reads `snapshot.search.total`/`count`,
483+
* which are only refreshed online, and `count` drives the row's visibility — so both must be patched, even when the
484+
* total is unchanged (an equal-amount split still changes `count`). Mirrors the amount-edit path: same-currency only
485+
* (the currency guard in `buildSnapshotTotalUpdatesForHash` skips a mismatch, since FX conversion isn't available offline).
480486
*/
481-
function getYourSpendSnapshotSplitUpdates({iouReport, originalTransaction, reimbursableDiff, currentUserAccountID}: GetYourSpendSnapshotSplitUpdatesParams): YourSpendSnapshotOnyxData {
487+
function getYourSpendSnapshotSplitUpdates({
488+
iouReport,
489+
originalTransaction,
490+
reimbursableDiff,
491+
reimbursableCountDiff,
492+
currentUserAccountID,
493+
}: GetYourSpendSnapshotSplitUpdatesParams): YourSpendSnapshotOnyxData {
482494
const result: YourSpendSnapshotOnyxData = {optimisticData: [], successData: [], failureData: []};
483-
if (!iouReport || !originalTransaction || reimbursableDiff === 0) {
495+
if (!iouReport || !originalTransaction || (reimbursableDiff === 0 && reimbursableCountDiff === 0)) {
484496
return result;
485497
}
486498

@@ -491,7 +503,7 @@ function getYourSpendSnapshotSplitUpdates({iouReport, originalTransaction, reimb
491503

492504
const currency = getCurrency(originalTransaction);
493505
const approvalQueryJSON = buildSearchQueryJSON(buildAwaitingApprovalQuery(currentUserAccountID, paidGroupPolicyIDs));
494-
mergeYourSpendSnapshotOnyxData(result, buildSnapshotTotalUpdatesForHash(approvalQueryJSON?.hash, reimbursableDiff, currency));
506+
mergeYourSpendSnapshotOnyxData(result, buildSnapshotTotalUpdatesForHash(approvalQueryJSON?.hash, reimbursableDiff, currency, reimbursableCountDiff));
495507
return result;
496508
}
497509

0 commit comments

Comments
 (0)