Skip to content

A local-first personal finance app

Remove usage of a raw variable in Account component (#5684)

Joel Jeremy Marquez2ed908aff4b2
diff --git a/packages/desktop-client/src/components/accounts/Account.tsx b/packages/desktop-client/src/components/accounts/Account.tsx
index ba069eaac7..e40f09c7a7 100644
--- a/packages/desktop-client/src/components/accounts/Account.tsx
+++ b/packages/desktop-client/src/components/accounts/Account.tsx
@@ -29,7 +29,7 @@ import {
makeChild,
makeAsNonChildTransactions,
} from 'loot-core/shared/transactions';
-import { applyChanges, groupById } from 'loot-core/shared/util';
+import { applyChanges, type IntegerAmount } from 'loot-core/shared/util';
import {
type NewRuleEntity,
type RuleActionEntity,
@@ -72,6 +72,7 @@ import {
import { useSyncedPref } from '@desktop-client/hooks/useSyncedPref';
import { useTransactionBatchActions } from '@desktop-client/hooks/useTransactionBatchActions';
import { useTransactionFilters } from '@desktop-client/hooks/useTransactionFilters';
+import { calculateRunningBalancesBottomUp } from '@desktop-client/hooks/useTransactions';
import {
openAccountCloseModal,
pushModal,
@@ -100,12 +101,12 @@ function isTransactionFilterEntity(
type AllTransactionsProps = {
account?: AccountEntity | undefined;
transactions: TransactionEntity[];
- balances: Record<string, { balance: number }> | null;
+ balances: Record<TransactionEntity['id'], IntegerAmount> | null;
showBalances?: boolean | undefined;
filtered?: boolean | undefined;
children: (
transactions: TransactionEntity[],
- balances: Record<string, { balance: number }> | null,
+ balances: Record<TransactionEntity['id'], IntegerAmount> | null,
) => ReactElement;
};
@@ -137,13 +138,13 @@ function AllTransactions({
transactions ??= [];
- let runningBalance = useMemo(() => {
+ const runningBalance = useMemo(() => {
if (!showBalances) {
return 0;
}
return balances && transactions?.length > 0
- ? (balances[transactions[0].id]?.balance ?? 0)
+ ? (balances[transactions[0].id] ?? 0)
: 0;
}, [showBalances, balances, transactions]);
@@ -152,24 +153,13 @@ function AllTransactions({
return null;
}
- // Reverse so we can calculate from earliest upcoming schedule.
- const previewBalances = [...previewTransactions]
- .reverse()
- .map(previewTransaction => {
- if (!previewTransaction.is_child) {
- return {
- // eslint-disable-next-line react-hooks/exhaustive-deps
- balance: (runningBalance += previewTransaction.amount),
- id: previewTransaction.id,
- };
- } else {
- return {
- balance: 0,
- id: previewTransaction.id,
- };
- }
- });
- return groupById(previewBalances);
+ return Object.fromEntries(
+ calculateRunningBalancesBottomUp(
+ previewTransactions,
+ 'all',
+ runningBalance,
+ ),
+ );
}, [showBalances, previewTransactions, runningBalance]);
const allTransactions = useMemo(() => {
@@ -272,7 +262,7 @@ type AccountInternalState = {
transactionCount: number;
transactionsFiltered?: boolean;
showBalances?: boolean | undefined;
- balances: Record<string, { balance: number }> | null;
+ balances: Record<TransactionEntity['id'], IntegerAmount> | null;
showCleared?: boolean | undefined;
prevShowCleared?: boolean | undefined;
showReconciled: boolean;
@@ -676,13 +666,17 @@ class AccountInternal extends PureComponent<
return null;
}
- const { data } = await aqlQuery(
- this.paged.query
- .options({ splits: 'none' })
- .select([{ balance: { $sumOver: '$amount' } }]),
- );
+ const { data }: { data: { id: string; balance: number }[] } =
+ await aqlQuery(
+ this.paged.query
+ .options({ splits: 'none' })
+ .select([{ balance: { $sumOver: '$amount' } }]),
+ );
- return groupById<{ id: string; balance: number }>(data);
+ return data.reduce((balances: Record<string, number>, row) => {
+ balances[row.id] = row.balance;
+ return balances;
+ }, {});
}
onRunRules = async (ids: string[]) => {
diff --git a/packages/desktop-client/src/components/transactions/TransactionsTable.tsx b/packages/desktop-client/src/components/transactions/TransactionsTable.tsx
index 53c8f82540..1bcb29802a 100644
--- a/packages/desktop-client/src/components/transactions/TransactionsTable.tsx
+++ b/packages/desktop-client/src/components/transactions/TransactionsTable.tsx
@@ -57,6 +57,7 @@ import {
} from 'loot-core/shared/transactions';
import {
amountToCurrency,
+ type IntegerAmount,
integerToCurrency,
titleFirst,
} from 'loot-core/shared/util';
@@ -1937,7 +1938,7 @@ type TransactionTableInnerProps = {
accounts: AccountEntity[];
categoryGroups: CategoryGroupEntity[];
payees: PayeeEntity[];
- balances: Record<TransactionEntity['id'], { balance: number }> | null;
+ balances: Record<TransactionEntity['id'], IntegerAmount> | null;
showBalances: boolean;
showReconciled: boolean;
showCleared: boolean;
@@ -2124,7 +2125,7 @@ function TransactionTableInner({
expanded={isExpanded?.(trans.id)}
matched={isMatched?.(trans.id)}
showZeroInDeposit={isChildDeposit}
- balance={balances?.[trans.id]?.balance ?? 0}
+ balance={balances?.[trans.id] ?? 0}
focusedField={editing ? tableNavigator.focusedField : undefined}
accounts={accounts}
categoryGroups={categoryGroups}
@@ -2284,7 +2285,7 @@ export type TransactionTableProps = {
accounts: AccountEntity[];
categoryGroups: CategoryGroupEntity[];
payees: PayeeEntity[];
- balances: Record<TransactionEntity['id'], { balance: number }> | null;
+ balances: Record<TransactionEntity['id'], IntegerAmount> | null;
showBalances: boolean;
showReconciled: boolean;
showCleared: boolean;
diff --git a/packages/desktop-client/src/hooks/useTransactions.ts b/packages/desktop-client/src/hooks/useTransactions.ts
index 8ee32be9c8..9b4b00fbd4 100644
--- a/packages/desktop-client/src/hooks/useTransactions.ts
+++ b/packages/desktop-client/src/hooks/useTransactions.ts
@@ -214,7 +214,7 @@ function getCalculateRunningBalancesFn(
}
export function calculateRunningBalancesBottomUp(
- transactions: TransactionEntity[],
+ transactions: readonly TransactionEntity[],
splits: TransactionSplitsOption,
startingBalance: IntegerAmount = 0,
) {
@@ -252,7 +252,7 @@ export function calculateRunningBalancesBottomUp(
}
export function calculateRunningBalancesTopDown(
- transactions: TransactionEntity[],
+ transactions: readonly TransactionEntity[],
splits: TransactionSplitsOption,
startingBalance: IntegerAmount = 0,
) {
diff --git a/upcoming-release-notes/5684.md b/upcoming-release-notes/5684.md
new file mode 100644
index 0000000000..8f5bb3a29b
--- /dev/null
+++ b/upcoming-release-notes/5684.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [joel-jeremy]
+---
+
+Remove usage of a raw variable in Account component