restructured preferences

This commit is contained in:
Ollie Taylor
2023-02-25 14:33:17 +00:00
parent e46b929d69
commit 49c5e4eb63
8 changed files with 53 additions and 62 deletions
+37
View File
@@ -0,0 +1,37 @@
import { browser } from '$app/environment';
import { user_preferences } from '$lib/preferences/db-state';
import type { UserPreferences } from '$lib/types';
import { info, warn } from '$lib/utility/package/log';
import { get } from 'svelte/store';
const USER_PREFERENCE_KEY = 'USER_PREFERENCE' as const;
export function save_preferences() {
if (!browser || !localStorage) {
warn('localStorage not available, skipping save');
return;
}
const preferences = get(user_preferences);
info(`Saving user preferences to localStorage`, preferences);
localStorage.setItem(USER_PREFERENCE_KEY, JSON.stringify(preferences));
}
export function load_preferences() {
if (!browser || !localStorage) {
warn('localStorage not available, skipping load');
return;
}
const data = localStorage.getItem(USER_PREFERENCE_KEY);
if (!data) {
info('No saved user preferences found');
return;
}
const preferences = JSON.parse(data) as UserPreferences;
user_preferences.edit(preferences);
info(`Loaded user preferences from localStorage`, preferences);
}