/** * 知识库配置管理组件 * * 提供地区-知识库绑定管理功能,包括增删改查 * * @author 开发团队 * @version 1.0.0 */ import { useEffect } from 'react'; import { Card, Table, Button, Tag, Space, Modal, Form, Input, Select, Switch, InputNumber, message, Flex, Typography, Popconfirm, Tooltip, } from 'antd'; import { PlusOutlined, EditOutlined, DeleteOutlined, InfoCircleOutlined, CheckCircleOutlined, } from '@ant-design/icons'; import { useAreaDatasetConfig } from '~/hooks/use-area-dataset-config'; import { usePermission } from '~/hooks/usePermission'; import type { AreaDataset } from '~/api/v3/dify/area-datasets'; const { Title, Text } = Typography; // 颜色常量 const colors = { bgLayout: '#f5f5f5', border: '#e8e8e8', text: '#262626', textSecondary: '#8c8c8c', primary: '#00684a', }; /** * 知识库配置管理组件 */ export default function AreaDatasetConfig() { // 使用自定义hook获取数据和操作方法 const { // 数据 datasets, loading, total, userArea, userRole, areas, // areasLoading, // 地区列表已加载hook中 // 筛选 - 多选 filterAreas, setFilterAreas, page, setPage, pageSize, // 表单状态 modalVisible, setModalVisible, editingId, setEditingId, submitLoading, // 操作方法 loadDatasets, loadAreas, handleCreate, handleUpdate, handleDelete, // 权限 canManageDataset, } = useAreaDatasetConfig(); const { userRole: rawUserRole, userArea: rawUserArea } = usePermission(); const isProvincialAdmin = rawUserRole === 'provincial_admin'; // 内部状态 const [form] = Form.useForm(); // ==================== Effects ==================== // 当编辑的ID变化时,加载表单数据 useEffect(() => { if (editingId && modalVisible) { const record = datasets.find((item) => item.id === editingId); if (record) { form.setFieldsValue({ area: record.area, dataset_name: record.dataset_name, dataset_description: record.dataset_description, is_public: record.is_public, is_default: record.is_default, sort_order: record.sort_order, }); } } else if (!editingId && modalVisible) { form.resetFields(); // 非省级管理员自动填充地区 if (!isProvincialAdmin && rawUserArea) { form.setFieldValue('area', rawUserArea); } } }, [editingId, modalVisible, datasets, form]); // ==================== Event Handlers ==================== /** * 处理新建按钮点击 */ const handleCreateClick = () => { if (!canManageDataset) { message.error('您没有创建知识库的权限'); return; } setEditingId(null); setModalVisible(true); form.resetFields(); }; /** * 处理编辑按钮点击 */ const handleEditClick = (record: AreaDataset) => { if (!canManageDataset) { message.error('您没有编辑知识库绑定的权限'); return; } setEditingId(record.id); setModalVisible(true); }; /** * 处理删除按钮点击 */ const handleDeleteClick = async (id: number) => { if (!canManageDataset) { message.error('您没有删除知识库绑定的权限'); return; } const success = await handleDelete(id); if (success) { message.success('删除成功'); } }; /** * 处理表单提交 */ const handleFormSubmit = async (values: any) => { // 编辑时检查 is_default 是否从 false 变为 true if (editingId && values.is_default) { const record = datasets.find((item) => item.id === editingId); if (record && !record.is_default) { Modal.confirm({ title: '切换默认知识库', content: '确认将此知识库设为默认?该地区的对话助手将自动绑定此知识库进行问答。', okText: '确认', cancelText: '取消', onOk: () => doSubmit(values), }); return; } } await doSubmit(values); }; const doSubmit = async (values: any) => { let success = false; if (editingId) { success = await handleUpdate(editingId, values); } else { success = await handleCreate(values); } if (success) { setModalVisible(false); form.resetFields(); setEditingId(null); } }; /** * 处理表单取消 */ const handleFormCancel = () => { setModalVisible(false); form.resetFields(); setEditingId(null); }; /** * 处理地区筛选变化 - 支持多选 */ const handleAreaFilterChange = (values: string[]) => { setFilterAreas(values); setPage(1); // 重置到第一页 }; // ==================== Render ==================== // 用户角色已经在 hook 中处理好了,直接使用 userRole const userRoleLabel = userRole || '未知角色'; // 表格列定义 const columns = [ { title: '序号', key: 'index', width: 60, render: (_: any, __: any, index: number) => (page - 1) * pageSize + index + 1, }, { title: '地区', dataIndex: 'area', key: 'area', width: 100, render: (area: string) => ( {area} ), }, { title: '知识库名称', dataIndex: 'dataset_name', key: 'dataset_name', width: 200, ellipsis: true, align: 'center', render: (text: string) => ( {text} ), }, // { // title: '知识库ID', // dataIndex: 'dataset_id', // key: 'dataset_id', // width: 200, // ellipsis: true, // render: (text: string) => ( // // // {text.substring(0, 8)}...{text.substring(text.length - 4)} // // // ), // }, { title: '描述', dataIndex: 'dataset_description', key: 'dataset_description', ellipsis: true, align: 'center', render: (text: string) => text ? ( {text.length > 30 ? text.substring(0, 30) + '...' : text} ) : ( - ), }, { title: '标签', key: 'tags', width: 120, render: (_: any, record: AreaDataset) => ( {record.is_public && ( }> 公共 )} {record.is_default && ( 默认 )} ), }, { title: '排序', dataIndex: 'sort_order', key: 'sort_order', width: 170, align: 'center' as const, sorter: (a: AreaDataset, b: AreaDataset) => a.sort_order - b.sort_order, }, { title: '状态', dataIndex: 'status', key: 'status', width: 80, render: (status: number) => ( {status === 1 ? '启用' : '禁用'} ), }, { title: '创建时间', dataIndex: 'created_at', key: 'created_at', width: 150, render: (text: string) => ( {new Date(text).toLocaleString('zh-CN')} ), }, // 操作列(仅管理员可见) ...(canManageDataset ? [ { title: '操作', key: 'actions', width: 120, fixed: 'right' as const, render: (_: any, record: AreaDataset) => { // 市级管理员只能编辑自己地区的知识库 const canEdit = isProvincialAdmin || record.area === rawUserArea; return ( {canEdit && ( )} {isProvincialAdmin && ( handleDeleteClick(record.id)} okText="确定" cancelText="取消" > )} ); }, }, ] : []), ]; return (
{/* 页面头部 */}
知识库配置管理 地区: {userArea || '-'} 角色: {userRoleLabel} 总数: {total}
{/* 仅管理员显示新增按钮 */} {canManageDataset && ( )}
{/* 筛选区域(仅管理员可见) */} {canManageDataset && ( 地区筛选: ({ label: area, value: area })) : []) : (rawUserArea ? [{ label: rawUserArea, value: rawUserArea }] : []) } /> {/* 知识库名称 */} {/* 知识库描述 */} {/* 高级设置 */}
高级设置
); }