Skip to content

A local-first personal finance app

move balance history graph to live queries (#5821)

Matt Fiddaman14d436712ab0
diff --git a/packages/desktop-client/src/components/accounts/BalanceHistoryGraph.tsx b/packages/desktop-client/src/components/accounts/BalanceHistoryGraph.tsx
index 99a2577917..22ea3cbf47 100644
--- a/packages/desktop-client/src/components/accounts/BalanceHistoryGraph.tsx
+++ b/packages/desktop-client/src/components/accounts/BalanceHistoryGraph.tsx
@@ -22,7 +22,7 @@ import { PrivacyFilter } from '@desktop-client/components/PrivacyFilter';
import { LoadingIndicator } from '@desktop-client/components/reports/LoadingIndicator';
import { useLocale } from '@desktop-client/hooks/useLocale';
import * as query from '@desktop-client/queries';
-import { aqlQuery } from '@desktop-client/queries/aqlQuery';
+import { liveQuery } from '@desktop-client/queries/liveQuery';
const LABEL_WIDTH = 70;
@@ -32,11 +32,6 @@ type BalanceHistoryGraphProps = {
ref?: Ref<HTMLDivElement>;
};
-type Balance = {
- date: string;
- balance: number;
-};
-
export function BalanceHistoryGraph({
accountId,
style,
@@ -51,6 +46,11 @@ export function BalanceHistoryGraph({
date: string;
balance: number;
} | null>(null);
+ const [startingBalance, setStartingBalance] = useState<number | null>(null);
+ const [monthlyTotals, setMonthlyTotals] = useState<Array<{
+ date: string;
+ balance: number;
+ }> | null>(null);
const percentageChange = useMemo(() => {
if (balanceData.length < 2) return 0;
@@ -65,7 +65,70 @@ export function BalanceHistoryGraph({
);
useEffect(() => {
- async function fetchBalanceHistory() {
+ // Reset state when accountId changes
+ setStartingBalance(null);
+ setMonthlyTotals(null);
+ setLoading(true);
+
+ const endDate = new Date();
+ const startDate = subMonths(endDate, 12);
+
+ const startingBalanceQuery = query
+ .transactions(accountId)
+ .filter({
+ date: { $lt: monthUtils.firstDayOfMonth(startDate) },
+ })
+ .calculate({ $sum: '$amount' });
+ const monthlyTotalsQuery = query
+ .transactions(accountId)
+ .filter({
+ $and: [
+ { date: { $gte: monthUtils.firstDayOfMonth(startDate) } },
+ { date: { $lte: monthUtils.lastDayOfMonth(endDate) } },
+ ],
+ })
+ .groupBy({ $month: '$date' })
+ .select([{ date: { $month: '$date' } }, { amount: { $sum: '$amount' } }]);
+
+ const startingBalanceLive: ReturnType<typeof liveQuery<number>> = liveQuery(
+ startingBalanceQuery,
+ {
+ onData: (data: number[]) => {
+ setStartingBalance(data[0] || 0);
+ },
+ onError: error => {
+ console.error('Error fetching starting balance:', error);
+ setLoading(false);
+ },
+ },
+ );
+
+ const monthlyTotalsLive: ReturnType<
+ typeof liveQuery<{ date: string; amount: number }>
+ > = liveQuery(monthlyTotalsQuery, {
+ onData: (data: Array<{ date: string; amount: number }>) => {
+ setMonthlyTotals(
+ data.map(d => ({
+ date: d.date,
+ balance: d.amount,
+ })),
+ );
+ },
+ onError: error => {
+ console.error('Error fetching monthly totals:', error);
+ setLoading(false);
+ },
+ });
+
+ return () => {
+ startingBalanceLive?.unsubscribe();
+ monthlyTotalsLive?.unsubscribe();
+ };
+ }, [accountId, locale]);
+
+ // Process data when both startingBalance and monthlyTotals are available
+ useEffect(() => {
+ if (startingBalance !== null && monthlyTotals !== null) {
const endDate = new Date();
const startDate = subMonths(endDate, 12);
const months = eachMonthOfInterval({
@@ -73,99 +136,70 @@ export function BalanceHistoryGraph({
end: endDate,
}).map(m => format(m, 'yyyy-MM'));
- const [starting, totals]: [number, Balance[]] = await Promise.all([
- aqlQuery(
- query
- .transactions(accountId)
- .filter({
- date: { $lt: monthUtils.firstDayOfMonth(startDate) },
- })
- .calculate({ $sum: '$amount' }),
- ).then(({ data }) => data),
+ function processData(
+ startingBalanceValue: number,
+ monthlyTotalsValue: Array<{ date: string; balance: number }>,
+ ) {
+ let currentBalance = startingBalanceValue;
+ const totals = [...monthlyTotalsValue];
+ totals.reverse().forEach(month => {
+ currentBalance = currentBalance + month.balance;
+ month.balance = currentBalance;
+ });
+
+ // if the account doesn't have recent transactions
+ // then the empty months will be missing from our data
+ // so add in entries for those here
+ if (totals.length === 0) {
+ //handle case of no transactions in the last year
+ months.forEach(expectedMonth =>
+ totals.push({
+ date: expectedMonth,
+ balance: startingBalanceValue,
+ }),
+ );
+ } else if (totals.length < months.length) {
+ // iterate through each array together and add in missing data
+ let totalsIndex = 0;
+ let mostRecent = startingBalanceValue;
+ months.forEach(expectedMonth => {
+ if (totalsIndex > totals.length - 1) {
+ // fill in the data at the end of the window
+ totals.push({
+ date: expectedMonth,
+ balance: mostRecent,
+ });
+ } else if (totals[totalsIndex].date === expectedMonth) {
+ // a matched month
+ mostRecent = totals[totalsIndex].balance;
+ totalsIndex += 1;
+ } else {
+ // a missing month in the middle
+ totals.push({
+ date: expectedMonth,
+ balance: mostRecent,
+ });
+ }
+ });
+ }
- aqlQuery(
- query
- .transactions(accountId)
- .filter({
- $and: [
- { date: { $gte: monthUtils.firstDayOfMonth(startDate) } },
- { date: { $lte: monthUtils.lastDayOfMonth(endDate) } },
- ],
- })
- .groupBy({ $month: '$date' })
- .select([
- { date: { $month: '$date' } },
- { amount: { $sum: '$amount' } },
- ]),
- ).then(({ data }) =>
- data.map((d: { date: string; amount: number }) => {
+ const balances = totals
+ .sort((a, b) => monthUtils.differenceInCalendarMonths(a.date, b.date))
+ .map(t => {
return {
- date: d.date,
- balance: d.amount,
+ balance: t.balance,
+ date: monthUtils.format(t.date, 'MMM yyyy', locale),
};
- }),
- ),
- ]);
-
- // calculate balances from sum of transactions
- let currentBalance = starting;
- totals.reverse().forEach(month => {
- currentBalance = currentBalance + month.balance;
- month.balance = currentBalance;
- });
+ });
- // if the account doesn't have recent transactions
- // then the empty months will be missing from our data
- // so add in entries for those here
- if (totals.length === 0) {
- //handle case of no transactions in the last year
- months.forEach(expectedMonth =>
- totals.push({
- date: expectedMonth,
- balance: starting,
- }),
- );
- } else if (totals.length < months.length) {
- // iterate through each array together and add in missing data
- let totalsIndex = 0;
- let mostRecent = starting;
- months.forEach(expectedMonth => {
- if (totalsIndex > totals.length - 1) {
- // fill in the data at the end of the window
- totals.push({
- date: expectedMonth,
- balance: mostRecent,
- });
- } else if (totals[totalsIndex].date === expectedMonth) {
- // a matched month
- mostRecent = totals[totalsIndex].balance;
- totalsIndex += 1;
- } else {
- // a missing month in the middle
- totals.push({
- date: expectedMonth,
- balance: mostRecent,
- });
- }
- });
+ setBalanceData(balances);
+ setHoveredValue(balances[balances.length - 1]);
+ setLoading(false);
}
- const balances = totals
- .sort((a, b) => monthUtils.differenceInCalendarMonths(a.date, b.date))
- .map(t => {
- return {
- balance: t.balance,
- date: monthUtils.format(t.date, 'MMM yyyy', locale),
- };
- });
-
- setBalanceData(balances);
- setHoveredValue(balances[balances.length - 1]);
- setLoading(false);
+ processData(startingBalance, monthlyTotals);
}
-
- fetchBalanceHistory();
- }, [accountId, locale]);
+ }, [startingBalance, monthlyTotals, locale]);
// State to track if the chart is hovered (used to conditionally render PrivacyFilter)
const [isHovered, setIsHovered] = useState(false);
diff --git a/packages/desktop-client/src/queries/liveQuery.ts b/packages/desktop-client/src/queries/liveQuery.ts
index 2e4e46cd05..6e5a26407c 100644
--- a/packages/desktop-client/src/queries/liveQuery.ts
+++ b/packages/desktop-client/src/queries/liveQuery.ts
@@ -180,7 +180,7 @@ export class LiveQuery<TResponse = unknown> {
protected fetchData = async (
runQuery: () => Promise<{
- data: Data<TResponse>;
+ data: Data<TResponse> | TResponse;
dependencies: string[];
}>,
) => {
@@ -205,7 +205,15 @@ export class LiveQuery<TResponse = unknown> {
// still subscribed (`this.unsubscribeSyncEvent` will exist)
if (this.inflightRequestId === reqId && this._unsubscribeSyncEvent) {
const previousData = this.data;
- this.data = data;
+
+ // For calculate queries, data is a raw value, not an array
+ // Convert it to an array format to maintain consistency
+ if (this._query.state.calculation) {
+ this.data = [data as TResponse];
+ } else {
+ this.data = data as Data<TResponse>;
+ }
+
this.onData(this.data, previousData);
this.inflightRequestId = null;
}
diff --git a/upcoming-release-notes/5821.md b/upcoming-release-notes/5821.md
new file mode 100644
index 0000000000..c1f5fdfa22
--- /dev/null
+++ b/upcoming-release-notes/5821.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [matt-fidd]
+---
+
+Ensure account balance graphs update when transactions are added or changed