index.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use client'
  2. import { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useContext } from 'use-context-selector'
  5. import { useAppContext } from '@/context/app-context'
  6. import { SimpleSelect } from '@/app/components/base/select'
  7. import type { Item } from '@/app/components/base/select'
  8. import { updateUserProfile } from '@/service/common'
  9. import { ToastContext } from '@/app/components/base/toast'
  10. import I18n from '@/context/i18n'
  11. import { timezones } from '@/utils/timezone'
  12. import { languages } from '@/i18n-config/language'
  13. const titleClassName = `
  14. mb-2 system-sm-semibold text-text-secondary
  15. `
  16. export default function LanguagePage() {
  17. const { locale, setLocaleOnClient } = useContext(I18n)
  18. const { userProfile, mutateUserProfile } = useAppContext()
  19. const { notify } = useContext(ToastContext)
  20. const [editing, setEditing] = useState(false)
  21. const { t } = useTranslation()
  22. // 组件加载时打印当前配置
  23. console.log('═'.repeat(80))
  24. console.log('[TIMEZONE] 📋 语言设置页面已加载')
  25. console.log('[TIMEZONE] 📋 当前用户时区:', userProfile.timezone)
  26. console.log('[TIMEZONE] 📋 当前用户语言:', userProfile.interface_language)
  27. console.log('[TIMEZONE] 📋 是否正在编辑:', editing)
  28. console.log('[TIMEZONE] 💡 提示:如果要调试,请在控制台右键 → 勾选 "Preserve log(保留日志)"')
  29. console.log('═'.repeat(80))
  30. const handleSelectLanguage = async (item: Item) => {
  31. const url = '/account/interface-language'
  32. const bodyKey = 'interface_language'
  33. setEditing(true)
  34. try {
  35. await updateUserProfile({ url, body: { [bodyKey]: item.value } })
  36. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  37. setLocaleOnClient(item.value.toString())
  38. }
  39. catch (e) {
  40. notify({ type: 'error', message: (e as Error).message })
  41. }
  42. finally {
  43. setEditing(false)
  44. }
  45. }
  46. const handleSelectTimezone = async (item: Item) => {
  47. const url = '/account/timezone'
  48. const bodyKey = 'timezone'
  49. console.log('[TIMEZONE] 🚀 开始修改时区流程...')
  50. console.log('[TIMEZONE] 📝 当前时区:', userProfile.timezone)
  51. console.log('[TIMEZONE] 📝 目标时区:', item.value)
  52. console.log('[TIMEZONE] 📝 API 路径:', url)
  53. console.log('[TIMEZONE] 📝 请求体:', { [bodyKey]: item.value })
  54. setEditing(true)
  55. try {
  56. console.log('[TIMEZONE] ⏳ 正在调用 updateUserProfile API...')
  57. const startTime = Date.now()
  58. const response = await updateUserProfile({ url, body: { [bodyKey]: item.value } })
  59. const endTime = Date.now()
  60. console.log('[TIMEZONE] ✅ API 调用成功! 耗时:', (endTime - startTime), 'ms')
  61. console.log('[TIMEZONE] 📦 API 响应:', response)
  62. // 成功通知,并提示需要手动刷新(调试模式)
  63. notify({
  64. type: 'success',
  65. message: `${t('common.actionMsg.modifiedSuccessfully')} - 请查看控制台日志,然后手动刷新页面(F5)`
  66. })
  67. console.log('[TIMEZONE] 🔄 正在刷新用户信息...')
  68. await mutateUserProfile()
  69. console.log('[TIMEZONE] ✅ 用户信息已刷新')
  70. console.log('[TIMEZONE] 📝 刷新后的时区 (可能还是旧值,因为组件未重渲染):', userProfile.timezone)
  71. // 🚨 调试模式:暂时禁用自动刷新,方便查看日志
  72. console.log('[TIMEZONE] ⚠️ 自动刷新已禁用(调试模式)')
  73. console.log('[TIMEZONE] 💡 请手动检查以下内容:')
  74. console.log('[TIMEZONE] 1️⃣ 上面的 API 响应中,timezone 是否已更新?')
  75. console.log('[TIMEZONE] 2️⃣ Network 标签中,/account/timezone 请求是否成功?')
  76. console.log('[TIMEZONE] 3️⃣ 准备好后,手动刷新页面(F5)查看时区是否生效')
  77. console.log('[TIMEZONE] 🏁 调试完成!')
  78. // Reload page to ensure all components use new timezone
  79. // 🚨 调试期间暂时注释掉自动刷新
  80. // setTimeout(() => {
  81. // console.log('[TIMEZONE] 🔄 正在重新加载页面...')
  82. // window.location.reload()
  83. // }, 500)
  84. }
  85. catch (e) {
  86. console.error('[TIMEZONE] ❌ 保存失败!')
  87. console.error('[TIMEZONE] 错误详情:', e)
  88. console.error('[TIMEZONE] 错误类型:', typeof e)
  89. console.error('[TIMEZONE] 错误内容:', JSON.stringify(e, null, 2))
  90. notify({ type: 'error', message: (e as Error).message })
  91. }
  92. finally {
  93. setEditing(false)
  94. console.log('[TIMEZONE] 🏁 handleSelectTimezone 函数执行完毕')
  95. }
  96. }
  97. return (
  98. <>
  99. <div className='mb-8'>
  100. <div className={titleClassName}>{t('common.language.displayLanguage')}</div>
  101. <SimpleSelect
  102. defaultValue={locale || userProfile.interface_language}
  103. items={languages.filter(item => item.supported)}
  104. onSelect={item => handleSelectLanguage(item)}
  105. disabled={editing}
  106. notClearable={true}
  107. />
  108. </div>
  109. <div className='mb-8'>
  110. <div className={titleClassName}>{t('common.language.timezone')}</div>
  111. <SimpleSelect
  112. defaultValue={userProfile.timezone}
  113. items={timezones}
  114. onSelect={(item) => {
  115. console.log('[TIMEZONE] 🎯 SimpleSelect onSelect 被触发!')
  116. console.log('[TIMEZONE] 🎯 选中的时区:', item)
  117. handleSelectTimezone(item)
  118. }}
  119. disabled={editing}
  120. notClearable={true}
  121. />
  122. </div>
  123. </>
  124. )
  125. }