index.tsx 10 KB

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