diff --git a/src/components/Comment/index.js b/src/components/Comment/index.js new file mode 100644 index 0000000..0442a95 --- /dev/null +++ b/src/components/Comment/index.js @@ -0,0 +1,93 @@ +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 ( +
+ + + {isOpen && ( +
+
+ {comments.map(comment => ( +
+
+ {comment.author} + {comment.timestamp} + +
+
{comment.text}
+
+ ))} +
+
+