import React, { useState, useEffect } from 'react'; import { useSearchParams, useNavigate } from 'react-router-dom'; import { Facebook, MessageSquare, Instagram, Linkedin, CheckCircle, XCircle, MoreHorizontal, ExternalLink, BookOpen, ThumbsUp, ThumbsDown, Minus, AlertTriangle, User, Award, Briefcase, Youtube, Hash, Filter, ChevronDown, ArrowLeft } from 'lucide-react'; import CommentPreview from './CommentPreview'; // 定义后端返回的评论类型 interface ApiComment { comment_id: string; content: string; sentiment_score: number; created_at: string; updated_at: string; post_id: string; user_id: string; user_profile?: { id: string; full_name: string; avatar_url: string; }; } // 定义前端使用的评论类型 interface FrontendComment { id: string; content: string; author: string; authorType: 'user' | 'kol' | 'official'; platform: 'facebook' | 'threads' | 'instagram' | 'linkedin' | 'xiaohongshu' | 'youtube'; contentType?: 'post' | 'reel' | 'video' | 'short'; timestamp: string; sentiment: string; status: string; replyStatus?: string; language?: string; articleTitle?: string; postAuthor?: string; postAuthorType?: string; url?: string; } interface CommentListProps { postId?: string; // 可选的帖子 ID,如果提供则只获取该帖子的评论 } interface PostData { id: string; title: string; description?: string; platform: string; post_url?: string; } const CommentList: React.FC = () => { const [searchParams] = useSearchParams(); const navigate = useNavigate(); const postId = searchParams.get('post_id'); const [comments, setComments] = useState([]); const [post, setPost] = useState(null); // Store post data const [loading, setLoading] = useState(true); const [selectedComment, setSelectedComment] = useState(null); const [error, setError] = useState(null); // 过滤和分页状态 const [platformFilter, setPlatformFilter] = useState('all'); const [statusFilter, setStatusFilter] = useState('all'); const [sentimentFilter, setSentimentFilter] = useState('all'); const [replyStatusFilter, setReplyStatusFilter] = useState('all'); const [languageFilter, setLanguageFilter] = useState('all'); const [searchQuery, setSearchQuery] = useState(''); const [currentPage, setCurrentPage] = useState(1); const [pageSize, setPageSize] = useState(10); const [totalComments, setTotalComments] = useState(0); const [showFilters, setShowFilters] = useState(false); // Fetch post details if postId is provided useEffect(() => { const fetchPostDetails = async () => { if (!postId) return; try { setLoading(true); // Mock post data const mockPost = { id: postId, title: 'Sample Post Title', content: 'This is a sample post content for demonstration purposes.', platform: 'Facebook', url: 'https://facebook.com/sample-post' }; setPost(mockPost); setLoading(false); } catch (error) { console.error('Error fetching post details:', error); setLoading(false); } }; fetchPostDetails(); }, [postId]); // Fetch comments useEffect(() => { const fetchComments = async () => { try { setLoading(true); // Mock comments data const mockComments = [ { id: '1', content: 'Great post! I really enjoyed reading this.', author: 'John Smith', timestamp: '2023-05-15T10:30:00Z', platform: 'Facebook', sentiment: 'positive', status: 'approved' }, { id: '2', content: 'This was very helpful, thanks for sharing!', author: 'Sarah Johnson', timestamp: '2023-05-14T14:45:00Z', platform: 'Twitter', sentiment: 'positive', status: 'pending' }, { id: '3', content: 'I have a question about the third point you mentioned...', author: 'Michael Brown', timestamp: '2023-05-13T09:15:00Z', platform: 'Instagram', sentiment: 'neutral', status: 'approved' } ]; setComments(mockComments); setTotalComments(mockComments.length); setLoading(false); } catch (error) { console.error('Error fetching comments:', error); setLoading(false); } }; fetchComments(); }, [postId, currentPage, pageSize, statusFilter, platformFilter, sentimentFilter]); // 简单的语言检测 const detectLanguage = (text: string): 'zh-TW' | 'zh-CN' | 'en' => { const traditionalChineseRegex = /[一-龥]/; const simplifiedChineseRegex = /[一-龥]/; const englishRegex = /[a-zA-Z]/; if (englishRegex.test(text) && !traditionalChineseRegex.test(text) && !simplifiedChineseRegex.test(text)) { return 'en'; } else if (traditionalChineseRegex.test(text)) { // 这里简化了繁体/简体的判断,实际实现应该更复杂 return 'zh-TW'; } else { return 'zh-CN'; } }; // Function to go back to posts list const handleBackToPosts = () => { navigate('/posts'); }; // 显示加载状态 if (loading) { return (
); } // 显示错误信息 if (error) { return (

{error}

); } return (
{postId && ( )}

{post ? `${post.title} 的评论` : '所有评论'}

{post && ( ({totalComments} 条评论) )}
setSearchQuery(e.target.value)} />
{/* Mobile filters panel */} {showFilters && (
)} {/* Mobile comment list */}
{comments.map((comment) => (
setSelectedComment(comment)} >
Facebook

{comment.content}

{comment.author}
{comment.timestamp}
))}
{/* Desktop table */}
{comments.map((comment) => ( setSelectedComment(comment)} > ))}
平台 留言內容 留言者 時間 語言 情感 回覆狀態 操作
Facebook
{comment.content}
{comment.author}
{comment.timestamp}
{comment.language === 'zh-TW' && ( 繁中 )} {comment.language === 'zh-CN' && ( 简中 )} {comment.language === 'en' && ( EN )} {comment.sentiment === 'positive' && ( 正面 )} {comment.sentiment === 'negative' && ( 負面 )} {comment.sentiment === 'neutral' && ( 中性 )} {comment.replyStatus === 'sent' && ( 已回覆 )} {comment.replyStatus === 'draft' && ( 草稿 )} {comment.replyStatus === 'none' && ( 未回覆 )}
{selectedComment && (
setSelectedComment(null)} />
)}
); }; export default CommentList;