safetwitch/src/settingsManager.ts

62 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-08-18 14:23:59 -04:00
export function getDefaultSettings() {
return {
version: import.meta.env.SAFETWITCH_TAG,
audioOnly: {
name: 'Audio Only',
selected: false,
type: 'checkbox'
},
defaultQuality: {
name: 'Default Quality',
options: ['160p', '360p', '480p', '720p', '1080p'],
selected: '480p',
type: 'option'
},
chatVisible: {
name: 'Hide Chat',
selected: false,
type: 'checkbox'
}
2023-08-18 14:23:59 -04:00
}
}
export function syncUserSettings() {
const defaultSettings = getDefaultSettings()
let userSettings = localStorage.getItem('settings')
if (!userSettings) return
const parsedUserSettings = JSON.parse(userSettings)
if(parsedUserSettings.version === import.meta.env.SAFETWITCH_TAG) {
console.log('Settings up to date!')
return
} else {
console.log('Settings outdated... Migrating')
}
const synced = {...defaultSettings, ...parsedUserSettings}
synced.version = import.meta.env.SAFETWITCH_TAG
localStorage.setItem('settings', JSON.stringify(synced))
console.log('Migrated!')
}
export function getSetting(key: string): boolean | string {
2023-08-18 14:23:59 -04:00
const storage = localStorage.getItem('settings')
let parsed
if (!storage) {
parsed = getDefaultSettings()
2023-08-18 14:23:59 -04:00
} else {
parsed = JSON.parse(storage)
2023-08-18 14:23:59 -04:00
}
2023-08-18 14:23:59 -04:00
return parsed[key].selected
}
export function chatVisible() {
2023-08-18 14:23:59 -04:00
const p = getSetting('chatVisible')
// Flip becuase on the setting page it's
// displayed as "Hide Chat", but the value
// is chatVisible
return !p
}