refactor: simplify settings change detection in migration v1

- Introduced a new `hasSettingsChanged` function to streamline the logic for detecting changes in UI and general settings.
- Replaced the previous loop-based checks with a more concise implementation using the new function, improving code readability and maintainability.

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei 2025-05-20 19:34:07 +08:00
parent 1b7c199905
commit 8ef045de1c
No known key found for this signature in database
GPG Key ID: 0F62D33977F021F7
1 changed files with 21 additions and 17 deletions

View File

@ -5,29 +5,33 @@ import { getUISettings } from "~/atoms/settings/ui"
import { defineMigration } from "../helper"
function hasSettingsChanged(
currentSettings: Record<string, any>,
defaultSettings: Record<string, any>,
): boolean {
for (const key in defaultSettings) {
const defaultValue = defaultSettings[key]
const currentValue = currentSettings[key]
if (currentValue === undefined) {
continue
}
if (defaultValue !== currentValue) {
return true
}
}
return false
}
export const v1 = defineMigration({
version: "v1",
migrate: () => {
const settings = getGeneralSettings()
const uiSettings = getUISettings()
let enabledEnhancedSettings = false
for (const key in defaultSettings.ui) {
const defaultValue = defaultSettings.ui[key]
const currentValue = uiSettings[key]
if (defaultValue !== currentValue) {
enabledEnhancedSettings = true
break
}
}
for (const key in defaultSettings.general) {
const defaultValue = defaultSettings.general[key]
const currentValue = settings[key]
if (defaultValue !== currentValue) {
enabledEnhancedSettings = true
break
}
}
const enabledEnhancedSettings =
hasSettingsChanged(uiSettings, defaultSettings.ui) ||
hasSettingsChanged(settings, defaultSettings.general)
setGeneralSetting("enhancedSettings", enabledEnhancedSettings)
},
})