2023-02-25 01:13:56 +00:00
|
|
|
import { browser } from '$app/environment';
|
2023-02-25 14:33:17 +00:00
|
|
|
import { user_preferences } from '$lib/preferences/db-state';
|
|
|
|
|
|
2023-02-25 01:13:56 +00:00
|
|
|
import type { UserPreferences } from '$lib/types';
|
|
|
|
|
import { info, warn } from '$lib/utility/package/log';
|
2023-02-25 14:33:17 +00:00
|
|
|
import { get } from 'svelte/store';
|
2023-02-25 01:13:56 +00:00
|
|
|
|
|
|
|
|
const USER_PREFERENCE_KEY = 'USER_PREFERENCE' as const;
|
|
|
|
|
|
2023-02-25 14:33:17 +00:00
|
|
|
export function save_preferences() {
|
2023-02-25 01:13:56 +00:00
|
|
|
if (!browser || !localStorage) {
|
|
|
|
|
warn('localStorage not available, skipping save');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-25 14:33:17 +00:00
|
|
|
|
|
|
|
|
const preferences = get(user_preferences);
|
|
|
|
|
info(`Saving user preferences to localStorage`, preferences);
|
|
|
|
|
localStorage.setItem(USER_PREFERENCE_KEY, JSON.stringify(preferences));
|
2023-02-25 01:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
2023-02-25 14:33:17 +00:00
|
|
|
export function load_preferences() {
|
2023-02-25 01:13:56 +00:00
|
|
|
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;
|
2023-02-25 14:33:17 +00:00
|
|
|
user_preferences.edit(preferences);
|
2023-02-25 01:13:56 +00:00
|
|
|
|
|
|
|
|
info(`Loaded user preferences from localStorage`, preferences);
|
|
|
|
|
}
|