aboutsummaryrefslogtreecommitdiff
path: root/src/stores/settings.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/stores/settings.ts')
-rw-r--r--src/stores/settings.ts61
1 files changed, 34 insertions, 27 deletions
diff --git a/src/stores/settings.ts b/src/stores/settings.ts
index fef0debf..c8ae9c94 100644
--- a/src/stores/settings.ts
+++ b/src/stores/settings.ts
@@ -4,6 +4,7 @@ import { get, writable } from 'svelte/store';
import settingsSyncPulled from './settingsSyncPulled';
import settingsSyncTimes from './settingsSyncTimes';
import identity from './identity';
+import localforage from 'localforage';
const VERSION = '1.0.1';
@@ -135,22 +136,26 @@ const defaultSettings: Settings = {
};
const createStore = () => {
- const { subscribe, set, update } = writable<Settings>(
- JSON.parse(
- browser
- ? (localStorage.getItem('settings') ?? JSON.stringify(defaultSettings))
- : JSON.stringify(defaultSettings)
- )
- );
- let state: Settings;
+ const store = writable<Settings>(defaultSettings);
+ let state: Settings = defaultSettings;
- subscribe((value) => (state = value));
+ if (browser)
+ localforage.getItem<Settings>('settings').then((value) => {
+ if (value && typeof value === 'object') store.set(value);
+ });
+
+ store.subscribe((value) => {
+ state = value;
+
+ if (browser) localforage.setItem('settings', value);
+ });
return {
- subscribe,
- set,
- update,
- reset: () => set(defaultSettings),
+ subscribe: store.subscribe,
+ set: store.set,
+ update: store.update,
+ reset: () => store.set(defaultSettings),
+
get: () => {
const keys = Object.keys(defaultSettings);
const settingsKeys = Object.keys(state);
@@ -158,51 +163,53 @@ const createStore = () => {
for (const key of keys)
if (!settingsKeys.includes(key))
- (updatedSettings[key as keyof Settings] as unknown) =
- defaultSettings[key as keyof Settings];
+ updatedSettings[key as keyof Settings] = defaultSettings[key as keyof Settings];
- if (browser) localStorage.setItem('settings', JSON.stringify(updatedSettings));
+ if (browser) localforage.setItem('settings', updatedSettings);
return updatedSettings;
},
+
setKey: (key: keyof Settings, value: unknown) =>
- update((settings) => ({ ...settings, [key]: value }))
+ store.update((settings) => ({ ...settings, [key]: value }))
};
};
const settings = createStore();
settings.subscribe((value) => {
- if (browser) localStorage.setItem('settings', JSON.stringify(value));
+ if (!browser) return;
- if (value.settingsSync && get(settingsSyncPulled) == true)
+ if (value.settingsSync && get(settingsSyncPulled) === true) {
fetch(root(`/api/configuration?id=${get(identity).id}`)).then((response) => {
if (response.ok)
response.json().then((data) => {
- const isEqualsJson = (object1: Settings, object2: Settings) => {
+ const isEqualsJson = (firstObject: Settings, secondObject: Settings) => {
type AnyObject = { [key: string]: unknown };
return (
- Object.keys(object1).length === Object.keys(object2).length &&
- Object.keys(object1).every(
- (key) =>
- (object1 as unknown as AnyObject)[key] == (object2 as unknown as AnyObject)[key]
+ Object.keys(firstObject).length === Object.keys(secondObject).length &&
+ Object.keys(firstObject).every(
+ (key) => (firstObject as AnyObject)[key] === (secondObject as AnyObject)[key]
)
);
};
- if (data && data.configuration && !isEqualsJson(data.configuration, value)) {
+ if (data?.configuration && !isEqualsJson(data.configuration, value))
fetch(root(`/api/configuration`), {
method: 'PUT',
body: JSON.stringify(value)
}).then((response) => {
if (response.ok) console.log('Pushed local configuration');
- settingsSyncTimes.update((times) => ({ ...times, lastPush: new Date() }));
+ settingsSyncTimes.update((times) => ({
+ ...times,
+ lastPush: new Date()
+ }));
});
- }
});
});
+ }
});
export default settings;