Files
accounted/lib/reference-data/seed.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

41 lines
1.6 KiB
TypeScript

/**
* Server-to-client seed for the reference-data cache.
*
* The dashboard layout already fetches company settings for its own nav;
* adding the (small) fiscal-period and cash-account lists to that batch and
* handing all three to SWR as `fallback` means the first form a user opens
* renders its period, bank-account and settings-driven fields on the first
* paint with zero client round trips. SWR consults `fallback` only while
* the cache has no entry for the key, so after the first background
* revalidation or any mutate() the live cache wins; a hard reload re-seeds.
*
* Keys are serialized with SWR's own `unstable_serialize` and built from
* `refKeys`, so the seed can never drift from the hooks (pinned by a test).
*/
import { unstable_serialize } from 'swr'
import type { CashAccount, CompanySettings, FiscalPeriod } from '@/types'
import { refKeys } from './keys'
export interface ReferenceSeed {
fiscalPeriods: FiscalPeriod[]
cashAccounts: CashAccount[]
/** `undefined` = not fetched (do not seed); `null` = no settings row yet. */
settings?: CompanySettings | null
}
export function buildReferenceFallback(
companyId: string | null,
seed: ReferenceSeed,
): Record<string, unknown> {
if (!companyId) return {}
const fallback: Record<string, unknown> = {
[unstable_serialize(refKeys.fiscalPeriods(companyId))]: seed.fiscalPeriods,
[unstable_serialize(refKeys.cashAccounts(companyId))]: seed.cashAccounts,
}
if (seed.settings !== undefined) {
fallback[unstable_serialize(refKeys.companySettings(companyId))] = seed.settings
}
return fallback
}