index.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. import Input from '@/app/components/base/input'
  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 validPassword = /^(?=.*[a-zA-Z])(?=.*\d).{8,}$/
  25. export default function AccountPage() {
  26. const { t } = useTranslation()
  27. const { systemFeatures } = useAppContext()
  28. const { mutateUserProfile, userProfile, apps } = useAppContext()
  29. const { notify } = useContext(ToastContext)
  30. const [editNameModalVisible, setEditNameModalVisible] = useState(false)
  31. const [editName, setEditName] = useState('')
  32. const [editing, setEditing] = useState(false)
  33. const [editPasswordModalVisible, setEditPasswordModalVisible] = useState(false)
  34. const [currentPassword, setCurrentPassword] = useState('')
  35. const [password, setPassword] = useState('')
  36. const [confirmPassword, setConfirmPassword] = useState('')
  37. const [showDeleteAccountModal, setShowDeleteAccountModal] = useState(false)
  38. const [showCurrentPassword, setShowCurrentPassword] = useState(false)
  39. const [showPassword, setShowPassword] = useState(false)
  40. const [showConfirmPassword, setShowConfirmPassword] = 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 handleSavePassword = 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='pt-2 pb-3'>
  124. <h4 className='title-2xl-semi-bold text-primary'>{t('common.account.myAccount')}</h4>
  125. </div>
  126. <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'>
  127. <Avatar name={userProfile.name} size={64} />
  128. <div className='ml-4'>
  129. <p className='system-xl-semibold text-text-primary'>{userProfile.name}</p>
  130. <p className='system-xs-regular text-text-tertiary'>{userProfile.email}</p>
  131. </div>
  132. </div>
  133. <div className='mb-8'>
  134. <div className={titleClassName}>{t('common.account.name')}</div>
  135. <div className='flex items-center justify-between gap-2 w-full mt-2'>
  136. <div className='flex-1 bg-gray-100 rounded-md p-2 system-sm-regular text-components-input-text-filled '>
  137. <span className='pl-1'>{userProfile.name}</span>
  138. </div>
  139. <div className=' bg-gray-100 rounded-md py-2 px-3 cursor-pointer system-sm-medium text-components-input-text-filled' onClick={handleEditName}>
  140. {t('common.operation.edit')}
  141. </div>
  142. </div>
  143. </div>
  144. <div className='mb-8'>
  145. <div className={titleClassName}>{t('common.account.email')}</div>
  146. <div className='flex items-center justify-between gap-2 w-full mt-2'>
  147. <div className='flex-1 bg-gray-100 rounded-md p-2 system-sm-regular text-components-input-text-filled '>
  148. <span className='pl-1'>{userProfile.email}</span>
  149. </div>
  150. </div>
  151. </div>
  152. {
  153. systemFeatures.enable_email_password_login && (
  154. <div className='mb-8 flex justify-between gap-2'>
  155. <div>
  156. <div className='mb-1 text-sm font-medium text-gray-900'>{t('common.account.password')}</div>
  157. <div className='mb-2 text-xs text-gray-500'>{t('common.account.passwordTip')}</div>
  158. </div>
  159. <Button onClick={() => setEditPasswordModalVisible(true)}>{userProfile.is_password_set ? t('common.account.resetPassword') : t('common.account.setPassword')}</Button>
  160. </div>
  161. )
  162. }
  163. <div className='mb-6 border-[0.5px] border-gray-100' />
  164. <div className='mb-8'>
  165. <div className={titleClassName}>{t('common.account.langGeniusAccount')}</div>
  166. <div className={descriptionClassName}>{t('common.account.langGeniusAccountTip')}</div>
  167. {!!apps.length && (
  168. <Collapse
  169. title={`${t('common.account.showAppLength', { length: apps.length })}`}
  170. items={apps.map(app => ({ key: app.id, name: app.name }))}
  171. renderItem={renderAppItem}
  172. wrapperClassName='mt-2'
  173. />
  174. )}
  175. {!IS_CE_EDITION && <Button className='mt-2 text-[#D92D20]' onClick={() => setShowDeleteAccountModal(true)}>{t('common.account.delete')}</Button>}
  176. </div>
  177. {
  178. editNameModalVisible && (
  179. <Modal
  180. isShow
  181. onClose={() => setEditNameModalVisible(false)}
  182. className={s.modal}
  183. >
  184. <div className='mb-6 text-lg font-medium text-gray-900'>{t('common.account.editName')}</div>
  185. <div className={titleClassName}>{t('common.account.name')}</div>
  186. <Input className='mt-2'
  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. <div className='relative mt-2'>
  218. <Input
  219. type={showCurrentPassword ? 'text' : 'password'}
  220. value={currentPassword}
  221. onChange={e => setCurrentPassword(e.target.value)}
  222. />
  223. <div className="absolute inset-y-0 right-0 flex items-center">
  224. <Button
  225. type="button"
  226. variant='ghost'
  227. onClick={() => setShowCurrentPassword(!showCurrentPassword)}
  228. >
  229. {showCurrentPassword ? '👀' : '😝'}
  230. </Button>
  231. </div>
  232. </div>
  233. </>
  234. )}
  235. <div className='mt-8 text-sm font-medium text-gray-900'>
  236. {userProfile.is_password_set ? t('common.account.newPassword') : t('common.account.password')}
  237. </div>
  238. <div className='relative mt-2'>
  239. <Input
  240. type={showPassword ? 'text' : 'password'}
  241. value={password}
  242. onChange={e => setPassword(e.target.value)}
  243. />
  244. <div className="absolute inset-y-0 right-0 flex items-center">
  245. <Button
  246. type="button"
  247. variant='ghost'
  248. onClick={() => setShowPassword(!showPassword)}
  249. >
  250. {showPassword ? '👀' : '😝'}
  251. </Button>
  252. </div>
  253. </div>
  254. <div className='mt-8 text-sm font-medium text-gray-900'>{t('common.account.confirmPassword')}</div>
  255. <div className='relative mt-2'>
  256. <Input
  257. type={showConfirmPassword ? 'text' : 'password'}
  258. value={confirmPassword}
  259. onChange={e => setConfirmPassword(e.target.value)}
  260. />
  261. <div className="absolute inset-y-0 right-0 flex items-center">
  262. <Button
  263. type="button"
  264. variant='ghost'
  265. onClick={() => setShowConfirmPassword(!showConfirmPassword)}
  266. >
  267. {showConfirmPassword ? '👀' : '😝'}
  268. </Button>
  269. </div>
  270. </div>
  271. <div className='flex justify-end mt-10'>
  272. <Button className='mr-2' onClick={() => {
  273. setEditPasswordModalVisible(false)
  274. resetPasswordForm()
  275. }}>{t('common.operation.cancel')}</Button>
  276. <Button
  277. disabled={editing}
  278. variant='primary'
  279. onClick={handleSavePassword}
  280. >
  281. {userProfile.is_password_set ? t('common.operation.reset') : t('common.operation.save')}
  282. </Button>
  283. </div>
  284. </Modal>
  285. )
  286. }
  287. {
  288. showDeleteAccountModal && (
  289. <Confirm
  290. isShow
  291. onCancel={() => setShowDeleteAccountModal(false)}
  292. onConfirm={() => setShowDeleteAccountModal(false)}
  293. showCancel={false}
  294. type='warning'
  295. title={t('common.account.delete')}
  296. content={
  297. <>
  298. <div className='my-1 text-[#D92D20] text-sm leading-5'>
  299. {t('common.account.deleteTip')}
  300. </div>
  301. <div className='mt-3 text-sm leading-5'>
  302. <span>{t('common.account.deleteConfirmTip')}</span>
  303. <a
  304. className='text-primary-600 cursor'
  305. href={`mailto:support@dify.ai?subject=Delete Account Request&body=Delete Account: ${userProfile.email}`}
  306. target='_blank'
  307. rel='noreferrer noopener'
  308. onClick={(e) => {
  309. e.preventDefault()
  310. window.location.href = e.currentTarget.href
  311. }}
  312. >
  313. support@dify.ai
  314. </a>
  315. </div>
  316. <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>
  317. </>
  318. }
  319. confirmText={t('common.operation.ok') as string}
  320. />
  321. )
  322. }
  323. </>
  324. )
  325. }