Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,478 @@
|
||||
/*
|
||||
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 React, { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import {
|
||||
Button,
|
||||
InputNumber,
|
||||
Modal,
|
||||
Select,
|
||||
Table,
|
||||
Toast,
|
||||
Typography,
|
||||
} from '@douyinfe/semi-ui';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { API } from '../../helpers';
|
||||
import {
|
||||
ActivationModeTag,
|
||||
AssignmentModeTag,
|
||||
SkillEmptyGuide,
|
||||
} from '../../components/skill';
|
||||
import {
|
||||
usePageGuide,
|
||||
PageGuideDrawer,
|
||||
PageGuidePill,
|
||||
PageGuideTrigger,
|
||||
flashGuideTarget,
|
||||
} from '../../components/common/PageGuide';
|
||||
import OrgPanel from './components/OrgPanel';
|
||||
import './org-skills.css';
|
||||
|
||||
const { Title, Paragraph, Text } = Typography;
|
||||
|
||||
const defaultForm = {
|
||||
skill_id: 0,
|
||||
scope_type: 'organization',
|
||||
scope_id: 0,
|
||||
scope_value: '',
|
||||
mode: 'optional',
|
||||
activation: 'manual',
|
||||
version_policy: 'follow',
|
||||
version_id: 0,
|
||||
effective_from: 0,
|
||||
effective_to: 0,
|
||||
};
|
||||
|
||||
const OrgSkillAssignments = ({ orgId, orgRole = '' }) => {
|
||||
const { t } = useTranslation();
|
||||
const headers = useMemo(() => ({ 'X-Org-Id': String(orgId) }), [orgId]);
|
||||
// Department admins are legitimate assignment actors but can only target
|
||||
// their own subtree; offering organization/project/member scopes would
|
||||
// produce guaranteed backend rejections.
|
||||
const isDeptAdmin = orgRole === 'dept_admin';
|
||||
const deptDefaultForm = useMemo(
|
||||
() =>
|
||||
isDeptAdmin ? { ...defaultForm, scope_type: 'department' } : defaultForm,
|
||||
[isDeptAdmin],
|
||||
);
|
||||
const scopeOptions = isDeptAdmin
|
||||
? ['department', 'department_leader']
|
||||
: [
|
||||
'organization',
|
||||
'department',
|
||||
'project',
|
||||
'role',
|
||||
'department_leader',
|
||||
'member',
|
||||
];
|
||||
const [items, setItems] = useState([]);
|
||||
const [library, setLibrary] = useState([]);
|
||||
// 首次加载完成前进度胶囊不渲染,避免对老用户闪现「0/4」。
|
||||
const [loaded, setLoaded] = useState(false);
|
||||
const [visible, setVisible] = useState(false);
|
||||
const [previewMember, setPreviewMember] = useState(0);
|
||||
const [preview, setPreview] = useState([]);
|
||||
const [form, setForm] = useState(deptDefaultForm);
|
||||
const load = useCallback(async () => {
|
||||
// finally 保证请求失败也置 loaded:拦截器已 toast,此处只需不让
|
||||
// 未捕获 reject 把 loaded 卡在 false。
|
||||
try {
|
||||
const [assignments, policies] = await Promise.all([
|
||||
API.get('/api/org/skills/assignments', { headers }),
|
||||
// The options endpoint is readable by every assignment actor;
|
||||
// /library requires skill_manage, which dept_admin does not hold.
|
||||
API.get('/api/org/skills/assignments/options', { headers }),
|
||||
]);
|
||||
if (assignments.data?.success) setItems(assignments.data.data || []);
|
||||
if (policies.data?.success)
|
||||
setLibrary((policies.data.data || []).filter((item) => item.enabled));
|
||||
} finally {
|
||||
setLoaded(true);
|
||||
}
|
||||
}, [headers]);
|
||||
useEffect(() => {
|
||||
void load();
|
||||
}, [load]);
|
||||
const save = async () => {
|
||||
try {
|
||||
const response = await API.post('/api/org/skills/assignments', form, {
|
||||
headers,
|
||||
skipErrorHandler: true,
|
||||
});
|
||||
if (!response.data?.success) {
|
||||
Toast.error(response.data?.message || t('保存失败'));
|
||||
return;
|
||||
}
|
||||
(response.data.warnings || []).forEach((warning) =>
|
||||
Toast.warning(warning),
|
||||
);
|
||||
setVisible(false);
|
||||
setForm(deptDefaultForm);
|
||||
await load();
|
||||
} catch (error) {
|
||||
Toast.error(
|
||||
error.response?.data?.message || error.message || t('保存失败'),
|
||||
);
|
||||
}
|
||||
};
|
||||
const remove = async (id) => {
|
||||
const response = await API.delete(`/api/org/skills/assignments/${id}`, {
|
||||
headers,
|
||||
});
|
||||
if (response.data?.success) await load();
|
||||
};
|
||||
// 指引步骤④「用预览验证」的完成态按组织持久化:preview 是会话内状态,
|
||||
// 不落盘的话每次进页胶囊都会复活(与知识页 verified-key 同语义)。
|
||||
// setItem 在隐私模式/配额耗尽时会抛异常,必须吞掉。
|
||||
const previewedKey = `org-skill-assignments-previewed-${orgId}`;
|
||||
const runPreview = async () => {
|
||||
const response = await API.get('/api/org/skills/preview', {
|
||||
headers,
|
||||
params: { member_id: previewMember },
|
||||
});
|
||||
if (!response.data?.success) {
|
||||
Toast.error(response.data?.message || t('预览失败'));
|
||||
return;
|
||||
}
|
||||
setPreview(response.data.data || []);
|
||||
try {
|
||||
window.localStorage.setItem(previewedKey, '1');
|
||||
} catch {
|
||||
// 忽略:仅损失跨会话记忆
|
||||
}
|
||||
};
|
||||
const previewVerified = (() => {
|
||||
try {
|
||||
return window.localStorage.getItem(previewedKey) === '1';
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
})();
|
||||
const columns = [
|
||||
{ title: t('Skill'), dataIndex: 'skill_id' },
|
||||
{
|
||||
title: t('范围'),
|
||||
render: (_, row) =>
|
||||
`${row.scope_type}:${row.scope_id || row.scope_value || 'all'}`,
|
||||
},
|
||||
{
|
||||
title: t('模式'),
|
||||
render: (_, row) => <AssignmentModeTag mode={row.mode} />,
|
||||
},
|
||||
{
|
||||
title: t('激活'),
|
||||
render: (_, row) => <ActivationModeTag mode={row.activation} />,
|
||||
},
|
||||
{ title: t('版本策略'), dataIndex: 'version_policy' },
|
||||
{
|
||||
title: t('操作'),
|
||||
render: (_, row) => (
|
||||
<Button type='danger' theme='borderless' onClick={() => remove(row.id)}>
|
||||
{t('删除')}
|
||||
</Button>
|
||||
),
|
||||
},
|
||||
];
|
||||
const openCreateModal = () => {
|
||||
setForm(deptDefaultForm);
|
||||
setVisible(true);
|
||||
};
|
||||
// 使用指引:分发策略决定「谁能用哪些技能」,完成态由策略/预览数据驱动。
|
||||
const guide = usePageGuide({
|
||||
guideKey: 'org-skill-assignments',
|
||||
ready: loaded,
|
||||
title: t('分发与锁定 · 使用指引'),
|
||||
intro: t('技能启用后,靠分发策略决定对哪些成员生效'),
|
||||
steps: [
|
||||
{
|
||||
title: t('创建分发策略'),
|
||||
detail: t('选技能、选范围、定模式,一条策略就是一条生效规则'),
|
||||
done: items.length > 0,
|
||||
cta: t('去创建策略'),
|
||||
onAction: openCreateModal,
|
||||
points: [
|
||||
t(
|
||||
'可选范围:全企业 / 部门子树 / 项目 / 角色 / 部门负责人 / 指定成员',
|
||||
),
|
||||
isDeptAdmin
|
||||
? t('部门管理员只能面向本部门子树与部门负责人分发')
|
||||
: t('技能下拉里只出现已在技能库启用的技能'),
|
||||
],
|
||||
},
|
||||
{
|
||||
title: t('理解模式与激活'),
|
||||
detail: t('模式决定成员能否关闭,激活决定何时注入'),
|
||||
done: items.length > 0,
|
||||
points: [
|
||||
t('模式:必需锁定(强制启用不可关)/ 默认开启 / 推荐 / 可选 / 禁止'),
|
||||
t(
|
||||
'激活:always 始终注入 / auto 按任务自动匹配 / manual 成员手动开启',
|
||||
),
|
||||
t('多条策略叠加时按范围精细度取胜;保存时的黄色提示会指出冲突'),
|
||||
],
|
||||
},
|
||||
{
|
||||
title: t('版本策略'),
|
||||
detail: t('决定成员用的是哪个版本'),
|
||||
done: items.length > 0,
|
||||
points: [
|
||||
t('「跟随升级」自动使用企业技能库的当前版本'),
|
||||
t('「锁定版本」填写版本 ID,把该范围固定在指定版本上'),
|
||||
],
|
||||
},
|
||||
{
|
||||
title: t('用预览验证'),
|
||||
detail: t('回答「为什么成员 X 有 / 没有技能 Y」'),
|
||||
done: preview.length > 0 || previewVerified,
|
||||
cta: t('去预览'),
|
||||
onAction: () =>
|
||||
flashGuideTarget(
|
||||
'.org-skill-preview',
|
||||
'.org-skill-preview-controls input',
|
||||
),
|
||||
points: [
|
||||
t('在「成员能力预览」输入成员 ID,查看该成员最终生效的能力集'),
|
||||
t('每行展示生效来源链,与成员「我的技能」页看到的一致'),
|
||||
],
|
||||
},
|
||||
],
|
||||
faq: [
|
||||
{
|
||||
q: t('创建时弹出黄色警告?'),
|
||||
a: t('策略已保存,警告提示与既有策略存在覆盖或冲突,按提示核对即可'),
|
||||
},
|
||||
{
|
||||
q: t('下拉里找不到想分发的技能?'),
|
||||
a: t('先到「技能超市」为企业启用该技能,使其进入企业技能库'),
|
||||
},
|
||||
],
|
||||
});
|
||||
const closeGuide = guide.close;
|
||||
// 预览结果与抽屉进度都是组织私有状态:切组织时清空/关闭并复位 loaded,
|
||||
// 否则旧组织的预览行/策略数据会把新组织的完成态渲染错。
|
||||
useEffect(() => {
|
||||
setPreview([]);
|
||||
setPreviewMember(0);
|
||||
setLoaded(false);
|
||||
closeGuide();
|
||||
}, [orgId, closeGuide]);
|
||||
return (
|
||||
<main className='org-skills-page'>
|
||||
<header className='org-skills-hero'>
|
||||
<div>
|
||||
<Title heading={3}>{t('分发与锁定')}</Title>
|
||||
<Paragraph>
|
||||
{t(
|
||||
'按企业、部门子树、项目、角色、负责人或成员分发,并实时预览最终能力集。',
|
||||
)}
|
||||
</Paragraph>
|
||||
</div>
|
||||
<div className='org-skills-hero-actions'>
|
||||
<PageGuideTrigger guide={guide} />
|
||||
<Button theme='solid' type='primary' onClick={openCreateModal}>
|
||||
{t('创建分发策略')}
|
||||
</Button>
|
||||
</div>
|
||||
</header>
|
||||
<PageGuidePill guide={guide} />
|
||||
<PageGuideDrawer guide={guide} />
|
||||
<OrgPanel title={t('策略列表')}>
|
||||
{items.length === 0 ? (
|
||||
<SkillEmptyGuide
|
||||
title={t('还没有分发策略')}
|
||||
description={t(
|
||||
'技能启用后还需要分发策略才会对成员生效,先创建第一条。',
|
||||
)}
|
||||
actionLabel={t('创建分发策略')}
|
||||
onAction={openCreateModal}
|
||||
/>
|
||||
) : (
|
||||
<Table
|
||||
columns={columns}
|
||||
dataSource={items}
|
||||
rowKey='id'
|
||||
pagination={false}
|
||||
/>
|
||||
)}
|
||||
</OrgPanel>
|
||||
<OrgPanel title={t('成员能力预览')} className='org-skill-preview'>
|
||||
<div className='org-skill-preview-controls'>
|
||||
<InputNumber
|
||||
min={1}
|
||||
value={previewMember}
|
||||
onChange={setPreviewMember}
|
||||
placeholder={t('成员 ID')}
|
||||
/>
|
||||
<Button onClick={runPreview}>{t('预览')}</Button>
|
||||
</div>
|
||||
{preview.map((item) => (
|
||||
<div key={item.skill_id} className='org-skill-preview-row'>
|
||||
<Text strong>{item.canonical_id}</Text>
|
||||
<AssignmentModeTag mode={item.mode} />
|
||||
<Text>{item.enabled ? t('生效') : t('未生效')}</Text>
|
||||
<Text type='tertiary'>
|
||||
{(item.sources || [])
|
||||
.map(
|
||||
(source) =>
|
||||
`${source.scope_type}:${source.scope_id || source.scope_value || 'all'}`,
|
||||
)
|
||||
.join(' > ')}
|
||||
</Text>
|
||||
</div>
|
||||
))}
|
||||
</OrgPanel>
|
||||
<Modal
|
||||
title={t('创建分发策略')}
|
||||
visible={visible}
|
||||
onCancel={() => setVisible(false)}
|
||||
onOk={save}
|
||||
>
|
||||
<div className='org-skill-form'>
|
||||
<label>
|
||||
<Text>Skill</Text>
|
||||
<Select
|
||||
value={form.skill_id}
|
||||
onChange={(skill_id) => setForm((old) => ({ ...old, skill_id }))}
|
||||
>
|
||||
{library.map((item) => (
|
||||
<Select.Option key={item.skill_id} value={item.skill_id}>
|
||||
Skill #{item.skill_id}
|
||||
</Select.Option>
|
||||
))}
|
||||
</Select>
|
||||
</label>
|
||||
<label>
|
||||
<Text>{t('范围')}</Text>
|
||||
<Select
|
||||
value={form.scope_type}
|
||||
onChange={(scope_type) =>
|
||||
setForm((old) => ({
|
||||
...old,
|
||||
scope_type,
|
||||
scope_id: scope_type === 'organization' ? 0 : old.scope_id,
|
||||
}))
|
||||
}
|
||||
>
|
||||
{scopeOptions.map((value) => (
|
||||
<Select.Option key={value} value={value}>
|
||||
{value}
|
||||
</Select.Option>
|
||||
))}
|
||||
{!isDeptAdmin && (
|
||||
<Select.Option value='expert'>{t('指定专家')}</Select.Option>
|
||||
)}
|
||||
</Select>
|
||||
</label>
|
||||
{form.scope_type !== 'organization' && form.scope_type !== 'role' && (
|
||||
<label>
|
||||
<Text>Scope ID</Text>
|
||||
<InputNumber
|
||||
min={1}
|
||||
value={form.scope_id}
|
||||
onChange={(scope_id) =>
|
||||
setForm((old) => ({ ...old, scope_id }))
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
)}
|
||||
{form.scope_type === 'role' && (
|
||||
<label>
|
||||
<Text>{t('角色')}</Text>
|
||||
<Select
|
||||
value={form.scope_value}
|
||||
onChange={(scope_value) =>
|
||||
setForm((old) => ({ ...old, scope_value }))
|
||||
}
|
||||
>
|
||||
{['owner', 'admin', 'dept_admin', 'member'].map((value) => (
|
||||
<Select.Option key={value} value={value}>
|
||||
{value}
|
||||
</Select.Option>
|
||||
))}
|
||||
</Select>
|
||||
</label>
|
||||
)}
|
||||
<label>
|
||||
<Text>{t('模式')}</Text>
|
||||
<Select
|
||||
value={form.mode}
|
||||
onChange={(mode) => setForm((old) => ({ ...old, mode }))}
|
||||
>
|
||||
{[
|
||||
'required_locked',
|
||||
'default_on',
|
||||
'recommended',
|
||||
'optional',
|
||||
'forbidden',
|
||||
].map((value) => (
|
||||
<Select.Option key={value} value={value}>
|
||||
{value}
|
||||
</Select.Option>
|
||||
))}
|
||||
</Select>
|
||||
</label>
|
||||
<label>
|
||||
<Text>{t('激活')}</Text>
|
||||
<Select
|
||||
value={form.activation}
|
||||
onChange={(activation) =>
|
||||
setForm((old) => ({ ...old, activation }))
|
||||
}
|
||||
>
|
||||
{['always', 'auto', 'manual'].map((value) => (
|
||||
<Select.Option key={value} value={value}>
|
||||
{value}
|
||||
</Select.Option>
|
||||
))}
|
||||
<Select.Option value='workflow_step' disabled>
|
||||
{t('工作流步骤(ADR 批准并独立立项后开放)')}
|
||||
</Select.Option>
|
||||
</Select>
|
||||
</label>
|
||||
<label>
|
||||
<Text>{t('版本策略')}</Text>
|
||||
<Select
|
||||
value={form.version_policy}
|
||||
onChange={(version_policy) =>
|
||||
setForm((old) => ({ ...old, version_policy }))
|
||||
}
|
||||
>
|
||||
<Select.Option value='follow'>{t('跟随升级')}</Select.Option>
|
||||
<Select.Option value='locked'>{t('锁定版本')}</Select.Option>
|
||||
</Select>
|
||||
</label>
|
||||
{form.version_policy === 'locked' && (
|
||||
<label>
|
||||
<Text>Version ID</Text>
|
||||
<InputNumber
|
||||
min={1}
|
||||
value={form.version_id}
|
||||
onChange={(version_id) =>
|
||||
setForm((old) => ({ ...old, version_id }))
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
)}
|
||||
</div>
|
||||
</Modal>
|
||||
</main>
|
||||
);
|
||||
};
|
||||
|
||||
export default OrgSkillAssignments;
|
||||
Reference in New Issue
Block a user