Fix(userStore): refactor setUserSetting to be async and ensure proper setting initialization#685
Conversation
… setting initialization Fixes: mineadmin#683
📝 WalkthroughWalkthrough对 Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor U as 用户
participant UI as 前端界面
participant Store as useUserStore
participant API as 后端
U->>UI: 登录
UI->>Store: requestUserInfo()
Store->>API: 获取用户信息与 backend_setting
API-->>Store: 返回 data (含 backend_setting)
Store->>Store: 规范化 raw = data?.backend_setting -> normalized
Store->>Store: await setUserSetting(normalized)
Note over Store: setUserSetting 异步执行\n在 nextTick 后执行副作用
rect rgb(235,245,235)
Store->>Store: initThemeColor() -- (先初始化主题)
Store->>Store: setLanguage(locale) -- (后设置语言,含输入校验)
end
Store->>Store: refreshMenu()
Store->>Store: refreshRole()
Store-->>UI: 初始化完成
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Assessment against linked issues
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 💡 Knowledge Base configuration:
You can enable these sources in your CodeRabbit configuration. 📒 Files selected for processing (1)
🧰 Additional context used🧬 Code graph analysis (1)web/src/store/modules/useUserStore.ts (1)
🔇 Additional comments (2)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
Status, Documentation and Community
|
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
web/src/store/modules/useUserStore.ts(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
web/src/store/modules/useUserStore.ts (1)
web/src/hooks/useThemeColor.ts (1)
useThemeColor(12-83)
🔇 Additional comments (1)
web/src/store/modules/useUserStore.ts (1)
131-132: 在 requestUserInfo 中等待 setUserSetting:顺序收敛合理,能消除竞态确保菜单/路由刷新发生在主题与语言初始化之后,有助于修复 #683 提到的首次进入时的主题/本地化错乱问题。
建议手动验证场景:
- 步骤:前端自动登出过夜 → 次日登录 → 检查各页签/新开页签的深浅色与分页文案语言是否一致,无需强刷即可生效。
…treamline initialization
There was a problem hiding this comment.
Actionable comments posted: 2
♻️ Duplicate comments (1)
web/src/store/modules/useUserStore.ts (1)
205-212: setUserSetting 异步化 + nextTick 顺序优化:LGTM按先设置配置/色阶模式 → await nextTick → 初始化主题色 → 设置语言的顺序,有助于消除首屏/切页闪烁与时序竞态。与此前建议一致,改动到位。
🧹 Nitpick comments (2)
web/src/store/modules/useUserStore.ts (2)
227-234: 登出时不建议将 language.value 置为 '';否则回退逻辑可能被“空字符串”短路即便清除了缓存,仍把
language.value写成空字符串,会与上方回退一起保留''。如果产品预期是“保留用户语言偏好”,建议保留内存值;若需重置,直接设为默认值而非空字符串。- cache.remove('language') + // 若需重置为默认语言再清缓存;否则保留用户偏好 + // cache.remove('language') ... - language.value = '' + // 保留内存中的语言;如需重置,设为默认而非空字符串 + // language.value = 'zh_CN'如确认必须清缓存,也请结合上方对
setLanguage的加固,避免写入空值。
214-221: 命名拼写:saveSettingToSever → saveSettingToServer小拼写问题,建议统一为 “Server”。若对外导出/被外部调用,请同步更新导出对象与调用点。
- function saveSettingToSever() { + function saveSettingToServer() { const backend_setting = setting.getSettings() useHttp().post('/admin/permission/update', { backend_setting }).then(() => { cache.set('sys_settings', backend_setting) }).catch((error) => { console.log(error) }) } @@ return { @@ - saveSettingToSever, + saveSettingToServer, @@ }可随后全局检索调用点以确保无遗漏:
rg -nP --type=ts '\bsaveSettingToSever\b' -C2Also applies to: 239-259
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
web/src/store/modules/useUserStore.ts(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
web/src/store/modules/useUserStore.ts (1)
web/src/hooks/useThemeColor.ts (1)
useThemeColor(12-83)
There was a problem hiding this comment.
Actionable comments posted: 1
♻️ Duplicate comments (1)
web/src/store/modules/useUserStore.ts (1)
161-166: 语言写入已加固,避免空串污染缓存:赞现在会忽略空/空白语言值并返回布尔值,符合预期。请保持该约束。
🧹 Nitpick comments (1)
web/src/store/modules/useUserStore.ts (1)
208-216: 初始化顺序与 nextTick 使用正确;微调 locale 计算与幂等设置以减少无效写入整体顺序 LGTM。为避免写入仅含空白的 locale,并减少重复写入缓存的小抖动,可小幅收敛:
@@ - useThemeColor().initThemeColor() - const locale = settings?.app?.useLocale ?? (language.value?.trim() || 'zh_CN') - setLanguage(locale) + useThemeColor().initThemeColor() + const rawLocale = typeof settings?.app?.useLocale === 'string' ? settings!.app!.useLocale : undefined + const locale = (rawLocale?.trim()) || (language.value?.trim() || 'zh_CN') + if (locale !== language.value) { + setLanguage(locale) + }
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
web/src/store/modules/useUserStore.ts(3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
web/src/store/modules/useUserStore.ts (1)
web/src/hooks/useThemeColor.ts (1)
useThemeColor(12-83)
🔇 Additional comments (1)
web/src/store/modules/useUserStore.ts (1)
208-216: 确认完毕:setUserSetting 的唯一调用(133 行)已使用 await,无遗漏
Changed the parameter type of setUserSetting function to 'any'.
|
@people-sea 大佬 部门分支能同步修复合并一下吗? |
Fixes: #683
Summary by CodeRabbit
Bug Fixes
Improvements