safetwitch/src/settingsManager.ts

78 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-08-19 20:37:11 -04:00
export const setLanguage = (selectedLanguage: string, i18n: any) => {
// Locales and languages in arrays to match them
const locales = ['en-US', 'es-ES', 'nl-NL', 'pt-PT', 'fa-IR', 'he-IL', 'ru-RU', 'ko-KR']
const languages = ['English', 'Español', 'Nederlands', 'Português', 'فارسی', 'עִבְרִית', 'Русский', '한국어']
const locale = locales[languages.indexOf(selectedLanguage)]
localStorage.setItem("language", locale)
i18n.locale = locale
}
2023-08-18 14:23:59 -04:00
export function getDefaultSettings() {
return {
version: import.meta.env.SAFETWITCH_TAG,
2023-08-19 20:37:11 -04:00
// audioOnly: {
// name: 'Audio Only',
// selected: false,
// type: 'checkbox'
// },
defaultQuality: {
2023-08-19 20:37:11 -04:00
name: 'defaultQuality',
options: ['160p', '360p', '480p', '720p', '1080p'],
selected: '480p',
type: 'option'
},
2023-08-19 20:37:11 -04:00
language: {
name: 'language',
options: ['English', 'Español', 'Nederlands', 'Português', 'فارسی', 'עִבְרִית', 'Русский', '한국어'],
selected: 'English',
type: 'option'
},
chatVisible: {
2023-08-19 20:37:11 -04:00
name: 'chatVisible',
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
}