A local-first personal finance app
Ensure only valid months can be selected in Sankey chart datepicker (#8790)
Emil Tveden Bjerglundd8008e0d3100
diff --git a/packages/desktop-client/src/components/reports/reportRanges.test.ts b/packages/desktop-client/src/components/reports/reportRanges.test.tsindex 32740ec867..52cbd5e42f 100644--- a/packages/desktop-client/src/components/reports/reportRanges.test.ts+++ b/packages/desktop-client/src/components/reports/reportRanges.test.ts@@ -2,6 +2,7 @@ import * as monthUtils from '@actual-app/core/shared/months';import { describe, expect, it } from 'vitest';import {+ boundMonthRange,calculateSpendingReportTimeRange,calculateTimeRange,getFullFutureRange,@@ -162,3 +163,23 @@ describe('getFullFutureRange', () => {]);});});++describe('boundMonthRange', () => {+ it('keeps a range that is already within bounds', () => {+ expect(boundMonthRange('2026-08', '2026-09', '2026-08', '2026-09')).toEqual(+ ['2026-08', '2026-09'],+ );+ });++ it('bounds both ends to the available range', () => {+ expect(boundMonthRange('2026-08', '2026-09', '2025-10', '2027-01')).toEqual(+ ['2026-08', '2026-09'],+ );+ });++ it('collapses to a single month when the bounded end is before start', () => {+ expect(boundMonthRange('2026-08', '2026-09', '2026-09', '2026-07')).toEqual(+ ['2026-09', '2026-09'],+ );+ });+});diff --git a/packages/desktop-client/src/components/reports/reportRanges.ts b/packages/desktop-client/src/components/reports/reportRanges.tsindex 470f5b7c6b..92fac9a9c1 100644--- a/packages/desktop-client/src/components/reports/reportRanges.ts+++ b/packages/desktop-client/src/components/reports/reportRanges.ts@@ -98,6 +98,48 @@ export function validateRange(return [start, end];}+export function boundMonthRange(+ earliestMonth: string,+ latestMonth: string,+ startMonth: string,+ endMonth: string,+): [string, string] {+ let boundedStart = startMonth;+ let boundedEnd = endMonth;++ if (monthUtils.isBefore(boundedStart, earliestMonth)) {+ boundedStart = earliestMonth;+ } else if (monthUtils.isAfter(boundedStart, latestMonth)) {+ boundedStart = latestMonth;+ }++ if (monthUtils.isBefore(boundedEnd, earliestMonth)) {+ boundedEnd = earliestMonth;+ } else if (monthUtils.isAfter(boundedEnd, latestMonth)) {+ boundedEnd = latestMonth;+ }++ if (monthUtils.isBefore(boundedEnd, boundedStart)) {+ boundedEnd = boundedStart;+ }++ return [boundedStart, boundedEnd];+}++export function boundMonthRangeFromDates(+ earliestDate: string,+ latestDate: string,+ start: string,+ end: string,+): [string, string] {+ return boundMonthRange(+ monthUtils.getMonth(earliestDate),+ monthUtils.getMonth(latestDate),+ monthUtils.getMonth(start),+ monthUtils.getMonth(end),+ );+}+function boundedRange(earliest: string,latest: string,diff --git a/packages/desktop-client/src/components/reports/reports/Sankey.tsx b/packages/desktop-client/src/components/reports/reports/Sankey.tsxindex 7e8371e602..c364920774 100644--- a/packages/desktop-client/src/components/reports/reports/Sankey.tsx+++ b/packages/desktop-client/src/components/reports/reports/Sankey.tsx@@ -26,7 +26,6 @@ import type {SankeyWidget,TimeFrame,} from '@actual-app/core/types/models';-import * as d from 'date-fns';import { debounce } from 'es-toolkit/compat';import type { TFunction } from 'i18next';import type { SankeyData } from 'recharts/types/chart/Sankey';@@ -37,7 +36,10 @@ import { MobilePageHeader, Page, PageHeader } from '#components/Page';import { SankeyGraph } from '#components/reports/graphs/SankeyGraph';import { Header } from '#components/reports/Header';import { LoadingIndicator } from '#components/reports/LoadingIndicator';-import { calculateTimeRange } from '#components/reports/reportRanges';+import {+ boundMonthRangeFromDates,+ calculateTimeRange,+} from '#components/reports/reportRanges';import {buildSankeyData,createBaseGraphSpreadsheet,@@ -46,7 +48,6 @@ import {} from '#components/reports/spreadsheets/sankey-spreadsheet';import type { Graph } from '#components/reports/spreadsheets/sankey-spreadsheet';import { useReport } from '#components/reports/useReport';-import { fromDateRepr } from '#components/reports/util';import { useCategories } from '#hooks/useCategories';import { useDashboardWidget } from '#hooks/useDashboardWidget';import { useFormatList } from '#hooks/useFormatList';@@ -581,9 +582,16 @@ function SankeyInner({ widget }: SankeyInnerProps) {return null;}- return createBaseGraphSpreadsheet(+ const [boundedStart, boundedEnd] = boundMonthRangeFromDates(+ earliestTransaction,+ latestTransaction,start,end,+ );++ return createBaseGraphSpreadsheet(+ boundedStart,+ boundedEnd,groupedCategories,conditions,conditionsOp,@@ -592,6 +600,8 @@ function SankeyInner({ widget }: SankeyInnerProps) {showTransfers,);}, [+ earliestTransaction,+ latestTransaction,datesInitialized,start,end,@@ -665,30 +675,14 @@ function SankeyInner({ widget }: SankeyInnerProps) {setDatesInitialized(true);const currentMonth = monthUtils.currentMonth();- let earliestMonth = earliestTransaction- ? monthUtils.monthFromDate(- d.parseISO(fromDateRepr(earliestTransaction.date)),- )- : currentMonth;- const latestTransactionMonth = latestTransaction- ? monthUtils.monthFromDate(- d.parseISO(fromDateRepr(latestTransaction.date)),- )- : currentMonth;+ const earliestMonth = monthUtils.getMonth(earliestTransactionDate);+ const latestTransactionMonth = monthUtils.getMonth(latestTransactionDate);const latestMonth =latestTransactionMonth > currentMonth? latestTransactionMonth: currentMonth;- // Make sure the month selects are at least populated with a- // year's worth of months. We can undo this when we have fancier- // date selects.- const yearAgo = monthUtils.subMonths(latestMonth, 12);- if (earliestMonth > yearAgo) {- earliestMonth = yearAgo;- }-const allMonths = monthUtils.rangeInclusive(earliestMonth, latestMonth).map(month => ({diff --git a/packages/desktop-client/src/components/reports/reports/SankeyCard.tsx b/packages/desktop-client/src/components/reports/reports/SankeyCard.tsxindex 1a485bff81..d0696e22d8 100644--- a/packages/desktop-client/src/components/reports/reports/SankeyCard.tsx+++ b/packages/desktop-client/src/components/reports/reports/SankeyCard.tsx@@ -5,6 +5,8 @@ import { useTranslation } from 'react-i18next';import { Block } from '@actual-app/components/block';import { theme } from '@actual-app/components/theme';import { View } from '@actual-app/components/view';+import { send } from '@actual-app/core/platform/client/connection';+import * as monthUtils from '@actual-app/core/shared/months';import type { SankeyWidget } from '@actual-app/core/types/models';import * as d from 'date-fns';import { debounce } from 'es-toolkit/compat';@@ -13,7 +15,10 @@ import { SankeyGraph } from '#components/reports/graphs/SankeyGraph';import { LoadingIndicator } from '#components/reports/LoadingIndicator';import { ReportCard } from '#components/reports/ReportCard';import { ReportCardName } from '#components/reports/ReportCardName';-import { calculateTimeRange } from '#components/reports/reportRanges';+import {+ boundMonthRangeFromDates,+ calculateTimeRange,+} from '#components/reports/reportRanges';import {getDefaultLayerRange,topNNodes,@@ -32,6 +37,11 @@ import { useCategories } from '#hooks/useCategories';import { useLocale } from '#hooks/useLocale';import { useResizeObserver } from '#hooks/useResizeObserver';+const defaultGetBaseGraph = async (+ _spreadsheet: unknown,+ setData: (data: Graph) => void,+) => setData(new Map());+type SankeyCardProps = {widgetId: string;isEditing?: boolean;@@ -47,10 +57,29 @@ export function SankeyCard({const { t } = useTranslation();const locale = useLocale();const [nameMenuOpen, setNameMenuOpen] = useState(false);+ const [earliestTransaction, setEarliestTransaction] = useState('');+ const [latestTransaction, setLatestTransaction] = useState('');+ const [datesInitialized, setDatesInitialized] = useState(false);const { data: { grouped: groupedCategories = [] } = { grouped: [] } } =useCategories();- const [start, end] = calculateTimeRange(meta?.timeFrame);+ useEffect(() => {+ void Promise.all([+ send('get-earliest-transaction'),+ send('get-latest-transaction'),+ ]).then(([earliest, latest]) => {+ const today = monthUtils.currentDay();+ setEarliestTransaction(earliest?.date ?? today);+ setLatestTransaction(latest?.date ?? today);+ setDatesInitialized(true);+ });+ }, []);++ const [start, end] = calculateTimeRange(+ meta?.timeFrame,+ undefined,+ latestTransaction,+ );const mode = meta?.mode ?? 'spent';const [cardHeight, setCardHeight] = useState(0);@@ -93,33 +122,41 @@ export function SankeyCard({const groupAccounts = meta?.groupAccounts ?? false;- const baseGraphParams = useMemo(- () =>- createBaseGraphSpreadsheet(- start,- end,- groupedCategories,- meta?.conditions ?? [],- meta?.conditionsOp ?? 'and',- mode,- groupAccounts,- meta?.showTransfers ?? false,- ),- [+ const baseGraphParams = useMemo(() => {+ if (!datesInitialized) {+ return null;+ }++ const [boundedStart, boundedEnd] = boundMonthRangeFromDates(+ earliestTransaction,+ latestTransaction,start,end,+ );++ return createBaseGraphSpreadsheet(+ boundedStart,+ boundedEnd,groupedCategories,- meta?.conditions,- meta?.conditionsOp,+ meta?.conditions ?? [],+ meta?.conditionsOp ?? 'and',mode,groupAccounts,- meta?.showTransfers,- ],- );- const defaultGetBaseGraph = async (- _spreadsheet: unknown,- setData: (data: Graph) => void,- ) => setData(new Map());+ meta?.showTransfers ?? false,+ );+ }, [+ datesInitialized,+ earliestTransaction,+ latestTransaction,+ start,+ end,+ groupedCategories,+ meta?.conditions,+ meta?.conditionsOp,+ mode,+ groupAccounts,+ meta?.showTransfers,+ ]);const baseGraph = useReport('sankey', baseGraphParams ?? defaultGetBaseGraph);const baseGraphRef = useRef(baseGraph);diff --git a/upcoming-release-notes/sankey-bugfix-no-budget-exists.md b/upcoming-release-notes/sankey-bugfix-no-budget-exists.mdnew file mode 100644index 0000000000..fc4b8c5519--- /dev/null+++ b/upcoming-release-notes/sankey-bugfix-no-budget-exists.md@@ -0,0 +1,6 @@+---+category: Bugfixes+authors: [emiltb]+---++Fix Sankey chart error in Budgeted mode when selecting months without data