index.tsx 10 KB

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