import React, { useEffect, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { API, isMobile, showError, showSuccess, renderQuota, renderQuotaWithPrompt } from '../../helpers'; import { Button, Input, Modal, Select, SideSheet, Space, Spin, Typography, Card, Tag, } from '@douyinfe/semi-ui'; import { IconUser, IconSave, IconClose, IconKey, IconCreditCard, IconLink, IconUserGroup, IconPlus, } from '@douyinfe/semi-icons'; import { useTranslation } from 'react-i18next'; const { Text, Title } = Typography; const EditUser = (props) => { const userId = props.editingUser.id; const [loading, setLoading] = useState(true); const [addQuotaModalOpen, setIsModalOpen] = useState(false); const [addQuotaLocal, setAddQuotaLocal] = useState(''); const [inputs, setInputs] = useState({ username: '', display_name: '', password: '', github_id: '', oidc_id: '', wechat_id: '', email: '', quota: 0, group: 'default', }); const [groupOptions, setGroupOptions] = useState([]); const { username, display_name, password, github_id, oidc_id, wechat_id, telegram_id, email, quota, group, } = inputs; const handleInputChange = (name, value) => { setInputs((inputs) => ({ ...inputs, [name]: value })); }; const fetchGroups = async () => { try { let res = await API.get(`/api/group/`); setGroupOptions( res.data.data.map((group) => ({ label: group, value: group, })), ); } catch (error) { showError(error.message); } }; const navigate = useNavigate(); const handleCancel = () => { props.handleClose(); }; const loadUser = async () => { setLoading(true); let res = undefined; if (userId) { res = await API.get(`/api/user/${userId}`); } else { res = await API.get(`/api/user/self`); } const { success, message, data } = res.data; if (success) { data.password = ''; setInputs(data); } else { showError(message); } setLoading(false); }; useEffect(() => { loadUser().then(); if (userId) { fetchGroups().then(); } }, [props.editingUser.id]); const submit = async () => { setLoading(true); let res = undefined; if (userId) { let data = { ...inputs, id: parseInt(userId) }; if (typeof data.quota === 'string') { data.quota = parseInt(data.quota); } res = await API.put(`/api/user/`, data); } else { res = await API.put(`/api/user/self`, inputs); } const { success, message } = res.data; if (success) { showSuccess('用户信息更新成功!'); props.refresh(); props.handleClose(); } else { showError(message); } setLoading(false); }; const addLocalQuota = () => { let newQuota = parseInt(quota) + parseInt(addQuotaLocal); setInputs((inputs) => ({ ...inputs, quota: newQuota })); }; const openAddQuotaModal = () => { setAddQuotaLocal('0'); setIsModalOpen(true); }; const { t } = useTranslation(); return ( <> {t('编辑')} {t('编辑用户')} } headerStyle={{ borderBottom: '1px solid var(--semi-color-border)', padding: '24px' }} bodyStyle={{ backgroundColor: 'var(--semi-color-bg-0)', padding: '0' }} visible={props.visible} width={isMobile() ? '100%' : 600} footer={
} closeIcon={null} onCancel={() => handleCancel()} >
{t('基本信息')}
{t('用户的基本账户信息')}
{t('用户名')} handleInputChange('username', value)} value={username} autoComplete="new-password" size="large" className="!rounded-lg" showClear />
{t('密码')} handleInputChange('password', value)} value={password} autoComplete="new-password" size="large" className="!rounded-lg" prefix={} />
{t('显示名称')} handleInputChange('display_name', value)} value={display_name} autoComplete="new-password" size="large" className="!rounded-lg" showClear />
{userId && (
{t('权限设置')}
{t('用户分组和额度管理')}
{t('分组')} handleInputChange('quota', value)} value={quota} type="number" autoComplete="new-password" size="large" className="flex-1 !rounded-lg" prefix={} />
)}
{t('绑定信息')}
{t('第三方账户绑定状态(只读)')}
{t('已绑定的 GitHub 账户')}
{t('已绑定的 OIDC 账户')}
{t('已绑定的微信账户')}
{t('已绑定的邮箱账户')}
{t('已绑定的 Telegram 账户')}
{ addLocalQuota(); setIsModalOpen(false); }} onCancel={() => setIsModalOpen(false)} closable={null} title={
{t('添加额度')}
} >
{`${t('新额度')}${renderQuota(quota)} + ${renderQuota(addQuotaLocal)} = ${renderQuota(quota + parseInt(addQuotaLocal || 0))}`}
{ setAddQuotaLocal(value); }} value={addQuotaLocal} type="number" autoComplete="new-password" size="large" className="!rounded-lg" prefix={} />
); }; export default EditUser;