0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch.git synced 2024-12-22 13:22:58 -05:00

Sync user settings with newer versions on changes

This commit is contained in:
dragongoose 2023-08-18 14:40:25 -04:00
parent 12d6bd2c38
commit 39808373cb
No known key found for this signature in database
GPG key ID: 01397EEC371CDAA5
2 changed files with 48 additions and 29 deletions

View file

@ -1,42 +1,60 @@
export function getDefaultSettings() { export function getDefaultSettings() {
return { return {
version: import.meta.env.SAFETWITCH_TAG, version: import.meta.env.SAFETWITCH_TAG,
audioOnly: { audioOnly: {
name: 'Audio Only', name: 'Audio Only',
selected: false, selected: false,
type: 'checkbox' type: 'checkbox'
}, },
defaultQuality: { defaultQuality: {
name: 'Default Quality', name: 'Default Quality',
options: ['160p', '360p', '480p', '720p', '1080p'], options: ['160p', '360p', '480p', '720p', '1080p'],
selected: '480p', selected: '480p',
type: 'option' type: 'option'
}, },
chatVisible: { chatVisible: {
name: 'Hide Chat', name: 'Hide Chat',
selected: false, selected: false,
type: 'checkbox' type: 'checkbox'
} }
} }
} }
export function getSetting(key: string): boolean | string { 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}
localStorage.setItem('settings', JSON.stringify(synced))
console.log('Migrated!')
}
export function getSetting(key: string): boolean | string {
const storage = localStorage.getItem('settings') const storage = localStorage.getItem('settings')
let parsed let parsed
if (!storage) { if (!storage) {
parsed = getDefaultSettings() parsed = getDefaultSettings()
} else { } else {
parsed = JSON.parse(storage) parsed = JSON.parse(storage)
} }
return parsed[key].selected return parsed[key].selected
} }
export function chatVisible() { export function chatVisible() {
const p = getSetting('chatVisible') const p = getSetting('chatVisible')
// Flip becuase on the setting page it's // Flip becuase on the setting page it's
// displayed as "Hide Chat", but the value // displayed as "Hide Chat", but the value
// is chatVisible // is chatVisible
return !p return !p
} }

View file

@ -1,9 +1,10 @@
<script lang="ts"> <script lang="ts">
import { getDefaultSettings } from '@/settingsManager' import { getDefaultSettings, syncUserSettings } from '@/settingsManager'
export default { export default {
setup() { setup() {
let settings let settings
syncUserSettings()
let storedSettings = localStorage.getItem('settings') let storedSettings = localStorage.getItem('settings')
if (storedSettings === null) { if (storedSettings === null) {