Files
monkey-ai-web/src/pages/Organization/datasecurity/OrgDsTest.jsx
T

140 lines
4.6 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 React, { useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { FlaskConical } from 'lucide-react';
import { Button, Tag, TextArea, Typography } from '@/components/ui';
import { API } from '../../../helpers/api';
import { showError } from '../../../helpers';
import OrgPanel from '../components/OrgPanel';
// OrgDsTest 脱敏测试子 tab:按本企业生效策略 + 生效规则集(含企业私有规则)演示脱敏效果。
// 样本仅内存处理、不入库。
const OrgDsTest = ({ orgId }) => {
const { t } = useTranslation();
const headers = useMemo(() => ({ 'X-Org-Id': String(orgId) }), [orgId]);
const [text, setText] = useState('我的邮箱 zhang@abc.com,手机 13800138000');
const [testing, setTesting] = useState(false);
const [result, setResult] = useState(null);
const runTest = async () => {
if (!text.trim()) {
showError(t('请输入要测试的样本文本'));
return;
}
setTesting(true);
try {
const res = await API.post(
'/api/org/data-security/test',
{ text },
{ headers },
);
if (res.data.success) {
setResult(res.data.data);
} else {
showError(res.data.message || t('测试失败'));
}
} catch (err) {
if (err?.name !== 'AxiosError') showError(err);
} finally {
setTesting(false);
}
};
return (
<OrgPanel
icon={FlaskConical}
title={t('脱敏测试')}
subtitle={t(
'按本企业生效策略与规则集演示脱敏效果,样本仅内存处理、不入库',
)}
padding='p-5'
className='mt-3 mb-2'
>
<TextArea
value={text}
onChange={setText}
autosize={{ minRows: 3, maxRows: 8 }}
maxLength={10000}
placeholder={t('输入样本文本(样本不入库,仅内存处理)')}
/>
<div className='mt-3 flex items-center justify-end'>
<Button
theme='solid'
type='primary'
loading={testing}
disabled={!text.trim()}
onClick={runTest}
>
{t('测试脱敏效果')}
</Button>
</div>
{result && (
<div className='mt-4'>
<div className='flex flex-wrap items-center gap-2 mb-2'>
<span className='text-sm font-medium text-gray-700 dark:text-gray-300'>
{t('脱敏后')}
</span>
{result.mode && (
<Tag color='blue'>
{t('本企业生效模式')}: {result.mode}
</Tag>
)}
{(result.mode === 'detect_only' || result.mode === 'block') && (
<Tag color='amber'>
{result.mode === 'block'
? t('block 模式下命中即拒绝,以下为命中演示')
: t('detect_only 模式下不改写,以下为命中演示')}
</Tag>
)}
</div>
<Typography.Paragraph
copyable
className='rounded-lg px-3 py-2 break-all'
style={{ background: 'var(--ui-color-fill-0)' }}
>
{result.desensitized}
</Typography.Paragraph>
<div className='mt-2 flex flex-wrap items-center gap-1.5'>
<span
className='text-sm text-gray-600 dark:text-gray-300 mr-1'
style={{ fontVariantNumeric: 'tabular-nums' }}
>
{t('命中')} {result.hit_count}
</span>
{(result.hits || []).map((h, i) => (
<Tag key={i} color={h.pii_level === 'sensitive' ? 'red' : 'blue'}>
{h.category} → {h.token}
</Tag>
))}
{result.hit_count === 0 && (
<span className='text-sm text-gray-400'>
{t('未命中任何规则')}
</span>
)}
</div>
</div>
)}
</OrgPanel>
);
};
export default OrgDsTest;