'use client' import type { IItem } from '@/app/components/header/account-setting/collapse' import type { App } from '@/types/app' import { RiGraduationCapFill, } from '@remixicon/react' import { useState } from 'react' import { useTranslation } from 'react-i18next' import { useContext } from 'use-context-selector' import AppIcon from '@/app/components/base/app-icon' import Button from '@/app/components/base/button' import Input from '@/app/components/base/input' import Modal from '@/app/components/base/modal' import PremiumBadge from '@/app/components/base/premium-badge' import { ToastContext } from '@/app/components/base/toast' import Collapse from '@/app/components/header/account-setting/collapse' import { IS_CE_EDITION, validPassword } from '@/config' import { useAppContext } from '@/context/app-context' import { useGlobalPublicStore } from '@/context/global-public-context' import { useProviderContext } from '@/context/provider-context' import { updateUserProfile } from '@/service/common' import { useAppList } from '@/service/use-apps' import DeleteAccount from '../delete-account' import AvatarWithEdit from './AvatarWithEdit' import EmailChangeModal from './email-change-modal' const titleClassName = ` system-sm-semibold text-text-secondary ` const descriptionClassName = ` mt-1 body-xs-regular text-text-tertiary ` export default function AccountPage() { const { t } = useTranslation() const { systemFeatures } = useGlobalPublicStore() const { data: appList } = useAppList({ page: 1, limit: 100, name: '' }) const apps = appList?.data || [] const { mutateUserProfile, userProfile } = useAppContext() const { isEducationAccount } = useProviderContext() const { notify } = useContext(ToastContext) const [editNameModalVisible, setEditNameModalVisible] = useState(false) const [editName, setEditName] = useState('') const [editing, setEditing] = useState(false) const [editPasswordModalVisible, setEditPasswordModalVisible] = useState(false) const [currentPassword, setCurrentPassword] = useState('') const [password, setPassword] = useState('') const [confirmPassword, setConfirmPassword] = useState('') const [showDeleteAccountModal, setShowDeleteAccountModal] = useState(false) const [showCurrentPassword, setShowCurrentPassword] = useState(false) const [showPassword, setShowPassword] = useState(false) const [showConfirmPassword, setShowConfirmPassword] = useState(false) const [showUpdateEmail, setShowUpdateEmail] = useState(false) const handleEditName = () => { setEditNameModalVisible(true) setEditName(userProfile.name) } const handleSaveName = async () => { try { setEditing(true) await updateUserProfile({ url: 'account/name', body: { name: editName } }) notify({ type: 'success', message: t('actionMsg.modifiedSuccessfully', { ns: 'common' }) }) mutateUserProfile() setEditNameModalVisible(false) setEditing(false) } catch (e) { notify({ type: 'error', message: (e as Error).message }) setEditing(false) } } const showErrorMessage = (message: string) => { notify({ type: 'error', message, }) } const valid = () => { if (!password.trim()) { showErrorMessage(t('error.passwordEmpty', { ns: 'login' })) return false } if (!validPassword.test(password)) { showErrorMessage(t('error.passwordInvalid', { ns: 'login' })) return false } if (password !== confirmPassword) { showErrorMessage(t('account.notEqual', { ns: 'common' })) return false } return true } const resetPasswordForm = () => { setCurrentPassword('') setPassword('') setConfirmPassword('') } const handleSavePassword = async () => { if (!valid()) return try { setEditing(true) await updateUserProfile({ url: 'account/password', body: { password: currentPassword, new_password: password, repeat_new_password: confirmPassword, }, }) notify({ type: 'success', message: t('actionMsg.modifiedSuccessfully', { ns: 'common' }) }) mutateUserProfile() setEditPasswordModalVisible(false) resetPasswordForm() setEditing(false) } catch (e) { notify({ type: 'error', message: (e as Error).message }) setEditPasswordModalVisible(false) setEditing(false) } } const renderAppItem = (item: IItem) => { const { icon, icon_background, icon_type, icon_url } = item as any return (
{item.name}
) } return ( <>

{t('account.myAccount', { ns: 'common' })}

{userProfile.name} {isEducationAccount && ( EDU )}

{userProfile.email}

{t('account.name', { ns: 'common' })}
{userProfile.name}
{t('operation.edit', { ns: 'common' })}
{t('account.email', { ns: 'common' })}
{userProfile.email}
{systemFeatures.enable_change_email && (
setShowUpdateEmail(true)}> {t('operation.change', { ns: 'common' })}
)}
{ systemFeatures.enable_email_password_login && (
{t('account.password', { ns: 'common' })}
{t('account.passwordTip', { ns: 'common' })}
) }
{t('account.langGeniusAccount', { ns: 'common' })}
{t('account.langGeniusAccountTip', { ns: 'common' })}
{!!apps.length && ( ({ ...app, key: app.id, name: app.name }))} renderItem={renderAppItem} wrapperClassName="mt-2" /> )} {!IS_CE_EDITION && }
{ editNameModalVisible && ( setEditNameModalVisible(false)} className="!w-[420px] !p-6" >
{t('account.editName', { ns: 'common' })}
{t('account.name', { ns: 'common' })}
setEditName(e.target.value)} />
) } { editPasswordModalVisible && ( { setEditPasswordModalVisible(false) resetPasswordForm() }} className="!w-[420px] !p-6" >
{userProfile.is_password_set ? t('account.resetPassword', { ns: 'common' }) : t('account.setPassword', { ns: 'common' })}
{userProfile.is_password_set && ( <>
{t('account.currentPassword', { ns: 'common' })}
setCurrentPassword(e.target.value)} />
)}
{userProfile.is_password_set ? t('account.newPassword', { ns: 'common' }) : t('account.password', { ns: 'common' })}
setPassword(e.target.value)} />
{t('account.confirmPassword', { ns: 'common' })}
setConfirmPassword(e.target.value)} />
) } { showDeleteAccountModal && ( setShowDeleteAccountModal(false)} onConfirm={() => setShowDeleteAccountModal(false)} /> ) } {showUpdateEmail && ( setShowUpdateEmail(false)} email={userProfile.email} /> )} ) }