import React, { useState, useEffect, useRef, useCallback } from 'react'; import { Link, useNavigate, useParams } from 'react-router-dom'; import apiClient from '../utils/apiClient'; import { ArrowLeft, FileText, Tag, Save } from 'lucide-react'; import MDEditor, * as commands from '@uiw/react-md-editor'; import '@uiw/react-md-editor/markdown-editor.css'; import { buildApiUrl, API_ENDPOINTS } from '../config/api'; import TagEditor from '../components/TagEditor'; import './EditKnowledgeBase.css'; const EditKnowledgeBase = ({ user }) => { const navigate = useNavigate(); const { kb_id } = useParams(); const [formData, setFormData] = useState({ title: '', content: '', tags: '' }); const [isLoading, setIsLoading] = useState(true); const [isSaving, setIsSaving] = useState(false); const [error, setError] = useState(''); const [kb, setKb] = useState(null); const handleContentChange = useCallback((value) => { setFormData(prev => ({ ...prev, content: value || '' })); }, []); useEffect(() => { fetchKbData(); }, [kb_id]); const fetchKbData = async () => { try { const response = await apiClient.get(buildApiUrl(API_ENDPOINTS.KNOWLEDGE_BASE.DETAIL(kb_id))); const kbData = response.data; // Check if current user is the creator if (kbData.creator_id !== user.user_id) { navigate('/knowledge-base'); return; } setKb(kbData); setFormData({ title: kbData.title, content: kbData.content || '', tags: kbData.tags ? kbData.tags.map(t => t.name).join(', ') : '' }); } catch (err) { setError('无法加载知识库信息'); console.error('Error fetching knowledge base:', err); } finally { setIsLoading(false); } }; const handleInputChange = (e) => { const { name, value } = e.target; setFormData(prev => ({ ...prev, [name]: value })); }; const handleSubmit = async (e) => { e.preventDefault(); if (!formData.title.trim()) { setError('请输入知识库标题'); return; } setIsSaving(true); setError(''); try { const updateData = { title: formData.title, content: formData.content, tags: formData.tags }; await apiClient.put(buildApiUrl(API_ENDPOINTS.KNOWLEDGE_BASE.UPDATE(kb_id)), updateData); navigate('/knowledge-base'); } catch (err) { setError(err.response?.data?.message || '更新知识库失败,请重试'); } finally { setIsSaving(false); } }; // 自定义工具栏命令配置 const customCommands = [ commands.bold, commands.italic, commands.strikethrough, commands.hr, commands.group([ commands.title1, commands.title2, commands.title3, commands.title4, commands.title5, commands.title6, ], { name: 'title', groupName: 'title', buttonProps: { 'aria-label': '插入标题', title: '插入标题' } }), commands.divider, commands.link, commands.quote, commands.code, commands.codeBlock, commands.image, commands.divider, commands.unorderedListCommand, commands.orderedListCommand, commands.checkedListCommand, ]; // 右侧额外命令(预览、全屏等) const customExtraCommands = [ commands.codeEdit, commands.codeLive, commands.codePreview, commands.divider, commands.fullscreen, ]; if (isLoading) { return (

加载中...

); } return (
返回知识库

编辑知识库

修改知识库标题、标签和内容摘要

setFormData(prev => ({ ...prev, tags: value }))} placeholder="输入标签,按回车或逗号分隔" />
使用Markdown格式编写知识库内容,支持**粗体**、*斜体*、# 标题、- 列表等格式。
{error && (
{error}
)}
取消
); }; export default EditKnowledgeBase;