perf(bookkeeping): fiscal-year pickers and cash accounts read the session cache (#1934)

First consumer migration onto lib/reference-data. FyPicker and
FiscalYearSelector (14 consumer surfaces, 47 fiscal-period fetch sites
before this series) now read useFiscalPeriods(); with the layout seed the
restore of the persisted scope runs in the first effect tick and onReady
fires on mount instead of after a round trip. Their restore rules are
extracted into a pure resolveInitialFiscalScope() (lib/reference-data/
fiscal-scope.ts) so the two pickers cannot drift apart again, and the
restore runs once per company load, not on every background revalidation.

- /reports: the static catalog renders immediately; only the "no fiscal
  year" empty state waits for the picker (previously six skeleton bars
  until /api/bookkeeping/fiscal-periods resolved).
- JournalEntryList (/bookkeeping): resolves its initial scope from the
  cached list instead of its own fetch; the saved-scope shortcut still
  unblocks the entries fetch first when nothing is cached, and resolution
  is guarded to once per company so a revalidation can never snap a
  deep-link "all years" visit back to the stored year.
- /transactions: the account chooser reads useCashAccounts({ enabledOnly })
  (seeded) instead of fetching /api/cash-accounts on every visit; the bank
  sync button invalidates that entry after a sync.
- STORAGE_KEY_PREFIX / ALL_YEARS_VALUE move to a dependency-free
  fiscal-year-storage.ts (re-exported from FiscalYearSelector) so lib/ code
  can import them without a React component.

raw-reference-fetch ratchet: 55 -> 51 files.

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-08-26 14:24:24 +02:00
committed by GitHub
co-authored by Jakob Wennberg Claude Fable 5
parent b436d47b64
commit 9a56b7aff9
10 changed files with 282 additions and 195 deletions
@@ -0,0 +1,89 @@
import { describe, it, expect } from 'vitest'
import { prepareFiscalPeriods, resolveInitialFiscalScope } from '../fiscal-scope'
import { ALL_YEARS_VALUE } from '@/components/common/fiscal-year-storage'
import type { FiscalPeriod } from '@/types'
const period = (id: string, start: string, end: string) =>
({ id, name: id, period_start: start, period_end: end }) as unknown as FiscalPeriod
const p2024 = period('p2024', '2024-01-01', '2024-12-31')
const p2025 = period('p2025', '2025-01-01', '2025-12-31')
const p2026 = period('p2026', '2026-01-01', '2026-12-31')
const p2027 = period('p2027', '2027-01-01', '2027-12-31')
const TODAY = '2026-08-26'
describe('prepareFiscalPeriods', () => {
it('sorts newest first and can hide periods that have not started', () => {
const all = prepareFiscalPeriods([p2024, p2027, p2026, p2025], false, TODAY)
expect(all.map((p) => p.id)).toEqual(['p2027', 'p2026', 'p2025', 'p2024'])
const started = prepareFiscalPeriods([p2024, p2027, p2026, p2025], true, TODAY)
expect(started.map((p) => p.id)).toEqual(['p2026', 'p2025', 'p2024'])
})
it('does not mutate the input', () => {
const input = [p2024, p2026]
prepareFiscalPeriods(input, false, TODAY)
expect(input.map((p) => p.id)).toEqual(['p2024', 'p2026'])
})
})
describe('resolveInitialFiscalScope', () => {
const periods = prepareFiscalPeriods([p2024, p2025, p2026], false, TODAY)
it('restores a persisted period that still exists', () => {
expect(resolveInitialFiscalScope(periods, 'p2025', { includeAllOption: true })).toEqual({
periodId: 'p2025',
period: p2025,
})
})
it('ignores a stale persisted id and falls back per the surface', () => {
expect(resolveInitialFiscalScope(periods, 'gone', { includeAllOption: true })).toBeNull()
expect(resolveInitialFiscalScope(periods, 'gone', { includeAllOption: false })).toEqual({
periodId: 'p2026',
period: p2026,
})
})
it('honours an explicit "all years" only where the surface allows it', () => {
expect(resolveInitialFiscalScope(periods, ALL_YEARS_VALUE, { includeAllOption: true })).toEqual({
periodId: null,
period: null,
})
expect(resolveInitialFiscalScope(periods, ALL_YEARS_VALUE, { includeAllOption: false })).toEqual({
periodId: 'p2026',
period: p2026,
})
})
it('with nothing stored: all-years surfaces stay unfiltered, others pick the newest', () => {
expect(resolveInitialFiscalScope(periods, null, { includeAllOption: true })).toBeNull()
expect(resolveInitialFiscalScope(periods, null, { includeAllOption: false })).toEqual({
periodId: 'p2026',
period: p2026,
})
})
it('preferLatestEnded opens on the most recently ended period and ignores the stored choice', () => {
expect(
resolveInitialFiscalScope(periods, 'p2024', { includeAllOption: false, preferLatestEnded: true, today: TODAY }),
).toEqual({ periodId: 'p2025', period: p2025 })
})
it('preferLatestEnded falls back to the newest period when none has ended', () => {
const onlyCurrent = prepareFiscalPeriods([p2026], false, TODAY)
expect(
resolveInitialFiscalScope(onlyCurrent, null, { includeAllOption: false, preferLatestEnded: true, today: TODAY }),
).toEqual({ periodId: 'p2026', period: p2026 })
})
it('returns null for an empty list on every path', () => {
expect(resolveInitialFiscalScope([], null, { includeAllOption: false })).toBeNull()
expect(resolveInitialFiscalScope([], ALL_YEARS_VALUE, { includeAllOption: false })).toBeNull()
expect(resolveInitialFiscalScope([], 'x', { includeAllOption: false, preferLatestEnded: true })).toBeNull()
expect(resolveInitialFiscalScope([], ALL_YEARS_VALUE, { includeAllOption: true })).toEqual({
periodId: null,
period: null,
})
})
})
+75
View File
@@ -0,0 +1,75 @@
/**
* Initial fiscal-year scope resolution shared by FyPicker and
* FiscalYearSelector. Pure so the restore rules (persisted choice, "all
* years", newest started period, most recently ended period) can be tested
* without React, and so both pickers cannot drift apart again.
*/
import type { FiscalPeriod } from '@/types'
import { ALL_YEARS_VALUE } from '@/components/common/fiscal-year-storage'
export function todayIso(): string {
return new Date().toISOString().split('T')[0]
}
/** Newest first; optionally drops periods that have not started yet. */
export function prepareFiscalPeriods(
periods: readonly FiscalPeriod[],
hideFuturePeriods: boolean,
today: string = todayIso(),
): FiscalPeriod[] {
return periods
.filter((p) => !hideFuturePeriods || p.period_start <= today)
.sort((a, b) => b.period_start.localeCompare(a.period_start))
}
export interface FiscalScopeOptions {
/** Whether "all years" (null) is a valid selection on this surface. */
includeAllOption: boolean
/**
* Filing surfaces: ignore the persisted choice and open on the most
* recently ended period (only an ended year can be declared).
*/
preferLatestEnded?: boolean
today?: string
}
export interface FiscalScopePick {
periodId: string | null
period: FiscalPeriod | null
}
/**
* What the picker should select on load, or null when nothing should be
* auto-selected. `periods` must already be prepared (newest first);
* `stored` is the raw persisted value for this company (or null).
*/
export function resolveInitialFiscalScope(
periods: readonly FiscalPeriod[],
stored: string | null,
options: FiscalScopeOptions,
): FiscalScopePick | null {
const newest = periods[0] ?? null
if (options.preferLatestEnded) {
const today = options.today ?? todayIso()
const pick = periods.find((p) => p.period_end < today) ?? newest
return pick ? { periodId: pick.id, period: pick } : null
}
if (stored === ALL_YEARS_VALUE) {
if (options.includeAllOption) return { periodId: null, period: null }
return newest ? { periodId: newest.id, period: newest } : null
}
if (stored) {
const match = periods.find((p) => p.id === stored)
if (match) return { periodId: match.id, period: match }
}
if (!options.includeAllOption && newest) {
return { periodId: newest.id, period: newest }
}
return null
}