9a56b7aff9
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>
76 lines
2.3 KiB
TypeScript
76 lines
2.3 KiB
TypeScript
/**
|
|
* 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
|
|
}
|