/** * 知识库配置管理组件 * * 提供地区-知识库绑定管理功能,包括增删改查 * * @author 开发团队 * @version 1.0.0 */ import { useState, useEffect } from 'react'; import { Card, Table, Button, Tag, Space, Modal, Form, Input, Select, Switch, InputNumber, message, Flex, Typography, Popconfirm, Spin, Tooltip, } from 'antd'; import { PlusOutlined, EditOutlined, DeleteOutlined, InfoCircleOutlined, CheckCircleOutlined, } from '@ant-design/icons'; import { useAreaDatasetConfig } from '~/hooks/use-area-dataset-config'; import { fetchDatasets } from '~/api/dify-dataset/api/datasetApi'; import type { Dataset as DifyDataset } from '~/api/dify-dataset/type'; 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 [form] = Form.useForm(); const [difyDatasets, setDifyDatasets] = useState([]); const [difyDatasetsLoading, setDifyDatasetsLoading] = useState(false); const [difyDatasetsTotal, setDifyDatasetsTotal] = useState(0); const [difyDatasetsPage, setDifyDatasetsPage] = useState(1); const [isLoadingDifyDatasets, setIsLoadingDifyDatasets] = useState(false); // ==================== Effects ==================== // 当编辑的ID变化时,加载表单数据 useEffect(() => { if (editingId && modalVisible) { const record = datasets.find((item) => item.id === editingId); if (record) { form.setFieldsValue({ area: record.area, dataset_id: record.dataset_id, 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(); loadDifyDatasets(); // 加载Dify知识库列表 } }, [editingId, modalVisible, datasets, form]); // ==================== Dify Datasets Loading ==================== /** * 从Dify API加载知识库列表 */ const loadDifyDatasets = async (pageNum: number = 1) => { if (isLoadingDifyDatasets) return; setIsLoadingDifyDatasets(true); try { const response = await fetchDatasets(pageNum, 20); setDifyDatasets(response.data); setDifyDatasetsTotal(response.total); setDifyDatasetsPage(pageNum); setDifyDatasetsLoading(false); } catch (error: any) { console.error('加载Dify知识库列表失败:', error); message.error('加载Dify知识库列表失败'); setDifyDatasetsLoading(false); } finally { setIsLoadingDifyDatasets(false); } }; /** * Dify数据集选择器滚动加载 */ const handleDatasetSelectScroll = (e: React.UIEvent) => { const { target } = e; const { scrollTop, scrollHeight, clientHeight } = target as HTMLDivElement; // 滚动到底部且还有更多数据时加载下一页 if (scrollHeight - scrollTop === clientHeight && difyDatasets.length < difyDatasetsTotal && !difyDatasetsLoading) { loadDifyDatasets(difyDatasetsPage + 1); } }; // ==================== Event Handlers ==================== /** * 处理新建按钮点击 */ const handleCreateClick = () => { if (!canManageDataset) { message.error('您没有创建知识库绑定的权限'); return; } setEditingId(null); setModalVisible(true); form.resetFields(); loadDifyDatasets(); }; /** * 处理编辑按钮点击 */ 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) => { 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, 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, 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: 70, 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) => ( handleDeleteClick(record.id)} okText="确定" cancelText="取消" > ), }, ] : []), ]; return (
{/* 页面头部 */}
知识库配置管理 地区: {userArea || '-'} 角色: {userRoleLabel} 总数: {total}
{/* 仅管理员显示新增按钮 */} {canManageDataset && ( )}
{/* 筛选区域(仅管理员可见) */} {canManageDataset && ( 地区筛选: ({ label: area, value: area, })) : []} /> {/* Dify知识库选择(仅新增时可选) */} {/* 知识库描述 */} {/* */} {/* 高级设置折叠面板 */}
高级设置 {/* 是否公开 */} {/* 是否默认 */} {/* 排序顺序 */}
); }