'use client'; // 信息流:帖子列表 + 排序 tab + 发布新帖按钮 import { useEffect, useState, useCallback, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { API } from '@/lib/api'; import { useApp } from './AppProvider'; import { Icon } from './Icons'; import ImageViewer from './ImageViewer'; import { renderMarkdown } from '@/lib/markdown'; import { useToast } from './Toast'; function avatar(username) { return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || '匿名用户') + '&background=6366f1&color=fff&size=64'; } export default function Feed({ onOpenModal, onReport }) { const { currentUser, sort, setSort, openPost, openUser, navigate, refreshKey } = useApp(); const { t } = useTranslation(); const { toast, confirmAction } = useToast(); const [posts, setPosts] = useState(null); const [error, setError] = useState(null); const [loading, setLoading] = useState(true); const [page, setPage] = useState(1); const [totalPages, setTotalPages] = useState(1); const [viewerSrc, setViewerSrc] = useState(null); const [selectedSort, setSelectedSort] = useState(sort); const [drawerPhase, setDrawerPhase] = useState('idle'); const postListRef = useRef(null); const sortTimerRef = useRef(null); const pendingSortRef = useRef(null); const PAGE_SIZE = 20; const load = useCallback(async (targetPage) => { setError(null); try { const result = await API.getPosts(sort, null, targetPage, PAGE_SIZE); setPosts(result.data || []); setTotalPages(result.pagination?.totalPages || 1); setPage(result.pagination?.page || 1); } catch (err) { setError(err.message); setPosts([]); } finally { if (pendingSortRef.current === sort) { requestAnimationFrame(() => { setDrawerPhase('opening'); sortTimerRef.current = setTimeout(() => { pendingSortRef.current = null; setDrawerPhase('idle'); }, 360); }); } } }, [sort, refreshKey]); useEffect(() => { load(page); }, [load]); // 长帖内容:超过 3 行时加 .has-fade(底部渐变遮罩提示还有更多内容) useEffect(() => { if (!posts || posts.length === 0 || !postListRef.current) return; const els = postListRef.current.querySelectorAll('.post-content'); els.forEach((el) => { if (el.scrollHeight > el.clientHeight + 4) { el.classList.add('has-fade'); } else { el.classList.remove('has-fade'); } }); }, [posts]); // 排序切换时回第一页 useEffect(() => { setPage(1); }, [sort]); useEffect(() => () => clearTimeout(sortTimerRef.current), []); const handleSortChange = (nextSort) => { if (nextSort === selectedSort) return; setSelectedSort(nextSort); clearTimeout(sortTimerRef.current); // 快速切回当前排序时,直接重新展开尚未替换的列表。 if (nextSort === sort) { pendingSortRef.current = null; setDrawerPhase('opening'); sortTimerRef.current = setTimeout(() => setDrawerPhase('idle'), 360); return; } setDrawerPhase('closing'); sortTimerRef.current = setTimeout(() => { pendingSortRef.current = nextSort; setDrawerPhase('closed'); setSort(nextSort); }, 220); }; const handleDelete = async (postId) => { if (!await confirmAction(t('feed.confirmDelete'))) return; try { await API.deletePost(postId); load(page); } catch (err) { toast(t('feed.deleteFailed', { msg: err.message }), 'error'); } }; return (
{posts === null ? (
{t('feed.loading')}
) : error ? (
{t('feed.loadFailed', { msg: error })}
) : posts.length === 0 ? (
{t('feed.empty')}
) : ( posts.map((p) => { const time = p.created_at ? new Date(p.created_at).toLocaleString('zh-CN') : ''; return (
openPost(p.id, e.currentTarget, p)}> {p.is_pinned && (
{t('feed.pinned')}
)}
{ e.stopPropagation(); openUser(p.username); }} > {p.username || t('feed.anonymous')} {time} {p.edited_at && ( {t('feed.deleted')} )}
{p.title || t('feed.noTitle')}
{p.images && p.images.length > 0 && (
{p.images.map((img, i) => ( { e.stopPropagation(); setViewerSrc(img); }} /> ))}
)}
{p.reply_count || 0} {currentUser && (currentUser.id === p.user_id || currentUser.role === 'admin') && ( )}
); }) )}
{totalPages > 1 && posts && posts.length > 0 && (
{Array.from({ length: totalPages }, (_, i) => i + 1).map((i) => ( ))}
)} {viewerSrc && setViewerSrc(null)} />}
); }