Skip to content

Commit defaecb

Browse files
committed
fix: 使用 localStorage 缓存设置状态,避免视觉闪烁
- 从 localStorage 读取缓存值作为初始值 - 加载和保存设置后更新缓存 - 实现"记住状态"效果,打开设置时直接显示正确状态 - 版本更新至 v0.2.4
1 parent a0c1bdb commit defaecb

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

frontend/src/components/General/Index.vue

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ import ThemeSetting from '../Setting/ThemeSetting.vue'
77
import { fetchAppSettings, saveAppSettings, type AppSettings } from '../../services/appSettings'
88
99
const router = useRouter()
10-
const heatmapEnabled = ref(true)
11-
const homeTitleVisible = ref(true)
12-
const autoStartEnabled = ref(true)
10+
// 从 localStorage 读取缓存值作为初始值,避免加载时的视觉闪烁
11+
const getCachedValue = (key: string, defaultValue: boolean): boolean => {
12+
const cached = localStorage.getItem(`app-settings-${key}`)
13+
return cached !== null ? cached === 'true' : defaultValue
14+
}
15+
const heatmapEnabled = ref(getCachedValue('heatmap', true))
16+
const homeTitleVisible = ref(getCachedValue('homeTitle', true))
17+
const autoStartEnabled = ref(getCachedValue('autoStart', false))
1318
const settingsLoading = ref(true)
1419
const saveBusy = ref(false)
1520
@@ -23,12 +28,17 @@ const loadAppSettings = async () => {
2328
const data = await fetchAppSettings()
2429
heatmapEnabled.value = data?.show_heatmap ?? true
2530
homeTitleVisible.value = data?.show_home_title ?? true
26-
autoStartEnabled.value = data?.auto_start ?? true
31+
autoStartEnabled.value = data?.auto_start ?? false
32+
33+
// 缓存到 localStorage,下次打开时直接显示正确状态
34+
localStorage.setItem('app-settings-heatmap', String(heatmapEnabled.value))
35+
localStorage.setItem('app-settings-homeTitle', String(homeTitleVisible.value))
36+
localStorage.setItem('app-settings-autoStart', String(autoStartEnabled.value))
2737
} catch (error) {
2838
console.error('failed to load app settings', error)
2939
heatmapEnabled.value = true
3040
homeTitleVisible.value = true
31-
autoStartEnabled.value = true
41+
autoStartEnabled.value = false
3242
} finally {
3343
settingsLoading.value = false
3444
}
@@ -44,6 +54,12 @@ const persistAppSettings = async () => {
4454
auto_start: autoStartEnabled.value,
4555
}
4656
await saveAppSettings(payload)
57+
58+
// 更新缓存
59+
localStorage.setItem('app-settings-heatmap', String(heatmapEnabled.value))
60+
localStorage.setItem('app-settings-homeTitle', String(homeTitleVisible.value))
61+
localStorage.setItem('app-settings-autoStart', String(autoStartEnabled.value))
62+
4763
window.dispatchEvent(new CustomEvent('app-settings-updated'))
4864
} catch (error) {
4965
console.error('failed to save app settings', error)

0 commit comments

Comments
 (0)