-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix wrong scheduled transfer payment direction on PWA #3402
Changes from all commits
897f040
2dcbbb1
c7b86ac
cc3e424
dbcc669
25c54ac
3695dd0
1e2b374
6aa86c6
1e01626
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import React, { | |
createRef, | ||
useMemo, | ||
type ReactElement, | ||
useEffect, | ||
} from 'react'; | ||
import { Trans } from 'react-i18next'; | ||
import { useSelector } from 'react-redux'; | ||
|
@@ -30,7 +31,6 @@ import { | |
import { send, listen } from 'loot-core/src/platform/client/fetch'; | ||
import { currentDay } from 'loot-core/src/shared/months'; | ||
import { q, type Query } from 'loot-core/src/shared/query'; | ||
import { getScheduledAmount } from 'loot-core/src/shared/schedules'; | ||
import { | ||
updateTransaction, | ||
realizeTempTransactions, | ||
|
@@ -50,14 +50,14 @@ import { | |
type TransactionFilterEntity, | ||
} from 'loot-core/src/types/models'; | ||
|
||
import { useAccountPreviewTransactions } from '../../hooks/useAccountPreviewTransactions'; | ||
import { useAccounts } from '../../hooks/useAccounts'; | ||
import { useActions } from '../../hooks/useActions'; | ||
import { useCategories } from '../../hooks/useCategories'; | ||
import { useDateFormat } from '../../hooks/useDateFormat'; | ||
import { useFailedAccounts } from '../../hooks/useFailedAccounts'; | ||
import { useLocalPref } from '../../hooks/useLocalPref'; | ||
import { usePayees } from '../../hooks/usePayees'; | ||
import { usePreviewTransactions } from '../../hooks/usePreviewTransactions'; | ||
import { | ||
SelectedProviderWithItems, | ||
type Actions, | ||
|
@@ -147,7 +147,6 @@ type AllTransactionsProps = { | |
transactions: TransactionEntity[], | ||
balances: Record<string, { balance: number }> | null, | ||
) => ReactElement; | ||
collapseTransactions: (ids: string[]) => void; | ||
}; | ||
|
||
function AllTransactions({ | ||
|
@@ -157,14 +156,24 @@ function AllTransactions({ | |
showBalances, | ||
filtered, | ||
children, | ||
collapseTransactions, | ||
}: AllTransactionsProps) { | ||
const accountId = account?.id; | ||
const prependTransactions: (TransactionEntity & { _inverse?: boolean })[] = | ||
usePreviewTransactions(collapseTransactions).map(trans => ({ | ||
...trans, | ||
_inverse: accountId ? accountId !== trans.account : false, | ||
})); | ||
const { dispatch: splitsExpandedDispatch } = useSplitsExpanded(); | ||
const { previewTransactions, isLoading: isPreviewTransactionsLoading } = | ||
useAccountPreviewTransactions({ accountId }); | ||
|
||
useEffect(() => { | ||
if (!isPreviewTransactionsLoading) { | ||
splitsExpandedDispatch({ | ||
type: 'close-splits', | ||
ids: previewTransactions.filter(t => t.is_parent).map(t => t.id), | ||
}); | ||
} | ||
}, [ | ||
isPreviewTransactionsLoading, | ||
previewTransactions, | ||
splitsExpandedDispatch, | ||
]); | ||
|
||
transactions ??= []; | ||
|
||
|
@@ -184,29 +193,26 @@ function AllTransactions({ | |
} | ||
|
||
// Reverse so we can calculate from earliest upcoming schedule. | ||
const scheduledBalances = [...prependTransactions] | ||
const previewBalances = [...previewTransactions] | ||
.reverse() | ||
.map(scheduledTransaction => { | ||
const amount = | ||
(scheduledTransaction._inverse ? -1 : 1) * | ||
getScheduledAmount(scheduledTransaction.amount); | ||
.map(previewTransaction => { | ||
return { | ||
// TODO: fix me | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
balance: (runningBalance += amount), | ||
id: scheduledTransaction.id, | ||
balance: (runningBalance += previewTransaction.amount), | ||
id: previewTransaction.id, | ||
}; | ||
}); | ||
return groupById(scheduledBalances); | ||
}, [showBalances, prependTransactions, runningBalance]); | ||
return groupById(previewBalances); | ||
}, [showBalances, previewTransactions, runningBalance]); | ||
|
||
const allTransactions = useMemo(() => { | ||
// Don't prepend scheduled transactions if we are filtering | ||
if (!filtered && prependTransactions.length > 0) { | ||
return prependTransactions.concat(transactions); | ||
if (!filtered && previewTransactions.length > 0) { | ||
return previewTransactions.concat(transactions); | ||
} | ||
return transactions; | ||
}, [filtered, prependTransactions, transactions]); | ||
}, [filtered, previewTransactions, transactions]); | ||
|
||
const allBalances = useMemo(() => { | ||
// Don't prepend scheduled transactions if we are filtering | ||
|
@@ -216,7 +222,7 @@ function AllTransactions({ | |
return balances; | ||
}, [filtered, prependBalances, balances]); | ||
|
||
if (!prependTransactions?.length || filtered) { | ||
if (!previewTransactions?.length || filtered) { | ||
return children(transactions, balances); | ||
} | ||
return children(allTransactions, allBalances); | ||
|
@@ -1669,9 +1675,6 @@ class AccountInternal extends PureComponent< | |
balances={balances} | ||
showBalances={showBalances} | ||
filtered={transactionsFiltered} | ||
collapseTransactions={ids => | ||
this.props.splitsExpandedDispatch({ type: 'close-splits', ids }) | ||
} | ||
> | ||
Comment on lines
-1672
to
1678
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the change from a callback to a Repro: create test budget, update the "Fast Internet" schedule to a date in the future, and make it a splitting rule. Then view the "Bank of America" account There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be fixed now :) |
||
{(allTransactions, allBalances) => ( | ||
<SelectedProviderWithItems | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve balance calculation readability.
The current implementation uses assignment within an expression which makes the code harder to understand and maintain. This pattern can lead to subtle bugs as expressions are typically expected to be side-effect free.
Consider refactoring to make the logic more explicit:
📝 Committable suggestion
🧰 Tools
🪛 Biome
[error] 202-202: The assignment should not be in an expression.
The use of assignments in expressions is confusing.
Expressions are often considered as side-effect free.
(lint/suspicious/noAssignInExpressions)