A local-first personal finance app
[Feature] Store and use last synced account balances (#4799)
Jeremy Tan562b5e2afd8c
diff --git a/packages/desktop-client/src/components/accounts/Reconcile.tsx b/packages/desktop-client/src/components/accounts/Reconcile.tsxindex ac6a6fcceb..eb589c9795 100644--- a/packages/desktop-client/src/components/accounts/Reconcile.tsx+++ b/packages/desktop-client/src/components/accounts/Reconcile.tsx@@ -1,4 +1,4 @@-import React, { type FormEvent, useState } from 'react';+import React, { useEffect, type FormEvent, useState } from 'react';import { Form } from 'react-aria-components';import { Trans } from 'react-i18next';@@ -134,9 +134,18 @@ export function ReconcileMenu({value: null,query: balanceQuery.query.filter({ cleared: true }),});+ const lastSyncedBalance = account.balance_current;const format = useFormat();const locale = useLocale();- const [inputValue, setInputValue] = useState<string | null>(null);++ const [inputValue, setInputValue] = useState<string | null>();+ // useEffect is needed here. clearedBalance does not work as a default value for inputValue and+ // to use a button to update inputValue we can't use defaultValue in the input form below+ useEffect(() => {+ if (clearedBalance != null) {+ setInputValue(format(clearedBalance, 'financial'));+ }+ }, [clearedBalance, format]);function onSubmit(e: FormEvent<HTMLFormElement>) {e.preventDefault();@@ -161,14 +170,29 @@ export function ReconcileMenu({reconcile with:</Trans></Text>- {clearedBalance != null && (- <InitialFocus>- <Input- defaultValue={format(clearedBalance, 'financial')}- onChangeValue={setInputValue}- style={{ margin: '7px 0' }}- />- </InitialFocus>+ <InitialFocus>+ <Input+ value={inputValue ?? ''}+ onChangeValue={setInputValue}+ style={{ margin: '7px 0' }}+ />+ </InitialFocus>+ {lastSyncedBalance != null && (+ <View>+ <Text>+ <Trans>Last Balance from Bank: </Trans>+ {format(lastSyncedBalance, 'financial')}+ </Text>+ <Button+ variant="menu"+ onPress={() =>+ setInputValue(format(lastSyncedBalance, 'financial'))+ }+ style={{ marginBottom: 7 }}+ >+ <Trans>Use last synced total</Trans>+ </Button>+ </View>)}<Text style={{ color: theme.pageTextSubdued, paddingBottom: 6 }}>{account?.last_reconcileddiff --git a/packages/loot-core/src/server/accounts/sync.ts b/packages/loot-core/src/server/accounts/sync.tsindex 610db049ec..54c3b6a3c2 100644--- a/packages/loot-core/src/server/accounts/sync.ts+++ b/packages/loot-core/src/server/accounts/sync.ts@@ -66,9 +66,9 @@ function getAccountBalance(account) {}}-async function updateAccountBalance(id, balance) {+async function updateAccountBalance(id: AccountEntity['id'], balance: number) {await db.runQuery('UPDATE accounts SET balance_current = ? WHERE id = ?', [- amountToInteger(balance),+ balance,id,]);}@@ -879,12 +879,17 @@ async function processBankSyncDownload(// that account sync sources can give two different transaction IDs even though it's the same transaction.const useStrictIdChecking = !acctRow.account_sync_source;+ /** Starting balance is actually the current balance of the account. */+ const {+ transactions: originalTransactions,+ startingBalance: currentBalance,+ } = download;+if (initialSync) {const { transactions } = download;- let balanceToUse = download.startingBalance;+ let balanceToUse = currentBalance;if (acctRow.account_sync_source === 'simpleFin') {- const currentBalance = download.startingBalance;const previousBalance = transactions.reduce((total, trans) => {return (total - parseInt(trans.transactionAmount.amount.replace('.', ''))@@ -935,8 +940,6 @@ async function processBankSyncDownload(});}- const { transactions: originalTransactions, accountBalance } = download;-if (originalTransactions.length === 0) {return { added: [], updated: [] };}@@ -954,7 +957,8 @@ async function processBankSyncDownload(useStrictIdChecking,);- if (accountBalance) await updateAccountBalance(id, accountBalance);+ /** Starting balance is actually the current balance of the account. */+ if (currentBalance) await updateAccountBalance(id, currentBalance);return result;});diff --git a/upcoming-release-notes/4799.md b/upcoming-release-notes/4799.mdnew file mode 100644index 0000000000..6b8c111f1a--- /dev/null+++ b/upcoming-release-notes/4799.md@@ -0,0 +1,6 @@+---+category: Features+authors: [koonweee, Spencer-Sawyer]+---++Show last synced account balance while reconciling