index.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 '@/utils/language'
  13. const titleClassName = `
  14. mb-2 text-sm font-medium text-gray-900
  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. const handleSelect = async (type: string, item: Item) => {
  23. let url = ''
  24. let bodyKey = ''
  25. if (type === 'language') {
  26. url = '/account/interface-language'
  27. bodyKey = 'interface_language'
  28. setLocaleOnClient(item.value.toString())
  29. }
  30. if (type === 'timezone') {
  31. url = '/account/timezone'
  32. bodyKey = 'timezone'
  33. }
  34. try {
  35. setEditing(true)
  36. await updateUserProfile({ url, body: { [bodyKey]: item.value } })
  37. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  38. mutateUserProfile()
  39. setEditing(false)
  40. }
  41. catch (e) {
  42. notify({ type: 'error', message: (e as Error).message })
  43. setEditing(false)
  44. }
  45. }
  46. return (
  47. <>
  48. <div className='mb-8'>
  49. <div className={titleClassName}>{t('common.language.displayLanguage')}</div>
  50. <SimpleSelect
  51. defaultValue={locale || userProfile.interface_language}
  52. items={languages}
  53. onSelect={item => handleSelect('language', item)}
  54. disabled={editing}
  55. />
  56. </div>
  57. <div className='mb-8'>
  58. <div className={titleClassName}>{t('common.language.timezone')}</div>
  59. <SimpleSelect
  60. defaultValue={userProfile.timezone}
  61. items={timezones}
  62. onSelect={item => handleSelect('timezone', item)}
  63. disabled={editing}
  64. />
  65. </div>
  66. </>
  67. )
  68. }