/** * 价值论坛主页面 * 类似小红书/X的帖子广场 */ import React, { useState, useEffect } from 'react'; import { Box, Container, Heading, Text, Button, HStack, VStack, SimpleGrid, Input, InputGroup, InputLeftElement, Select, Spinner, Center, useDisclosure, Flex, Badge, } from '@chakra-ui/react'; import { Search, PenSquare, TrendingUp, Clock, Heart } from 'lucide-react'; import { motion, AnimatePresence } from 'framer-motion'; import { forumColors } from '@theme/forumTheme'; import { getPosts, searchPosts } from '@services/elasticsearchService'; import PostCard from './components/PostCard'; import CreatePostModal from './components/CreatePostModal'; const MotionBox = motion(Box); const ValueForum = () => { const [posts, setPosts] = useState([]); const [loading, setLoading] = useState(true); const [searchKeyword, setSearchKeyword] = useState(''); const [sortBy, setSortBy] = useState('created_at'); const [page, setPage] = useState(1); const [total, setTotal] = useState(0); const [hasMore, setHasMore] = useState(true); const { isOpen, onOpen, onClose } = useDisclosure(); // 获取帖子列表 const fetchPosts = async (currentPage = 1, reset = false) => { try { setLoading(true); let result; if (searchKeyword.trim()) { result = await searchPosts(searchKeyword, { page: currentPage, size: 20, }); } else { result = await getPosts({ page: currentPage, size: 20, sort: sortBy, order: 'desc', }); } if (reset) { setPosts(result.posts); } else { setPosts((prev) => [...prev, ...result.posts]); } setTotal(result.total); setHasMore(result.posts.length === 20); } catch (error) { console.error('获取帖子列表失败:', error); } finally { setLoading(false); } }; // 初始化加载 useEffect(() => { fetchPosts(1, true); }, [sortBy]); // 搜索处理 const handleSearch = () => { setPage(1); fetchPosts(1, true); }; // 加载更多 const loadMore = () => { const nextPage = page + 1; setPage(nextPage); fetchPosts(nextPage, false); }; // 发帖成功回调 const handlePostCreated = () => { setPage(1); fetchPosts(1, true); }; // 排序选项 const sortOptions = [ { value: 'created_at', label: '最新发布', icon: Clock }, { value: 'likes_count', label: '最多点赞', icon: Heart }, { value: 'views_count', label: '最多浏览', icon: TrendingUp }, ]; return ( {/* 顶部横幅 */} {/* 标题区域 */} 价值论坛 分享投资见解,追踪市场热点,共同发现价值 {/* 发帖按钮 */} {/* 搜索和筛选栏 */} {/* 搜索框 */} setSearchKeyword(e.target.value)} onKeyPress={(e) => e.key === 'Enter' && handleSearch()} bg={forumColors.background.card} border="1px solid" borderColor={forumColors.border.default} color={forumColors.text.primary} _placeholder={{ color: forumColors.text.tertiary }} _hover={{ borderColor: forumColors.border.light }} _focus={{ borderColor: forumColors.border.gold, boxShadow: `0 0 0 1px ${forumColors.border.goldGlow}`, }} /> {/* 排序选项 */} {sortOptions.map((option) => { const Icon = option.icon; const isActive = sortBy === option.value; return ( ); })} {/* 统计信息 */} {total} 篇帖子 {/* 帖子网格 */} {loading && page === 1 ? (
加载中...
) : posts.length === 0 ? (
{searchKeyword ? '未找到相关帖子' : '暂无帖子,快来发布第一篇吧!'} {!searchKeyword && ( )}
) : ( <> {posts.map((post, index) => ( ))} {/* 加载更多按钮 */} {hasMore && (
)} )}
{/* 发帖模态框 */}
); }; export default ValueForum;