A local-first personal finance app
[AI] Fix live spending reports not updating on month rollover (#9098)
youngcw046239693156
diff --git a/packages/desktop-client/src/components/reports/reportRanges.test.ts b/packages/desktop-client/src/components/reports/reportRanges.test.tsindex 52cbd5e42f..4f65569a8a 100644--- a/packages/desktop-client/src/components/reports/reportRanges.test.ts+++ b/packages/desktop-client/src/components/reports/reportRanges.test.ts@@ -96,29 +96,29 @@ describe('getLatestRange', () => {// In test mode, monthUtils.currentMonth() returns '2017-01'describe('calculateSpendingReportTimeRange', () => {- it('preserves the saved compare month for live average reports', () => {+ it('slides live average reports to the current month', () => {const [compare, compareTo] = calculateSpendingReportTimeRange({compare: '2016-12',isLive: true,mode: 'average',});- expect(compare).toBe('2016-12');- expect(compareTo).toBe('2016-12');+ expect(compare).toBe('2017-01');+ expect(compareTo).toBe('2017-01');});- it('preserves the saved compare month for live budget reports', () => {+ it('slides live budget reports to the current month', () => {const [compare, compareTo] = calculateSpendingReportTimeRange({compare: '2016-12',isLive: true,mode: 'budget',});- expect(compare).toBe('2016-12');- expect(compareTo).toBe('2016-12');+ expect(compare).toBe('2017-01');+ expect(compareTo).toBe('2017-01');});- it('preserves the saved compare months for live single month reports', () => {+ it('slides live single month reports to the current month, preserving the gap', () => {const [compare, compareTo] = calculateSpendingReportTimeRange({compare: '2016-12',compareTo: '2016-11',@@ -126,10 +126,67 @@ describe('calculateSpendingReportTimeRange', () => {mode: 'single-month',});+ expect(compare).toBe('2017-01');+ expect(compareTo).toBe('2016-12');+ });++ it('preserves a wider gap when sliding live single month reports', () => {+ const [compare, compareTo] = calculateSpendingReportTimeRange({+ compare: '2016-12',+ compareTo: '2016-09',+ isLive: true,+ mode: 'single-month',+ });++ expect(compare).toBe('2017-01');+ expect(compareTo).toBe('2016-10'); // gap of 3 preserved+ });++ it('defaults a missing compareTo to the month before compare', () => {+ const [compare, compareTo] = calculateSpendingReportTimeRange({+ compare: '2016-01',+ isLive: true,+ mode: 'single-month',+ });++ expect(compare).toBe('2017-01');+ expect(compareTo).toBe('2016-12');+ });++ it('defaults a missing compareTo for static single month reports', () => {+ const [compare, compareTo] = calculateSpendingReportTimeRange({+ compare: '2016-01',+ isLive: false,+ mode: 'single-month',+ });++ expect(compare).toBe('2016-01');+ expect(compareTo).toBe('2015-12');+ });++ it('preserves the saved compare months for static single month reports', () => {+ const [compare, compareTo] = calculateSpendingReportTimeRange({+ compare: '2016-12',+ compareTo: '2016-11',+ isLive: false,+ mode: 'single-month',+ });+expect(compare).toBe('2016-12');expect(compareTo).toBe('2016-11');});+ it('preserves the saved compare month for static budget reports', () => {+ const [compare] = calculateSpendingReportTimeRange({+ compare: '2016-12',+ compareTo: '2016-12',+ isLive: false,+ mode: 'budget',+ });++ expect(compare).toBe('2016-12');+ });+it('defaults live average reports to the current month without a saved compare month', () => {const [compare, compareTo] = calculateSpendingReportTimeRange({isLive: true,diff --git a/packages/desktop-client/src/components/reports/reportRanges.ts b/packages/desktop-client/src/components/reports/reportRanges.tsindex 92fac9a9c1..40c52be2c5 100644--- a/packages/desktop-client/src/components/reports/reportRanges.ts+++ b/packages/desktop-client/src/components/reports/reportRanges.ts@@ -355,19 +355,20 @@ export function calculateSpendingReportTimeRange({isLive?: boolean;mode?: 'budget' | 'average' | 'single-month';}): [string, string] {+ // Budget and average chart a single month, so a live report is always the+ // current month.if (['budget', 'average'].includes(mode) && isLive) {- const month = compare ?? monthUtils.currentMonth();+ const month = monthUtils.currentMonth();return [month, month];}- if (mode === 'single-month' && isLive && compare) {- return [compare, compareTo ?? monthUtils.subMonths(compare, 1)];- }-+ // Default a missing end to the month before `compare`; the shared default is+ // the month before now, which would widen the comparison.const [start, end] = calculateTimeRange({start: compare,- end: compareTo,+ end:+ compareTo ?? (compare ? monthUtils.subMonths(compare, 1) : undefined),mode: (isLive ?? true) ? 'sliding-window' : 'static',},{diff --git a/packages/desktop-client/src/components/reports/reports/Spending.tsx b/packages/desktop-client/src/components/reports/reports/Spending.tsxindex 19b97e6809..8e8183ff1b 100644--- a/packages/desktop-client/src/components/reports/reports/Spending.tsx+++ b/packages/desktop-client/src/components/reports/reports/Spending.tsx@@ -246,6 +246,28 @@ function SpendingInternal({ widget }: SpendingInternalProps) {}};+ // Picking a month pins the report, as it does in the other reports. The "to"+ // select stays live: its offset from `compare` is what slides.+ const onCompareChange = (value: string) => {+ setCompare(value);+ setIsLive(false);+ };++ const onToggleIsLive = () => {+ const nextIsLive = !isLive;+ if (nextIsLive) {+ const [liveCompare, liveCompareTo] = calculateSpendingReportTimeRange({+ compare,+ compareTo,+ isLive: true,+ mode: reportMode,+ });+ setCompare(liveCompare);+ setCompareTo(liveCompareTo);+ }+ setIsLive(nextIsLive);+ };+const title = widget?.meta?.name || t('Monthly Spending');const onSaveWidgetName = async (newName: string) => {if (!widget) {@@ -304,7 +326,7 @@ function SpendingInternal({ widget }: SpendingInternalProps) {<SpaceBetween gap={0}><Buttonvariant={isLive ? 'primary' : 'normal'}- onPress={() => setIsLive(state => !state)}+ onPress={onToggleIsLive}>{isLive ? t('Live') : t('Static')}</Button>@@ -325,7 +347,7 @@ function SpendingInternal({ widget }: SpendingInternalProps) {</Text><Selectvalue={compare}- onChange={setCompare}+ onChange={onCompareChange}options={allIntervals.map(({ name, pretty }) => [name, pretty] as const,)}diff --git a/upcoming-release-notes/fix-live-spending-report-month.md b/upcoming-release-notes/fix-live-spending-report-month.mdnew file mode 100644index 0000000000..9299bdefdb--- /dev/null+++ b/upcoming-release-notes/fix-live-spending-report-month.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [youngcw]+---++Fix live spending reports not moving to the current month when the month changes