index.tsx 13 KB

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