13105bdba5
验证:868 项单测、ESLint、Prettier、UI 依赖审计和差异检查通过。
355 lines
10 KiB
React
355 lines
10 KiB
React
/*
|
||
Copyright (C) 2025 QuantumNous
|
||
|
||
This program is free software: you can redistribute it and/or modify
|
||
it under the terms of the GNU Affero General Public License as
|
||
published by the Free Software Foundation, either version 3 of the
|
||
License, or (at your option) any later version.
|
||
|
||
This program is distributed in the hope that it will be useful,
|
||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
GNU Affero General Public License for more details.
|
||
|
||
You should have received a copy of the GNU Affero General Public License
|
||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
||
For commercial licensing, please contact support@quantumnous.com
|
||
*/
|
||
import { formatIdReference } from '../../helpers/safeDisplay';
|
||
import {
|
||
RefreshCw as ActionRefreshCw,
|
||
Pencil as ActionPencil,
|
||
Plus as ActionPlus,
|
||
} from '@/components/common/icons/ActionIcons';
|
||
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
||
import { useTranslation } from 'react-i18next';
|
||
import {
|
||
Table,
|
||
Button,
|
||
Select,
|
||
Input,
|
||
Tag,
|
||
Toast,
|
||
Popconfirm,
|
||
Empty,
|
||
Typography,
|
||
} from '@/components/ui';
|
||
import { API } from '../../helpers/api';
|
||
import { showError } from '../../helpers';
|
||
import OrgPageHeader from './components/OrgPageHeader';
|
||
import OrgPanel from './components/OrgPanel';
|
||
import OrgModal from './components/OrgModal';
|
||
|
||
const { Text } = Typography;
|
||
const getOrgHeaders = (orgId) => ({ 'X-Org-Id': String(orgId) });
|
||
|
||
const memberName = (m) =>
|
||
m.remark ||
|
||
m.display_name ||
|
||
m.username ||
|
||
formatIdReference(m.org_member_id);
|
||
|
||
// 我的项目(阶段一):项目经理(项目负责人/项目管理员)自助管理项目成员与项目名。
|
||
const OrgProjectManage = ({ orgId }) => {
|
||
const { t } = useTranslation();
|
||
const [projects, setProjects] = useState([]);
|
||
const [memberOptions, setMemberOptions] = useState([]);
|
||
const [projectId, setProjectId] = useState(null);
|
||
const [members, setMembers] = useState([]);
|
||
const [loading, setLoading] = useState(false);
|
||
|
||
const [addModal, setAddModal] = useState(false);
|
||
const [addMemberId, setAddMemberId] = useState(null);
|
||
const [addRole, setAddRole] = useState('project_member');
|
||
const [adding, setAdding] = useState(false);
|
||
|
||
const [renameModal, setRenameModal] = useState(false);
|
||
const [renameValue, setRenameValue] = useState('');
|
||
const [renaming, setRenaming] = useState(false);
|
||
|
||
const headers = useMemo(() => getOrgHeaders(orgId), [orgId]);
|
||
const currentProject = useMemo(
|
||
() => projects.find((p) => p.id === projectId) || null,
|
||
[projects, projectId],
|
||
);
|
||
|
||
const fetchScope = useCallback(async () => {
|
||
try {
|
||
const res = await API.get('/api/org/me/project-scope', { headers });
|
||
if (res.data.success) {
|
||
const list = res.data.data?.projects || [];
|
||
setProjects(list);
|
||
setMemberOptions(res.data.data?.member_options || []);
|
||
setProjectId((prev) => prev || (list[0] ? list[0].id : null));
|
||
}
|
||
} catch (err) {
|
||
showError(err.message);
|
||
}
|
||
}, [headers]);
|
||
|
||
useEffect(() => {
|
||
fetchScope();
|
||
}, [fetchScope]);
|
||
|
||
const fetchMembers = useCallback(async () => {
|
||
if (!projectId) return;
|
||
setLoading(true);
|
||
try {
|
||
const res = await API.get(`/api/org/project/${projectId}/members`, {
|
||
headers,
|
||
});
|
||
if (res.data.success) setMembers(res.data.data || []);
|
||
else showError(res.data.message);
|
||
} catch (err) {
|
||
showError(err.message);
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
}, [projectId, headers]);
|
||
|
||
useEffect(() => {
|
||
if (projectId) fetchMembers();
|
||
}, [projectId, fetchMembers]);
|
||
|
||
// 可添加成员:排除已在项目中的
|
||
const addableOptions = useMemo(() => {
|
||
const inProject = new Set(members.map((m) => m.org_member_id));
|
||
return memberOptions
|
||
.filter((o) => !inProject.has(o.member_id))
|
||
.map((o) => ({ label: o.name, value: o.member_id }));
|
||
}, [memberOptions, members]);
|
||
|
||
const submitAdd = async () => {
|
||
if (!addMemberId) {
|
||
Toast.warning(t('请选择成员'));
|
||
return;
|
||
}
|
||
setAdding(true);
|
||
try {
|
||
const res = await API.post(
|
||
`/api/org/project/${projectId}/members`,
|
||
{ org_member_id: addMemberId, role: addRole },
|
||
{ headers },
|
||
);
|
||
if (res.data.success) {
|
||
Toast.success(t('成员已添加'));
|
||
setAddModal(false);
|
||
setAddMemberId(null);
|
||
setAddRole('project_member');
|
||
fetchMembers();
|
||
} else {
|
||
showError(res.data.message);
|
||
}
|
||
} catch (err) {
|
||
showError(err.message);
|
||
} finally {
|
||
setAdding(false);
|
||
}
|
||
};
|
||
|
||
const removeMember = async (orgMemberId) => {
|
||
try {
|
||
const res = await API.delete(
|
||
`/api/org/project/${projectId}/members/${orgMemberId}`,
|
||
{ headers },
|
||
);
|
||
if (res.data.success) {
|
||
Toast.success(t('成员已移除'));
|
||
fetchMembers();
|
||
} else {
|
||
showError(res.data.message);
|
||
}
|
||
} catch (err) {
|
||
showError(err.message);
|
||
}
|
||
};
|
||
|
||
const submitRename = async () => {
|
||
const name = renameValue.trim();
|
||
if (!name) {
|
||
showError(t('项目名称不能为空'));
|
||
return;
|
||
}
|
||
setRenaming(true);
|
||
try {
|
||
const res = await API.put(
|
||
`/api/org/project/${projectId}`,
|
||
{ name },
|
||
{ headers },
|
||
);
|
||
if (res.data.success) {
|
||
Toast.success(t('项目已更新'));
|
||
setRenameModal(false);
|
||
fetchScope();
|
||
} else {
|
||
showError(res.data.message);
|
||
}
|
||
} catch (err) {
|
||
showError(err.message);
|
||
} finally {
|
||
setRenaming(false);
|
||
}
|
||
};
|
||
|
||
const columns = [
|
||
{ title: t('成员'), render: (_, r) => memberName(r) },
|
||
{
|
||
title: t('项目角色'),
|
||
dataIndex: 'role',
|
||
render: (v) => (
|
||
<Tag color={v === 'project_admin' ? 'blue' : 'grey'}>
|
||
{v === 'project_admin' ? t('项目管理员') : t('项目成员')}
|
||
</Tag>
|
||
),
|
||
},
|
||
{
|
||
title: t('操作'),
|
||
render: (_, r) => (
|
||
<Popconfirm
|
||
title={t('确认移除该成员?')}
|
||
content={t('移除后该成员将失去对本项目的访问权限。')}
|
||
onConfirm={() => removeMember(r.org_member_id)}
|
||
>
|
||
<Button size='small' type='danger' theme='light'>
|
||
{t('移除')}
|
||
</Button>
|
||
</Popconfirm>
|
||
),
|
||
},
|
||
];
|
||
|
||
return (
|
||
<div className='uc-page'>
|
||
<OrgPageHeader
|
||
title={t('我的项目')}
|
||
subtitle={t('管理您负责的项目成员')}
|
||
actions={
|
||
<>
|
||
<Select
|
||
value={projectId}
|
||
onChange={setProjectId}
|
||
placeholder={t('选择项目')}
|
||
style={{ width: 200 }}
|
||
optionList={projects.map((p) => ({
|
||
label: p.code ? `${p.name}(${p.code})` : p.name,
|
||
value: p.id,
|
||
}))}
|
||
/>
|
||
<Button
|
||
theme='light'
|
||
icon={<ActionRefreshCw size={14} />}
|
||
onClick={() => {
|
||
fetchScope();
|
||
fetchMembers();
|
||
}}
|
||
>
|
||
{t('刷新')}
|
||
</Button>
|
||
</>
|
||
}
|
||
/>
|
||
|
||
{projects.length === 0 ? (
|
||
<Empty
|
||
description={t('您当前没有负责的项目')}
|
||
style={{ padding: 60 }}
|
||
/>
|
||
) : (
|
||
<OrgPanel>
|
||
<div className='flex items-center justify-between mb-3 flex-wrap gap-2'>
|
||
<div>
|
||
<Text strong>{currentProject?.name || ''}</Text>
|
||
{currentProject?.code && (
|
||
<Text type='tertiary' size='small' className='ml-2'>
|
||
{currentProject.code}
|
||
</Text>
|
||
)}
|
||
</div>
|
||
<div className='flex items-center gap-2'>
|
||
<Button
|
||
size='small'
|
||
theme='light'
|
||
icon={<ActionPencil size={14} />}
|
||
onClick={() => {
|
||
setRenameValue(currentProject?.name || '');
|
||
setRenameModal(true);
|
||
}}
|
||
>
|
||
{t('重命名')}
|
||
</Button>
|
||
<Button
|
||
size='small'
|
||
theme='solid'
|
||
icon={<ActionPlus size={14} />}
|
||
onClick={() => setAddModal(true)}
|
||
>
|
||
{t('添加成员')}
|
||
</Button>
|
||
</div>
|
||
</div>
|
||
<Table
|
||
columns={columns}
|
||
dataSource={members}
|
||
loading={loading}
|
||
rowKey='org_member_id'
|
||
pagination={false}
|
||
empty={<Empty description={t('暂无项目成员')} />}
|
||
/>
|
||
</OrgPanel>
|
||
)}
|
||
|
||
{/* 添加成员 */}
|
||
<OrgModal
|
||
title={t('添加项目成员')}
|
||
visible={addModal}
|
||
onOk={submitAdd}
|
||
onCancel={() => setAddModal(false)}
|
||
okText={t('添加')}
|
||
cancelText={t('取消')}
|
||
confirmLoading={adding}
|
||
>
|
||
<div className='space-y-3'>
|
||
<Select
|
||
placeholder={t('选择成员')}
|
||
style={{ width: '100%' }}
|
||
value={addMemberId}
|
||
onChange={setAddMemberId}
|
||
filter
|
||
optionList={addableOptions}
|
||
/>
|
||
<Select
|
||
style={{ width: '100%' }}
|
||
value={addRole}
|
||
onChange={setAddRole}
|
||
optionList={[
|
||
{ label: t('项目成员'), value: 'project_member' },
|
||
{ label: t('项目管理员'), value: 'project_admin' },
|
||
]}
|
||
/>
|
||
</div>
|
||
</OrgModal>
|
||
|
||
{/* 重命名项目 */}
|
||
<OrgModal
|
||
title={t('重命名项目')}
|
||
visible={renameModal}
|
||
onOk={submitRename}
|
||
onCancel={() => setRenameModal(false)}
|
||
okText={t('保存')}
|
||
cancelText={t('取消')}
|
||
confirmLoading={renaming}
|
||
>
|
||
<Input
|
||
value={renameValue}
|
||
onChange={setRenameValue}
|
||
placeholder={t('请输入项目名称')}
|
||
onEnterPress={submitRename}
|
||
/>
|
||
</OrgModal>
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default OrgProjectManage;
|