index.tsx 13 KB

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