You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. 'use client'
  2. import { useState } from 'react'
  3. import useSWR from 'swr'
  4. import { useTranslation } from 'react-i18next'
  5. import {
  6. RiGraduationCapFill,
  7. } from '@remixicon/react'
  8. import { useContext } from 'use-context-selector'
  9. import DeleteAccount from '../delete-account'
  10. import AvatarWithEdit from './AvatarWithEdit'
  11. import Collapse from '@/app/components/header/account-setting/collapse'
  12. import type { IItem } from '@/app/components/header/account-setting/collapse'
  13. import Modal from '@/app/components/base/modal'
  14. import Button from '@/app/components/base/button'
  15. import { updateUserProfile } from '@/service/common'
  16. import { useAppContext } from '@/context/app-context'
  17. import { useProviderContext } from '@/context/provider-context'
  18. import { ToastContext } from '@/app/components/base/toast'
  19. import AppIcon from '@/app/components/base/app-icon'
  20. import { IS_CE_EDITION } from '@/config'
  21. import Input from '@/app/components/base/input'
  22. import PremiumBadge from '@/app/components/base/premium-badge'
  23. import { useGlobalPublicStore } from '@/context/global-public-context'
  24. import EmailChangeModal from './email-change-modal'
  25. import { validPassword } from '@/config'
  26. import { fetchAppList } from '@/service/apps'
  27. import type { App } from '@/types/app'
  28. const titleClassName = `
  29. system-sm-semibold text-text-secondary
  30. `
  31. const descriptionClassName = `
  32. mt-1 body-xs-regular text-text-tertiary
  33. `
  34. export default function AccountPage() {
  35. const { t } = useTranslation()
  36. const { systemFeatures } = useGlobalPublicStore()
  37. const { data: appList } = useSWR({ url: '/apps', params: { page: 1, limit: 100, name: '' } }, fetchAppList)
  38. const apps = appList?.data || []
  39. const { mutateUserProfile, userProfile } = useAppContext()
  40. const { isEducationAccount } = useProviderContext()
  41. const { notify } = useContext(ToastContext)
  42. const [editNameModalVisible, setEditNameModalVisible] = useState(false)
  43. const [editName, setEditName] = useState('')
  44. const [editing, setEditing] = useState(false)
  45. const [editPasswordModalVisible, setEditPasswordModalVisible] = useState(false)
  46. const [currentPassword, setCurrentPassword] = useState('')
  47. const [password, setPassword] = useState('')
  48. const [confirmPassword, setConfirmPassword] = useState('')
  49. const [showDeleteAccountModal, setShowDeleteAccountModal] = useState(false)
  50. const [showCurrentPassword, setShowCurrentPassword] = useState(false)
  51. const [showPassword, setShowPassword] = useState(false)
  52. const [showConfirmPassword, setShowConfirmPassword] = useState(false)
  53. const [showUpdateEmail, setShowUpdateEmail] = useState(false)
  54. const handleEditName = () => {
  55. setEditNameModalVisible(true)
  56. setEditName(userProfile.name)
  57. }
  58. const handleSaveName = async () => {
  59. try {
  60. setEditing(true)
  61. await updateUserProfile({ url: 'account/name', body: { name: editName } })
  62. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  63. mutateUserProfile()
  64. setEditNameModalVisible(false)
  65. setEditing(false)
  66. }
  67. catch (e) {
  68. notify({ type: 'error', message: (e as Error).message })
  69. setEditNameModalVisible(false)
  70. setEditing(false)
  71. }
  72. }
  73. const showErrorMessage = (message: string) => {
  74. notify({
  75. type: 'error',
  76. message,
  77. })
  78. }
  79. const valid = () => {
  80. if (!password.trim()) {
  81. showErrorMessage(t('login.error.passwordEmpty'))
  82. return false
  83. }
  84. if (!validPassword.test(password)) {
  85. showErrorMessage(t('login.error.passwordInvalid'))
  86. return false
  87. }
  88. if (password !== confirmPassword) {
  89. showErrorMessage(t('common.account.notEqual'))
  90. return false
  91. }
  92. return true
  93. }
  94. const resetPasswordForm = () => {
  95. setCurrentPassword('')
  96. setPassword('')
  97. setConfirmPassword('')
  98. }
  99. const handleSavePassword = async () => {
  100. if (!valid())
  101. return
  102. try {
  103. setEditing(true)
  104. await updateUserProfile({
  105. url: 'account/password',
  106. body: {
  107. password: currentPassword,
  108. new_password: password,
  109. repeat_new_password: confirmPassword,
  110. },
  111. })
  112. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  113. mutateUserProfile()
  114. setEditPasswordModalVisible(false)
  115. resetPasswordForm()
  116. setEditing(false)
  117. }
  118. catch (e) {
  119. notify({ type: 'error', message: (e as Error).message })
  120. setEditPasswordModalVisible(false)
  121. setEditing(false)
  122. }
  123. }
  124. const renderAppItem = (item: IItem) => {
  125. const { icon, icon_background, icon_type, icon_url } = item as any
  126. return (
  127. <div className='flex px-3 py-1'>
  128. <div className='mr-3'>
  129. <AppIcon
  130. size='tiny'
  131. iconType={icon_type}
  132. icon={icon}
  133. background={icon_background}
  134. imageUrl={icon_url}
  135. />
  136. </div>
  137. <div className='system-sm-medium mt-[3px] text-text-secondary'>{item.name}</div>
  138. </div>
  139. )
  140. }
  141. return (
  142. <>
  143. <div className='pb-3 pt-2'>
  144. <h4 className='title-2xl-semi-bold text-text-primary'>{t('common.account.myAccount')}</h4>
  145. </div>
  146. <div className='mb-8 flex items-center rounded-xl bg-gradient-to-r from-background-gradient-bg-fill-chat-bg-2 to-background-gradient-bg-fill-chat-bg-1 p-6'>
  147. <AvatarWithEdit avatar={userProfile.avatar_url} name={userProfile.name} onSave={mutateUserProfile} size={64} />
  148. <div className='ml-4'>
  149. <p className='system-xl-semibold text-text-primary'>
  150. {userProfile.name}
  151. {isEducationAccount && (
  152. <PremiumBadge size='s' color='blue' className='ml-1 !px-2'>
  153. <RiGraduationCapFill className='mr-1 h-3 w-3' />
  154. <span className='system-2xs-medium'>EDU</span>
  155. </PremiumBadge>
  156. )}
  157. </p>
  158. <p className='system-xs-regular text-text-tertiary'>{userProfile.email}</p>
  159. </div>
  160. </div>
  161. <div className='mb-8'>
  162. <div className={titleClassName}>{t('common.account.name')}</div>
  163. <div className='mt-2 flex w-full items-center justify-between gap-2'>
  164. <div className='system-sm-regular flex-1 rounded-lg bg-components-input-bg-normal p-2 text-components-input-text-filled '>
  165. <span className='pl-1'>{userProfile.name}</span>
  166. </div>
  167. <div className='system-sm-medium cursor-pointer rounded-lg bg-components-button-tertiary-bg px-3 py-2 text-components-button-tertiary-text' onClick={handleEditName}>
  168. {t('common.operation.edit')}
  169. </div>
  170. </div>
  171. </div>
  172. <div className='mb-8'>
  173. <div className={titleClassName}>{t('common.account.email')}</div>
  174. <div className='mt-2 flex w-full items-center justify-between gap-2'>
  175. <div className='system-sm-regular flex-1 rounded-lg bg-components-input-bg-normal p-2 text-components-input-text-filled '>
  176. <span className='pl-1'>{userProfile.email}</span>
  177. </div>
  178. {systemFeatures.enable_change_email && (
  179. <div className='system-sm-medium cursor-pointer rounded-lg bg-components-button-tertiary-bg px-3 py-2 text-components-button-tertiary-text' onClick={() => setShowUpdateEmail(true)}>
  180. {t('common.operation.change')}
  181. </div>
  182. )}
  183. </div>
  184. </div>
  185. {
  186. systemFeatures.enable_email_password_login && (
  187. <div className='mb-8 flex justify-between gap-2'>
  188. <div>
  189. <div className='system-sm-semibold mb-1 text-text-secondary'>{t('common.account.password')}</div>
  190. <div className='body-xs-regular mb-2 text-text-tertiary'>{t('common.account.passwordTip')}</div>
  191. </div>
  192. <Button onClick={() => setEditPasswordModalVisible(true)}>{userProfile.is_password_set ? t('common.account.resetPassword') : t('common.account.setPassword')}</Button>
  193. </div>
  194. )
  195. }
  196. <div className='mb-6 border-[1px] border-divider-subtle' />
  197. <div className='mb-8'>
  198. <div className={titleClassName}>{t('common.account.langGeniusAccount')}</div>
  199. <div className={descriptionClassName}>{t('common.account.langGeniusAccountTip')}</div>
  200. {!!apps.length && (
  201. <Collapse
  202. title={`${t('common.account.showAppLength', { length: apps.length })}`}
  203. items={apps.map((app: App) => ({ ...app, key: app.id, name: app.name }))}
  204. renderItem={renderAppItem}
  205. wrapperClassName='mt-2'
  206. />
  207. )}
  208. {!IS_CE_EDITION && <Button className='mt-2 text-components-button-destructive-secondary-text' onClick={() => setShowDeleteAccountModal(true)}>{t('common.account.delete')}</Button>}
  209. </div>
  210. {
  211. editNameModalVisible && (
  212. <Modal
  213. isShow
  214. onClose={() => setEditNameModalVisible(false)}
  215. className='!w-[420px] !p-6'
  216. >
  217. <div className='title-2xl-semi-bold mb-6 text-text-primary'>{t('common.account.editName')}</div>
  218. <div className={titleClassName}>{t('common.account.name')}</div>
  219. <Input className='mt-2'
  220. value={editName}
  221. onChange={e => setEditName(e.target.value)}
  222. />
  223. <div className='mt-10 flex justify-end'>
  224. <Button className='mr-2' onClick={() => setEditNameModalVisible(false)}>{t('common.operation.cancel')}</Button>
  225. <Button
  226. disabled={editing || !editName}
  227. variant='primary'
  228. onClick={handleSaveName}
  229. >
  230. {t('common.operation.save')}
  231. </Button>
  232. </div>
  233. </Modal>
  234. )
  235. }
  236. {
  237. editPasswordModalVisible && (
  238. <Modal
  239. isShow
  240. onClose={() => {
  241. setEditPasswordModalVisible(false)
  242. resetPasswordForm()
  243. }}
  244. className='!w-[420px] !p-6'
  245. >
  246. <div className='title-2xl-semi-bold mb-6 text-text-primary'>{userProfile.is_password_set ? t('common.account.resetPassword') : t('common.account.setPassword')}</div>
  247. {userProfile.is_password_set && (
  248. <>
  249. <div className={titleClassName}>{t('common.account.currentPassword')}</div>
  250. <div className='relative mt-2'>
  251. <Input
  252. type={showCurrentPassword ? 'text' : 'password'}
  253. value={currentPassword}
  254. onChange={e => setCurrentPassword(e.target.value)}
  255. />
  256. <div className="absolute inset-y-0 right-0 flex items-center">
  257. <Button
  258. type="button"
  259. variant='ghost'
  260. onClick={() => setShowCurrentPassword(!showCurrentPassword)}
  261. >
  262. {showCurrentPassword ? '👀' : '😝'}
  263. </Button>
  264. </div>
  265. </div>
  266. </>
  267. )}
  268. <div className='system-sm-semibold mt-8 text-text-secondary'>
  269. {userProfile.is_password_set ? t('common.account.newPassword') : t('common.account.password')}
  270. </div>
  271. <div className='relative mt-2'>
  272. <Input
  273. type={showPassword ? 'text' : 'password'}
  274. value={password}
  275. onChange={e => setPassword(e.target.value)}
  276. />
  277. <div className="absolute inset-y-0 right-0 flex items-center">
  278. <Button
  279. type="button"
  280. variant='ghost'
  281. onClick={() => setShowPassword(!showPassword)}
  282. >
  283. {showPassword ? '👀' : '😝'}
  284. </Button>
  285. </div>
  286. </div>
  287. <div className='system-sm-semibold mt-8 text-text-secondary'>{t('common.account.confirmPassword')}</div>
  288. <div className='relative mt-2'>
  289. <Input
  290. type={showConfirmPassword ? 'text' : 'password'}
  291. value={confirmPassword}
  292. onChange={e => setConfirmPassword(e.target.value)}
  293. />
  294. <div className="absolute inset-y-0 right-0 flex items-center">
  295. <Button
  296. type="button"
  297. variant='ghost'
  298. onClick={() => setShowConfirmPassword(!showConfirmPassword)}
  299. >
  300. {showConfirmPassword ? '👀' : '😝'}
  301. </Button>
  302. </div>
  303. </div>
  304. <div className='mt-10 flex justify-end'>
  305. <Button className='mr-2' onClick={() => {
  306. setEditPasswordModalVisible(false)
  307. resetPasswordForm()
  308. }}>{t('common.operation.cancel')}</Button>
  309. <Button
  310. disabled={editing}
  311. variant='primary'
  312. onClick={handleSavePassword}
  313. >
  314. {userProfile.is_password_set ? t('common.operation.reset') : t('common.operation.save')}
  315. </Button>
  316. </div>
  317. </Modal>
  318. )
  319. }
  320. {
  321. showDeleteAccountModal && (
  322. <DeleteAccount
  323. onCancel={() => setShowDeleteAccountModal(false)}
  324. onConfirm={() => setShowDeleteAccountModal(false)}
  325. />
  326. )
  327. }
  328. {showUpdateEmail && (
  329. <EmailChangeModal
  330. show={showUpdateEmail}
  331. onClose={() => setShowUpdateEmail(false)}
  332. email={userProfile.email}
  333. />
  334. )}
  335. </>
  336. )
  337. }