index.tsx 11 KB

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