import React, { useState, useEffect } from 'react'; import styles from './styles.module.css'; export default function Comment({ docId }) { const [comments, setComments] = useState([]); const [newComment, setNewComment] = useState(''); const [isOpen, setIsOpen] = useState(false); useEffect(() => { // 从localStorage中读取评论 const savedComments = JSON.parse(localStorage.getItem(`docComments_${docId}`) || '[]'); setComments(savedComments); }, [docId]); const handleAddComment = () => { if (!newComment.trim()) return; const comment = { id: Date.now(), text: newComment, timestamp: new Date().toLocaleString(), author: '用户' // 这里可以替换为实际的用户名 }; const updatedComments = [...comments, comment]; setComments(updatedComments); localStorage.setItem(`docComments_${docId}`, JSON.stringify(updatedComments)); setNewComment(''); }; const handleDeleteComment = (commentId) => { const updatedComments = comments.filter(comment => comment.id !== commentId); setComments(updatedComments); localStorage.setItem(`docComments_${docId}`, JSON.stringify(updatedComments)); }; return (