Files
accounted/lib/ui-state/client.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

45 lines
1.4 KiB
TypeScript

'use client'
import type { UserUiState } from '@/types'
/**
* Fire-and-forget persistence of a partial user_preferences.ui_state patch
* (nav collapse/folds, split-button last-used modes). Cosmetic preference
* data: a lost write self-corrects on the next change, so failures are
* swallowed deliberately.
*/
export function persistUiState(patch: Partial<UserUiState>): void {
void fetch('/api/user/ui-state', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(patch),
}).catch(() => {})
}
/**
* Remember the last-used mode of a split button (ui_state.create_mode),
* keyed per surface (e.g. 'bookkeeping' -> 'mall').
*/
export function rememberCreateMode(surface: string, mode: string): void {
persistUiState({ create_mode: { [surface]: mode } })
}
/**
* Resolve which split-button mode to show as primary on first render:
* the persisted last-used mode when it's still one of the valid options,
* otherwise the given fallback. Guards against stale persisted keys after
* an option is renamed or removed.
*/
export function resolveInitialMode<T extends string>(
uiState: UserUiState | undefined | null,
surface: string,
validKeys: readonly T[],
fallback: T,
): T {
const persisted = uiState?.create_mode?.[surface]
if (persisted && (validKeys as readonly string[]).includes(persisted)) {
return persisted as T
}
return fallback
}