Skip to content

A local-first personal finance app

:sparkles: show account historical balance change in side-nav hover tooltip (#5085)

Matiss Janis Aboltinsb5f29ccb4a76
diff --git a/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-closes-an-account-4-chromium-linux.png b/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-closes-an-account-4-chromium-linux.png
index cc178a3fe7..91c4c13ca1 100644
Binary files a/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-closes-an-account-4-chromium-linux.png and b/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-closes-an-account-4-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-and-cash-flow-reports-1-chromium-linux.png b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-and-cash-flow-reports-1-chromium-linux.png
index 598354293c..cb450ca1ba 100644
Binary files a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-and-cash-flow-reports-1-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-and-cash-flow-reports-1-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-and-cash-flow-reports-2-chromium-linux.png b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-and-cash-flow-reports-2-chromium-linux.png
index 52387a0ddd..f6d0698375 100644
Binary files a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-and-cash-flow-reports-2-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-and-cash-flow-reports-2-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-and-cash-flow-reports-3-chromium-linux.png b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-and-cash-flow-reports-3-chromium-linux.png
index 26bf6ce99b..304a1d2c90 100644
Binary files a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-and-cash-flow-reports-3-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-and-cash-flow-reports-3-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-graph-and-checks-visuals-1-chromium-linux.png b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-graph-and-checks-visuals-1-chromium-linux.png
index bac01296ab..ec1c35bcb9 100644
Binary files a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-graph-and-checks-visuals-1-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-graph-and-checks-visuals-1-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-graph-and-checks-visuals-2-chromium-linux.png b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-graph-and-checks-visuals-2-chromium-linux.png
index 0331e5af36..51a5a8aba5 100644
Binary files a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-graph-and-checks-visuals-2-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-graph-and-checks-visuals-2-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-graph-and-checks-visuals-3-chromium-linux.png b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-graph-and-checks-visuals-3-chromium-linux.png
index 5b73be435b..56a1ab8eb6 100644
Binary files a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-graph-and-checks-visuals-3-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-loads-net-worth-graph-and-checks-visuals-3-chromium-linux.png differ
diff --git a/packages/desktop-client/src/components/reports/graphs/NetWorthGraph.tsx b/packages/desktop-client/src/components/reports/graphs/NetWorthGraph.tsx
index 7c9a7cf424..ba822d431b 100644
--- a/packages/desktop-client/src/components/reports/graphs/NetWorthGraph.tsx
+++ b/packages/desktop-client/src/components/reports/graphs/NetWorthGraph.tsx
@@ -197,7 +197,7 @@ export function NetWorthGraph({
</defs>
<Area
- type="linear"
+ type="monotone"
dot={false}
activeDot={false}
animationDuration={0}
diff --git a/packages/desktop-client/src/components/sidebar/Account.tsx b/packages/desktop-client/src/components/sidebar/Account.tsx
index e7999ac7d4..6555f025cc 100644
--- a/packages/desktop-client/src/components/sidebar/Account.tsx
+++ b/packages/desktop-client/src/components/sidebar/Account.tsx
@@ -16,6 +16,8 @@ import { css, cx } from '@emotion/css';
import * as Platform from 'loot-core/shared/platform';
import { type AccountEntity } from 'loot-core/types/models';
+import { BalanceHistoryGraph } from './BalanceHistoryGraph';
+
import { Link } from '@desktop-client/components/common/Link';
import { Notes } from '@desktop-client/components/Notes';
import {
@@ -282,16 +284,18 @@ export function Account<FieldName extends SheetFields<'account'>>({
<Text
style={{
fontWeight: 'bold',
- borderBottom: accountNote ? `1px solid ${theme.tableBorder}` : 0,
- marginBottom: accountNote ? '0.5rem' : 0,
}}
>
{name}
</Text>
+ {account && <BalanceHistoryGraph accountId={account.id} />}
{accountNote && (
<Notes
getStyle={() => ({
+ borderTop: `1px solid ${theme.tableBorder}`,
padding: 0,
+ paddingTop: '0.5rem',
+ marginTop: '0.5rem',
})}
notes={accountNote}
/>
diff --git a/packages/desktop-client/src/components/sidebar/BalanceHistoryGraph.tsx b/packages/desktop-client/src/components/sidebar/BalanceHistoryGraph.tsx
new file mode 100644
index 0000000000..7ac5143940
--- /dev/null
+++ b/packages/desktop-client/src/components/sidebar/BalanceHistoryGraph.tsx
@@ -0,0 +1,149 @@
+import React, { useState, useEffect, useMemo } from 'react';
+
+import { SpaceBetween } from '@actual-app/components/space-between';
+import { styles } from '@actual-app/components/styles';
+import { Text } from '@actual-app/components/text';
+import { theme } from '@actual-app/components/theme';
+import { subMonths, format, eachMonthOfInterval, endOfMonth } from 'date-fns';
+import { LineChart, Line, YAxis, Tooltip as RechartsTooltip } from 'recharts';
+
+import { send } from 'loot-core/platform/client/fetch';
+import { integerToCurrency } from 'loot-core/shared/util';
+
+import { LoadingIndicator } from '@desktop-client/components/reports/LoadingIndicator';
+
+const CHART_HEIGHT = 70;
+const CHART_WIDTH = 280;
+const LABEL_WIDTH = 70;
+const TOTAL_WIDTH = CHART_WIDTH + LABEL_WIDTH;
+
+type BalanceHistoryGraphProps = {
+ accountId: string;
+};
+
+export function BalanceHistoryGraph({ accountId }: BalanceHistoryGraphProps) {
+ const [balanceData, setBalanceData] = useState<
+ Array<{ date: string; balance: number }>
+ >([]);
+ const [loading, setLoading] = useState(true);
+ const [hoveredValue, setHoveredValue] = useState<{
+ date: string;
+ balance: number;
+ } | null>(null);
+
+ const percentageChange = useMemo(() => {
+ if (balanceData.length < 2) return 0;
+ const firstBalance = balanceData[0].balance;
+ const lastBalance = balanceData[balanceData.length - 1].balance;
+ if (firstBalance === 0) return 0;
+ return ((lastBalance - firstBalance) / Math.abs(firstBalance)) * 100;
+ }, [balanceData]);
+
+ useEffect(() => {
+ async function fetchBalanceHistory() {
+ const endDate = new Date();
+ const startDate = subMonths(endDate, 12);
+ const months = eachMonthOfInterval({ start: startDate, end: endDate });
+
+ const balances = await Promise.all(
+ months.map(async date => {
+ const balance = await send('api/account-balance', {
+ id: accountId,
+ cutoff: endOfMonth(date),
+ });
+ return {
+ date: format(date, 'MMM yyyy'),
+ balance: balance || 0,
+ };
+ }),
+ );
+
+ setBalanceData(balances);
+ setHoveredValue(balances[balances.length - 1]);
+ setLoading(false);
+ }
+
+ fetchBalanceHistory();
+ }, [accountId]);
+
+ if (loading) {
+ return (
+ <div style={{ width: TOTAL_WIDTH, height: CHART_HEIGHT, marginTop: 10 }}>
+ <LoadingIndicator />
+ </div>
+ );
+ }
+
+ return (
+ <div style={{ width: TOTAL_WIDTH, marginTop: 10 }}>
+ <div
+ style={{
+ display: 'flex',
+ alignItems: 'stretch',
+ justifyContent: 'space-between',
+ }}
+ >
+ <LineChart data={balanceData} width={CHART_WIDTH} height={CHART_HEIGHT}>
+ <YAxis domain={['dataMin', 'dataMax']} hide={true} />
+ <RechartsTooltip
+ contentStyle={{
+ display: 'none',
+ }}
+ labelFormatter={(label, items) => {
+ const data = items[0]?.payload;
+ if (data) {
+ setHoveredValue(data);
+ }
+ return '';
+ }}
+ isAnimationActive={false}
+ />
+ <Line
+ type="monotone"
+ dataKey="balance"
+ stroke={
+ percentageChange >= 0 ? theme.noticeTextLight : theme.errorText
+ }
+ strokeWidth={2}
+ dot={false}
+ animationDuration={0}
+ />
+ </LineChart>
+
+ <SpaceBetween
+ direction="vertical"
+ style={{
+ alignItems: 'flex-end',
+ justifyContent: 'space-between',
+ width: LABEL_WIDTH,
+ textAlign: 'right',
+ ...styles.verySmallText,
+ }}
+ >
+ {percentageChange === 0 ? (
+ <div />
+ ) : (
+ <Text
+ style={{
+ color:
+ percentageChange >= 0
+ ? theme.noticeTextLight
+ : theme.errorText,
+ }}
+ >
+ {percentageChange >= 0 ? '+' : ''}
+ {percentageChange.toFixed(1)}%
+ </Text>
+ )}
+
+ {hoveredValue && (
+ <Text>
+ <div style={{ fontWeight: 800 }}>{hoveredValue.date}</div>
+ <div>{integerToCurrency(hoveredValue.balance)}</div>
+ </Text>
+ )}
+ </SpaceBetween>
+ </div>
+ </div>
+ );
+}
diff --git a/upcoming-release-notes/5085.md b/upcoming-release-notes/5085.md
new file mode 100644
index 0000000000..92ee46ddcd
--- /dev/null
+++ b/upcoming-release-notes/5085.md
@@ -0,0 +1,6 @@
+---
+category: Features
+authors: [MatissJanis]
+---
+
+Show account balance historical change in side nav hover tooltip