feat: now save actually changed options instead of all options in new localstorage key changedSettings for clarity
This commit is contained in:
parent
b32bab8211
commit
cd9b796f16
2 changed files with 29 additions and 6 deletions
|
|
@ -167,6 +167,18 @@ const migrateOptions = (options: Partial<AppOptions & Record<string, any>>) => {
|
|||
|
||||
return options
|
||||
}
|
||||
const migrateOptionsLocalStorage = () => {
|
||||
if (Object.keys(appStorage.options).length) {
|
||||
for (const key of Object.keys(appStorage.options)) {
|
||||
if (!(key in defaultOptions)) continue // drop unknown options
|
||||
const defaultValue = defaultOptions[key]
|
||||
if (JSON.stringify(defaultValue) !== JSON.stringify(appStorage.options[key])) {
|
||||
appStorage.changedSettings[key] = appStorage.options[key]
|
||||
}
|
||||
}
|
||||
appStorage.options = {}
|
||||
}
|
||||
}
|
||||
|
||||
export type AppOptions = typeof defaultOptions
|
||||
|
||||
|
|
@ -187,14 +199,15 @@ const isDeepEqual = (a: any, b: any): boolean => {
|
|||
|
||||
export const getChangedSettings = () => {
|
||||
return Object.fromEntries(
|
||||
Object.entries(options).filter(([key, value]) => !isDeepEqual(defaultOptions[key], value))
|
||||
Object.entries(appStorage.changedSettings).filter(([key, value]) => !isDeepEqual(defaultOptions[key], value))
|
||||
)
|
||||
}
|
||||
|
||||
migrateOptionsLocalStorage()
|
||||
export const options: AppOptions = proxy({
|
||||
...defaultOptions,
|
||||
...initialAppConfig.defaultSettings,
|
||||
...migrateOptions(appStorage.options),
|
||||
...migrateOptions(appStorage.changedSettings),
|
||||
...qsOptions
|
||||
})
|
||||
|
||||
|
|
@ -210,10 +223,17 @@ Object.defineProperty(window, 'debugChangedOptions', {
|
|||
},
|
||||
})
|
||||
|
||||
subscribe(options, () => {
|
||||
// Don't save disabled settings to localStorage
|
||||
const saveOptions = omitObj(options, [...disabledSettings.value] as any)
|
||||
appStorage.options = saveOptions
|
||||
subscribe(options, (ops) => {
|
||||
for (const op of ops) {
|
||||
const [type, path, value] = op
|
||||
// let patch
|
||||
// let accessor = options
|
||||
// for (const part of path) {
|
||||
// }
|
||||
const key = path[0] as string
|
||||
if (disabledSettings.value.has(key)) continue
|
||||
appStorage.changedSettings[key] = options[key]
|
||||
}
|
||||
})
|
||||
|
||||
type WatchValue = <T extends Record<string, any>>(proxy: T, callback: (p: T, isChanged: boolean) => void) => () => void
|
||||
|
|
|
|||
|
|
@ -32,7 +32,9 @@ type StorageData = {
|
|||
customCommands: Record<string, CustomCommand> | undefined
|
||||
username: string | undefined
|
||||
keybindings: UserOverridesConfig | undefined
|
||||
/** @deprecated */
|
||||
options: any
|
||||
changedSettings: any
|
||||
proxiesData: SavedProxiesData | undefined
|
||||
serversHistory: ServerHistoryEntry[]
|
||||
authenticatedAccounts: AuthenticatedAccount[]
|
||||
|
|
@ -72,6 +74,7 @@ const defaultStorageData: StorageData = {
|
|||
username: undefined,
|
||||
keybindings: undefined,
|
||||
options: {},
|
||||
changedSettings: {},
|
||||
proxiesData: undefined,
|
||||
serversHistory: [],
|
||||
authenticatedAccounts: [],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue