Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 4x 4x 8x 2x 2x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 2x | /**
* @file SettingsStorage.ts
* @description Persistence layer for user preferences using localStorage
*
* Handles all reading/writing of settings to localStorage with error handling.
* Provides language-aware defaults and utility functions for the provider.
*/
import type { UserPreferences } from './SettingsContext.types';
const STORAGE_KEY = 'appSettings';
/**
* Get language-appropriate default preferences
*
* Returns locale-specific defaults based on i18n language:
* - German (de-*): DD.MM.YYYY dates, DE number format
* - Other: MM/DD/YYYY dates, EN_US number format
*
* @param language - i18n language code (e.g., 'de', 'en', 'de-DE')
* @returns Default preferences for the language
*/
export const getDefaultPreferences = (language: string): UserPreferences => {
const isGerman = language?.startsWith('de');
return {
dateFormat: isGerman ? 'DD.MM.YYYY' : 'MM/DD/YYYY',
numberFormat: isGerman ? 'DE' : 'EN_US',
tableDensity: 'comfortable',
};
};
/**
* Load preferences from localStorage
*
* Attempts to parse stored preferences, falling back to defaults if:
* - No stored preferences exist
* - Stored data is corrupted/unparseable
* - localStorage is unavailable
*
* Logs warning but doesn't throw (graceful degradation).
*
* @param language - i18n language for defaults
* @returns Stored preferences or language defaults
*/
export const loadPreferencesFromStorage = (language: string): UserPreferences => {
try {
const stored = localStorage.getItem(STORAGE_KEY);
if (stored) {
return JSON.parse(stored) as UserPreferences;
}
} catch (error) {
console.warn('Failed to load settings from localStorage:', error);
}
return getDefaultPreferences(language);
};
/**
* Save preferences to localStorage
*
* Serializes preferences and persists to localStorage.
* Logs warning on failure but doesn't throw (non-blocking).
*
* @param prefs - User preferences to persist
*/
export const savePreferencesToStorage = (prefs: UserPreferences): void => {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(prefs));
} catch (error) {
console.warn('Failed to save settings to localStorage:', error);
}
};
/**
* Clear preferences from localStorage
*
* Removes the entire settings entry. Next load will use fresh defaults.
* Logs warning on failure but doesn't throw.
*/
export const clearPreferencesFromStorage = (): void => {
try {
localStorage.removeItem(STORAGE_KEY);
} catch (error) {
console.warn('Failed to clear settings from localStorage:', error);
}
};
|