/*
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 .
For commercial licensing, please contact support@quantumnous.com
*/
import React, { useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import {
Alert,
Button,
Empty,
Modal,
Spin,
Switch,
Tag,
Typography,
} from 'antd';
import { showError, showSuccess } from '../../helpers';
import { getConsents, updateConsent } from './api';
const { Text } = Typography;
const kindText = (t, kind) =>
({
judge: {
title: t('智能判定'),
desc: t(
'把脱敏、截断后的请求片段发送给境外判定服务(Jev),用于选择合适的模型。',
),
},
eval: {
title: t('质量打分'),
desc: t(
'把脱敏、截断后的请求与回答片段发送给境外判定服务,用于质量评估。',
),
},
shadow: {
title: t('影子评审'),
desc: t('把正式脱敏后的请求发送给基线模型和评审模型,用于对比质量。'),
},
replay: {
title: t('回放留存'),
desc: t('把脱敏后的请求加密留存最多 90 天,用于离线评估。'),
},
})[kind] || { title: kind, desc: '' };
// 同意记录(§9.5)。企业同意只能由真实的企业所有者操作。
// onChange:授权状态变化后回调最新列表(企业中心"仅授权模式"据此刷新入口)
const ConsentPanel = ({ mode, orgId, canWrite = true, onChange }) => {
const { t } = useTranslation();
const [rows, setRows] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState('');
const load = useCallback(async () => {
setLoading(true);
setError('');
try {
setRows((await getConsents(mode, orgId)) || []);
} catch (e) {
setError(e?.message || t('加载失败'));
} finally {
setLoading(false);
}
}, [mode, orgId, t]);
useEffect(() => {
load();
}, [load]);
const toggle = (row, granted) => {
const text = kindText(t, row.kind);
const doIt = async () => {
try {
const next = await updateConsent(mode, orgId, row.kind, granted);
setRows(next);
onChange?.(next);
showSuccess(granted ? t('已授权') : t('已撤回'));
} catch (e) {
if (e?.message) showError(e.message);
load(); // 写入可能已成功、只是回读失败:重新拉取,避免开关停在旧状态
}
};
if (!granted) return doIt();
Modal.confirm({
title: `${t('确认授权')} · ${text.title}`,
content: (
{text.desc}
{row.overseas && (
)}
{mode === 'org' && (
{t('授权后会覆盖企业「数据安全」「不用于训练」功能的默认保护。')}
)}
),
onOk: doIt,
});
};
if (loading)
return (
);
if (error)
return (
{t('重试')}
}
/>
);
const available = rows.filter((r) => r.available);
if (available.length === 0)
return (
);
return (
{!canWrite && (
)}
{available.map((r) => {
const text = kindText(t, r.kind);
return (
{text.title}
{r.overseas && (
{t('境外处理')}
)}
{r.valid ? (
{t('已生效')}
) : r.granted ? (
{t('文案已更新,需要重新授权')}
) : null}
{text.desc}
toggle(r, v)}
aria-label={text.title}
/>
);
})}
);
};
export default ConsentPanel;