Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

fix(reports): correct the period selector's year stepping and date language (#1298)

Tássio Noronhac8e40b6b53c2
diff --git a/docs/reports-period-navigation/after.png b/docs/reports-period-navigation/after.png
new file mode 100644
index 00000000..0e2050ee
Binary files /dev/null and b/docs/reports-period-navigation/after.png differ
diff --git a/docs/reports-period-navigation/before.png b/docs/reports-period-navigation/before.png
new file mode 100644
index 00000000..5bf5e9bb
Binary files /dev/null and b/docs/reports-period-navigation/before.png differ
diff --git a/frontend/src/components/ui/date-range-picker.tsx b/frontend/src/components/ui/date-range-picker.tsx
index d6bd28ab..3ab7c7e9 100644
--- a/frontend/src/components/ui/date-range-picker.tsx
+++ b/frontend/src/components/ui/date-range-picker.tsx
@@ -14,7 +14,7 @@ import { cn } from '@/lib/utils'
import { localDateString } from '@/lib/date-utils'
import { formatDateRange } from '@/lib/date-range-format'
import { resolveDateFnsLocale } from '@/lib/date-fns-locale'
-import { useDisplayLocale } from '@/hooks/use-display-locale'
+import { useDateLocale } from '@/hooks/use-display-locale'
export interface DateRangePickerProps {
/** Inclusive start date as YYYY-MM-DD, or empty string when unset. */
@@ -87,7 +87,9 @@ export function DateRangePicker({
maxRangeYears,
}: DateRangePickerProps) {
const { t, i18n } = useTranslation()
- const dateLocale = useDisplayLocale()
+ // The trigger label has to read in the same language as the calendar below
+ // it, so both follow the app language rather than the currency locale.
+ const dateLocale = useDateLocale()
const dateFnsLocale = resolveDateFnsLocale(i18n.resolvedLanguage ?? i18n.language)
const [open, setOpen] = useState(false)
const [draftFrom, setDraftFrom] = useState(from)
diff --git a/frontend/src/lib/report-period.test.ts b/frontend/src/lib/report-period.test.ts
index 50ccf136..f9239763 100644
--- a/frontend/src/lib/report-period.test.ts
+++ b/frontend/src/lib/report-period.test.ts
@@ -1,5 +1,13 @@
import { describe, expect, it } from 'vitest'
-import { addDaysIso, addMonthsIso, daysBetween, periodWindow, rollingStart, windowMonths } from './report-period'
+import {
+ addDaysIso,
+ addMonthsIso,
+ alignedSpanMonths,
+ daysBetween,
+ periodWindow,
+ rollingStart,
+ windowMonths,
+} from './report-period'
describe('addDaysIso', () => {
it('shifts within a month', () => {
@@ -178,4 +186,53 @@ describe('periodWindow: custom ranges', () => {
to: '2026-04-20',
})
})
+
+ it('steps a picked year back one year, not thirteen months', () => {
+ const year = { from: '2025-01-01', to: '2025-12-31' }
+ expect(periodWindow({ kind: 'custom' }, '2026-09-15', -1, year)).toEqual({
+ from: '2024-01-01',
+ to: '2024-12-31',
+ })
+ })
+
+ it('steps a picked year forward again to where it started', () => {
+ const year = { from: '2025-01-01', to: '2025-12-31' }
+ expect(periodWindow({ kind: 'custom' }, '2026-09-15', 1, year)).toEqual({
+ from: '2026-01-01',
+ to: '2026-12-31',
+ })
+ })
+
+ it('keeps February whole when a leap year steps back', () => {
+ const window = { from: '2024-01-01', to: '2024-02-29' }
+ expect(periodWindow({ kind: 'custom' }, '2026-09-15', -1, window)).toEqual({
+ from: '2023-11-01',
+ to: '2023-12-31',
+ })
+ })
+
+ it('steps a half-year of whole months back by six months', () => {
+ const half = { from: '2026-07-01', to: '2026-12-31' }
+ expect(periodWindow({ kind: 'custom' }, '2026-09-15', -1, half)).toEqual({
+ from: '2026-01-01',
+ to: '2026-06-30',
+ })
+ })
+})
+
+describe('alignedSpanMonths', () => {
+ it('counts the months a range of whole months covers', () => {
+ expect(alignedSpanMonths({ from: '2025-01-01', to: '2025-12-31' })).toBe(12)
+ expect(alignedSpanMonths({ from: '2026-07-01', to: '2026-12-31' })).toBe(6)
+ expect(alignedSpanMonths({ from: '2026-03-01', to: '2026-03-31' })).toBe(1)
+ expect(alignedSpanMonths({ from: '2024-02-01', to: '2024-02-29' })).toBe(1)
+ })
+
+ it('declines a range that does not start on the first', () => {
+ expect(alignedSpanMonths({ from: '2026-05-10', to: '2026-08-31' })).toBeNull()
+ })
+
+ it('declines a range that stops short of the month end', () => {
+ expect(alignedSpanMonths({ from: '2026-01-01', to: '2026-10-26' })).toBeNull()
+ })
})
diff --git a/frontend/src/lib/report-period.ts b/frontend/src/lib/report-period.ts
index 912163f4..d9bd8766 100644
--- a/frontend/src/lib/report-period.ts
+++ b/frontend/src/lib/report-period.ts
@@ -66,6 +66,20 @@ export function rollingStart(today: string, months: number): string {
return addDaysIso(iso(y, m, 1), -(months * 30)).slice(0, 8) + '01'
}
+/**
+ * Whole calendar months a range covers, or null when it does not line up with
+ * month boundaries. A range that starts on the 1st and ends on a month's last
+ * day has an exact width in months; the day-based estimate below does not,
+ * because a calendar year is 364 or 365 days and rounds up to thirteen.
+ */
+export function alignedSpanMonths(window: PeriodWindow): number | null {
+ const s = parseIso(window.from)
+ const t = parseIso(window.to)
+ if (s.d !== 1) return null
+ if (t.d !== new Date(t.y, t.m, 0).getDate()) return null
+ return (t.y - s.y) * 12 + (t.m - s.m) + 1
+}
+
/**
* Window width in months: ceil of the span in days / 30, clamped to 1..24.
* Same rule the page uses when it sends a custom range, so shifted windows
@@ -90,8 +104,9 @@ export function windowMonths(window: PeriodWindow): number {
* `offset` counts periods of the preset (months, financial years, the
* rolling length or the custom span), not calendar months.
* - `custom` at offset 0 returns null (the page already sends the picked
- * dates); negative offsets step the picked range back by its own length in
- * months.
+ * dates); negative offsets step the picked range back by its own length,
+ * in whole months when the range covers whole months, otherwise by its
+ * rounded span.
*/
export function periodWindow(
preset: PeriodPreset,
@@ -140,6 +155,17 @@ export function periodWindow(
}
case 'custom': {
if (offset === 0 || !customRange) return null
+ // A range that covers whole months (the year picker sends one, and so
+ // does any month-to-month pick) steps by exactly that many months, each
+ // window ending the day before the next one starts. Rounding its day
+ // span instead would move a year back by thirteen months.
+ const aligned = alignedSpanMonths(customRange)
+ if (aligned !== null) {
+ return {
+ from: addMonthsIso(customRange.from, aligned * offset),
+ to: addDaysIso(addMonthsIso(customRange.from, aligned * (offset + 1)), -1),
+ }
+ }
const spanMonths = Math.max(1, Math.ceil(daysBetween(customRange.from, customRange.to) / 30))
return {
from: addMonthsIso(customRange.from, spanMonths * offset),
diff --git a/frontend/src/pages/reports.tsx b/frontend/src/pages/reports.tsx
index 242cfba6..39672a73 100644
--- a/frontend/src/pages/reports.tsx
+++ b/frontend/src/pages/reports.tsx
@@ -1,7 +1,7 @@
import { useContext, useEffect, useState } from 'react'
import { loadReportsRange, saveReportsRange, type ReportsRange } from '@/lib/remembered-range'
import { useTranslation } from 'react-i18next'
-import { useDisplayLocale } from '@/hooks/use-display-locale'
+import { useDateLocale, useDisplayLocale } from '@/hooks/use-display-locale'
import { useQuery } from '@tanstack/react-query'
import {
AreaChart,
@@ -72,7 +72,7 @@ function formatCompact(value: number, currency = 'USD', locale = 'en-US') {
}).format(value)
}
-/** "Apr 2026" from a YYYY-MM-DD date, in the display locale. */
+/** "Apr 2026" from a YYYY-MM-DD date, in the date locale. */
function formatShortMonth(isoDate: string, locale: string) {
const [y, m] = isoDate.split('-').map(Number)
return new Date(y, m - 1, 1).toLocaleDateString(locale, { month: 'short', year: 'numeric' })
@@ -226,6 +226,9 @@ export default function ReportsPage() {
const { user } = useAuth()
const userCurrency = user?.preferences?.currency_display ?? 'USD'
const locale = useDisplayLocale()
+ // Month and day words follow the app language, not the currency locale: a
+ // Portuguese UI showing US dollars still reads "Setembro de 2026".
+ const dateLocale = useDateLocale()
const customDefaults = defaultCustomRange()
// The page opens on the first tab with the range last used there (issue
@@ -330,9 +333,9 @@ export default function ReportsPage() {
isCustomRange && !activeWindow
? t('reports.customRange')
: activeWindow && selectedRange.monthAnchor !== undefined
- ? monthLabel(activeWindow.from.slice(0, 7), locale)
+ ? monthLabel(activeWindow.from.slice(0, 7), dateLocale)
: activeWindow
- ? `${formatShortMonth(activeWindow.from, locale)} - ${formatShortMonth(activeWindow.to, locale)}`
+ ? `${formatShortMonth(activeWindow.from, dateLocale)} - ${formatShortMonth(activeWindow.to, dateLocale)}`
: t(`reports.${RANGE_LABELS[selectedRange.key]}`)
const handleSelectTab = (key: string) => {
@@ -459,7 +462,7 @@ export default function ReportsPage() {
const formatDay = (iso: string) => {
const day = new Date(iso + 'T00:00:00')
const sameYear = day.getFullYear() === new Date().getFullYear()
- return day.toLocaleDateString(locale, {
+ return day.toLocaleDateString(dateLocale, {
day: 'numeric',
month: 'short',
...(sameYear ? {} : { year: 'numeric' }),