index.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. 'use client'
  2. import { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import classNames from 'classnames'
  5. import { useContext } from 'use-context-selector'
  6. import Collapse from '../collapse'
  7. import type { IItem } from '../collapse'
  8. import s from './index.module.css'
  9. import Modal from '@/app/components/base/modal'
  10. import Button from '@/app/components/base/button'
  11. import { updateUserProfile } from '@/service/common'
  12. import { useAppContext } from '@/context/app-context'
  13. import { ToastContext } from '@/app/components/base/toast'
  14. import AppIcon from '@/app/components/base/app-icon'
  15. import Avatar from '@/app/components/base/avatar'
  16. const titleClassName = `
  17. text-sm font-medium text-gray-900
  18. `
  19. const descriptionClassName = `
  20. mt-1 text-xs font-normal text-gray-500
  21. `
  22. const inputClassName = `
  23. mt-2 w-full px-3 py-2 bg-gray-100 rounded
  24. text-sm font-normal text-gray-800
  25. `
  26. const validPassword = /^(?=.*[a-zA-Z])(?=.*\d).{8,}$/
  27. export default function AccountPage() {
  28. const { t } = useTranslation()
  29. const { mutateUserProfile, userProfile, apps } = useAppContext()
  30. const { notify } = useContext(ToastContext)
  31. const [editNameModalVisible, setEditNameModalVisible] = useState(false)
  32. const [editName, setEditName] = useState('')
  33. const [editing, setEditing] = useState(false)
  34. const [editPasswordModalVisible, setEditPasswordModalVisible] = useState(false)
  35. const [currentPassword, setCurrentPassword] = useState('')
  36. const [password, setPassword] = useState('')
  37. const [confirmPassword, setConfirmPassword] = useState('')
  38. const handleEditName = () => {
  39. setEditNameModalVisible(true)
  40. setEditName(userProfile.name)
  41. }
  42. const handleSaveName = async () => {
  43. try {
  44. setEditing(true)
  45. await updateUserProfile({ url: 'account/name', body: { name: editName } })
  46. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  47. mutateUserProfile()
  48. setEditNameModalVisible(false)
  49. setEditing(false)
  50. }
  51. catch (e) {
  52. notify({ type: 'error', message: (e as Error).message })
  53. setEditNameModalVisible(false)
  54. setEditing(false)
  55. }
  56. }
  57. const showErrorMessage = (message: string) => {
  58. notify({
  59. type: 'error',
  60. message,
  61. })
  62. }
  63. const valid = () => {
  64. if (!password.trim()) {
  65. showErrorMessage(t('login.error.passwordEmpty'))
  66. return false
  67. }
  68. if (!validPassword.test(password))
  69. showErrorMessage(t('login.error.passwordInvalid'))
  70. if (password !== confirmPassword)
  71. showErrorMessage(t('common.account.notEqual'))
  72. return true
  73. }
  74. const resetPasswordForm = () => {
  75. setCurrentPassword('')
  76. setPassword('')
  77. setConfirmPassword('')
  78. }
  79. const handleSavePassowrd = async () => {
  80. if (!valid())
  81. return
  82. try {
  83. setEditing(true)
  84. await updateUserProfile({
  85. url: 'account/password',
  86. body: {
  87. password: currentPassword,
  88. new_password: password,
  89. repeat_new_password: confirmPassword,
  90. },
  91. })
  92. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  93. mutateUserProfile()
  94. setEditPasswordModalVisible(false)
  95. resetPasswordForm()
  96. setEditing(false)
  97. }
  98. catch (e) {
  99. notify({ type: 'error', message: (e as Error).message })
  100. setEditPasswordModalVisible(false)
  101. setEditing(false)
  102. }
  103. }
  104. const renderAppItem = (item: IItem) => {
  105. return (
  106. <div className='flex px-3 py-1'>
  107. <div className='mr-3'>
  108. <AppIcon size='tiny' />
  109. </div>
  110. <div className='mt-[3px] text-xs font-medium text-gray-700 leading-[18px]'>{item.name}</div>
  111. </div>
  112. )
  113. }
  114. return (
  115. <>
  116. <div className='mb-8'>
  117. <div className={titleClassName}>{t('common.account.avatar')}</div>
  118. <Avatar name={userProfile.name} size={64} className='mt-2' />
  119. </div>
  120. <div className='mb-8'>
  121. <div className={titleClassName}>{t('common.account.name')}</div>
  122. <div className={classNames('flex items-center justify-between mt-2 w-full h-9 px-3 bg-gray-100 rounded text-sm font-normal text-gray-800 cursor-pointer group')}>
  123. {userProfile.name}
  124. <div className='items-center hidden h-6 px-2 text-xs font-normal bg-white border border-gray-200 rounded-md group-hover:flex' onClick={handleEditName}>{t('common.operation.edit')}</div>
  125. </div>
  126. </div>
  127. <div className='mb-8'>
  128. <div className={titleClassName}>{t('common.account.email')}</div>
  129. <div className={classNames(inputClassName, 'cursor-pointer')}>{userProfile.email}</div>
  130. </div>
  131. <div className='mb-8'>
  132. <div className='mb-1 text-sm font-medium text-gray-900'>{t('common.account.password')}</div>
  133. <div className='mb-2 text-xs text-gray-500'>{t('common.account.passwordTip')}</div>
  134. <Button className='font-medium !text-gray-700 !px-3 !py-[7px] !text-[13px]' onClick={() => setEditPasswordModalVisible(true)}>{userProfile.is_password_set ? t('common.account.resetPassword') : t('common.account.setPassword')}</Button>
  135. </div>
  136. {!!apps.length && (
  137. <>
  138. <div className='mb-6 border-[0.5px] border-gray-100' />
  139. <div className='mb-8'>
  140. <div className={titleClassName}>{t('common.account.langGeniusAccount')}</div>
  141. <div className={descriptionClassName}>{t('common.account.langGeniusAccountTip')}</div>
  142. <Collapse
  143. title={`${t('common.account.showAppLength', { length: apps.length })}`}
  144. items={apps.map(app => ({ key: app.id, name: app.name }))}
  145. renderItem={renderAppItem}
  146. wrapperClassName='mt-2'
  147. />
  148. </div>
  149. </>
  150. )}
  151. {editNameModalVisible && (
  152. <Modal
  153. isShow
  154. onClose={() => setEditNameModalVisible(false)}
  155. className={s.modal}
  156. >
  157. <div className='mb-6 text-lg font-medium text-gray-900'>{t('common.account.editName')}</div>
  158. <div className={titleClassName}>{t('common.account.name')}</div>
  159. <input
  160. className={inputClassName}
  161. value={editName}
  162. onChange={e => setEditName(e.target.value)}
  163. />
  164. <div className='flex justify-end mt-10'>
  165. <Button className='mr-2 text-sm font-medium' onClick={() => setEditNameModalVisible(false)}>{t('common.operation.cancel')}</Button>
  166. <Button
  167. disabled={editing || !editName}
  168. type='primary'
  169. className='text-sm font-medium'
  170. onClick={handleSaveName}
  171. >
  172. {t('common.operation.save')}
  173. </Button>
  174. </div>
  175. </Modal>
  176. )}
  177. {editPasswordModalVisible && (
  178. <Modal
  179. isShow
  180. onClose={() => {
  181. setEditPasswordModalVisible(false)
  182. resetPasswordForm()
  183. }}
  184. className={s.modal}
  185. >
  186. <div className='mb-6 text-lg font-medium text-gray-900'>{userProfile.is_password_set ? t('common.account.resetPassword') : t('common.account.setPassword')}</div>
  187. {userProfile.is_password_set && (
  188. <>
  189. <div className={titleClassName}>{t('common.account.currentPassword')}</div>
  190. <input
  191. type="password"
  192. className={inputClassName}
  193. value={currentPassword}
  194. onChange={e => setCurrentPassword(e.target.value)}
  195. />
  196. </>
  197. )}
  198. <div className='mt-8 text-sm font-medium text-gray-900'>
  199. {userProfile.is_password_set ? t('common.account.newPassword') : t('common.account.password')}
  200. </div>
  201. <input
  202. type="password"
  203. className={inputClassName}
  204. value={password}
  205. onChange={e => setPassword(e.target.value)}
  206. />
  207. <div className='mt-8 text-sm font-medium text-gray-900'>{t('common.account.confirmPassword')}</div>
  208. <input
  209. type="password"
  210. className={inputClassName}
  211. value={confirmPassword}
  212. onChange={e => setConfirmPassword(e.target.value)}
  213. />
  214. <div className='flex justify-end mt-10'>
  215. <Button className='mr-2 text-sm font-medium' onClick={() => {
  216. setEditPasswordModalVisible(false)
  217. resetPasswordForm()
  218. }}>{t('common.operation.cancel')}</Button>
  219. <Button
  220. disabled={editing}
  221. type='primary'
  222. className='text-sm font-medium'
  223. onClick={handleSavePassowrd}
  224. >
  225. {userProfile.is_password_set ? t('common.operation.reset') : t('common.operation.save')}
  226. </Button>
  227. </div>
  228. </Modal>
  229. )}
  230. </>
  231. )
  232. }