misc: Add setting

This commit is contained in:
Xyphyn committed 2023-07-10 09:49:05 -07:00
1 parent fab41a6784
commit ae07e8b14b
5 files changed
+76 -5

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
import { writable } from 'svelte/store'
interface Settings {
newComments: boolean
}
const defaultSettings: Settings = {
newComments: true,
}
export const userSettings = writable(defaultSettings)
function mergeObjects<T>(obj1: any, obj2: any): T {
for (let prop in obj2) {
if (!(prop in obj1) || obj1[prop] == null || obj2[prop] == '') {
obj1[prop] = obj2[prop]
}
}
return obj1
}
if (typeof window != 'undefined') {
let oldUserSettings = JSON.parse(
localStorage.getItem('settings') ?? JSON.stringify(defaultSettings)
)
userSettings.set(mergeObjects<Settings>(oldUserSettings, defaultSettings))
}
userSettings.subscribe((settings) => {
if (typeof window != 'undefined') {
localStorage.setItem('settings', JSON.stringify(settings))
}
})