import React, { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import KnowledgeCard from './KnowledgeCard'; import { useDispatch } from 'react-redux'; import { showNotification } from '../../store/notification.slice'; import SvgIcon from '../../components/SvgIcon'; export default function KnowledgeBase() { const dispatch = useDispatch(); const navigate = useNavigate(); const [showCreateModal, setShowCreateModal] = useState(false); const [showAccessRequestModal, setShowAccessRequestModal] = useState(false); const [formErrors, setFormErrors] = useState({}); const [accessRequestErrors, setAccessRequestErrors] = useState({}); const [accessRequestData, setAccessRequestData] = useState({ id: '', title: '', accessType: '只读访问', duration: '一周', projectInfo: '', reason: '', }); const [newKnowledgeBase, setNewKnowledgeBase] = useState({ title: '', description: '', }); const knowledgeList = [ { id: '1', title: '产品开发知识库', description: '产品开发流程及规范说明文档', documents: 24, date: '2025-02-15', access: 'full', }, { id: '2', title: '市场分析知识库', description: '2025年Q1市场分析总结', documents: 12, date: '2025-02-10', access: 'read', }, { id: '3', title: '财务知识库', description: '月度财务分析报告', documents: 8, date: '2025-02-01', access: 'none', }, ]; const handleInputChange = (e) => { const { name, value } = e.target; setNewKnowledgeBase((prev) => ({ ...prev, [name]: value, })); // Clear error when user types if (formErrors[name]) { setFormErrors((prev) => ({ ...prev, [name]: '', })); } }; const handleAccessRequestInputChange = (e) => { const { name, value } = e.target; setAccessRequestData((prev) => ({ ...prev, [name]: value, })); // Clear error when user types if (accessRequestErrors[name]) { setAccessRequestErrors((prev) => ({ ...prev, [name]: '', })); } }; const validateCreateForm = () => { const errors = {}; if (!newKnowledgeBase.title.trim()) { errors.title = '请输入知识库名称'; } if (!newKnowledgeBase.description.trim()) { errors.description = '请输入知识库描述'; } setFormErrors(errors); return Object.keys(errors).length === 0; }; const validateAccessRequestForm = () => { const errors = {}; if (!accessRequestData.projectInfo.trim()) { errors.projectInfo = '请输入项目信息'; } if (!accessRequestData.reason.trim()) { errors.reason = '请输入申请原因'; } setAccessRequestErrors(errors); return Object.keys(errors).length === 0; }; const handleCreateKnowledgeBase = () => { // Validate form if (!validateCreateForm()) { return; } // For now, just show a success notification dispatch( showNotification({ message: '知识库创建成功', type: 'success', }) ); // In a real application, you would get the ID from the API response // For now, we'll generate a mock ID const newId = Date.now().toString(); // Reset form and close modal setNewKnowledgeBase({ title: '', description: '' }); setFormErrors({}); setShowCreateModal(false); // Navigate to the newly created knowledge base with datasets tab navigate(`/knowledge-base/${newId}/datasets`); }; const handleCardClick = (id) => { navigate(`/knowledge-base/${id}/datasets`); }; const handleRequestAccess = (id, title) => { setAccessRequestData((prev) => ({ ...prev, id, title, })); setAccessRequestErrors({}); setShowAccessRequestModal(true); }; const handleSubmitAccessRequest = () => { // Validate form if (!validateAccessRequestForm()) { return; } // Here you would typically call an API to submit the access request dispatch( showNotification({ message: '权限申请已提交', type: 'success', }) ); // Reset form and close modal setAccessRequestData((prev) => ({ ...prev, projectInfo: '', reason: '', })); setAccessRequestErrors({}); setShowAccessRequestModal(false); }; return (
{knowledgeList.map((item) => ( handleCardClick(item.id)} onRequestAccess={handleRequestAccess} /> ))}
{/* 新建知识库弹窗 */} {showCreateModal && (
新建知识库
{formErrors.title &&
{formErrors.title}
}
{formErrors.description && (
{formErrors.description}
)}
)} {/* 申请访问权限弹窗 */} {showAccessRequestModal && (
申请访问权限
setAccessRequestData((prev) => ({ ...prev, accessType: '只读访问' })) } >
只读访问
仅查看数据集内容
setAccessRequestData((prev) => ({ ...prev, accessType: '完全访问' })) } >
完全访问
查看、编辑和管理数据
{accessRequestErrors.projectInfo && (
{accessRequestErrors.projectInfo}
)}
{accessRequestErrors.reason && (
{accessRequestErrors.reason}
)}
)}
); }