front fix
This commit is contained in:
@@ -24,7 +24,6 @@ import {
|
||||
ArrowLeft
|
||||
} from 'lucide-react';
|
||||
import CommentPreview from './CommentPreview';
|
||||
import { commentsApi, postsApi } from '../utils/api';
|
||||
|
||||
// 定义后端返回的评论类型
|
||||
interface ApiComment {
|
||||
@@ -96,105 +95,82 @@ const CommentList: React.FC<CommentListProps> = () => {
|
||||
const [totalComments, setTotalComments] = useState<number>(0);
|
||||
const [showFilters, setShowFilters] = useState<boolean>(false);
|
||||
|
||||
// Fetch post data if postId is provided
|
||||
// Fetch post details if postId is provided
|
||||
useEffect(() => {
|
||||
const fetchPostData = async () => {
|
||||
if (postId) {
|
||||
try {
|
||||
const response = await postsApi.getPost(postId);
|
||||
setPost(response.data);
|
||||
} catch (err) {
|
||||
console.error('Failed to fetch post data:', err);
|
||||
}
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
fetchPostData();
|
||||
fetchPostDetails();
|
||||
}, [postId]);
|
||||
|
||||
// 获取评论数据
|
||||
// Fetch comments
|
||||
useEffect(() => {
|
||||
const fetchComments = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
|
||||
// Build query parameters
|
||||
const params: Record<string, string | number> = {};
|
||||
|
||||
if (postId) {
|
||||
params.post_id = postId;
|
||||
}
|
||||
|
||||
if (platformFilter !== 'all') {
|
||||
params.platform = platformFilter;
|
||||
}
|
||||
|
||||
if (statusFilter !== 'all') {
|
||||
params.status = statusFilter;
|
||||
}
|
||||
|
||||
if (sentimentFilter !== 'all') {
|
||||
params.sentiment = sentimentFilter;
|
||||
}
|
||||
|
||||
if (searchQuery) {
|
||||
params.query = searchQuery;
|
||||
}
|
||||
|
||||
if (languageFilter !== 'all') {
|
||||
params.language = languageFilter;
|
||||
}
|
||||
|
||||
// Add pagination
|
||||
params.limit = pageSize;
|
||||
params.offset = (currentPage - 1) * pageSize;
|
||||
|
||||
const response = await commentsApi.getComments(params);
|
||||
|
||||
// 处理返回的数据
|
||||
const apiComments: ApiComment[] = response.data.comments || [];
|
||||
const total = response.data.total || apiComments.length;
|
||||
|
||||
// 转换为前端格式
|
||||
const frontendComments: FrontendComment[] = apiComments.map(comment => {
|
||||
// 确定情感
|
||||
let sentiment = 'neutral';
|
||||
if (comment.sentiment_score > 0.3) {
|
||||
sentiment = 'positive';
|
||||
} else if (comment.sentiment_score < -0.3) {
|
||||
sentiment = 'negative';
|
||||
// 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'
|
||||
}
|
||||
|
||||
// 检测语言
|
||||
const language = detectLanguage(comment.content);
|
||||
|
||||
return {
|
||||
id: comment.comment_id,
|
||||
content: comment.content,
|
||||
author: comment.user_profile?.full_name || '匿名用户',
|
||||
authorType: 'user', // 默认为普通用户
|
||||
platform: 'facebook', // 假设默认是 Facebook
|
||||
timestamp: comment.created_at,
|
||||
sentiment,
|
||||
status: 'approved', // 假设默认已审核
|
||||
language,
|
||||
// 其他可选字段可以根据 API 返回的数据动态添加
|
||||
};
|
||||
});
|
||||
];
|
||||
|
||||
setComments(frontendComments);
|
||||
setTotalComments(total);
|
||||
setError(null);
|
||||
} catch (err) {
|
||||
console.error('Failed to fetch comments:', err);
|
||||
setError('加载评论失败,请稍后再试');
|
||||
} finally {
|
||||
setComments(mockComments);
|
||||
setTotalComments(mockComments.length);
|
||||
setLoading(false);
|
||||
} catch (error) {
|
||||
console.error('Error fetching comments:', error);
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchComments();
|
||||
}, [postId, platformFilter, statusFilter, sentimentFilter, searchQuery, languageFilter, currentPage, pageSize]);
|
||||
}, [postId, currentPage, pageSize, statusFilter, platformFilter, sentimentFilter]);
|
||||
|
||||
// 简单的语言检测
|
||||
const detectLanguage = (text: string): 'zh-TW' | 'zh-CN' | 'en' => {
|
||||
|
||||
Reference in New Issue
Block a user