index.tsx 10 KB

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