import { useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { ArrowLeft, Star } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Spinner } from '@/components/ui/spinner'; import { notify } from '@/lib/notify'; import { api } from '../api/client'; import type { PostItem } from '../api/types'; import { useAuth } from '../hooks/useAuth'; import { useSessionResource } from '../hooks/useSessionResource'; import PostListItem from '../components/PostListItem'; import { loginPath } from '../utils/authRedirect'; import { useForumLimits } from '../hooks/useForumLimits'; import { openForumPost } from '../utils/openPost'; import { useNoIndexSEO } from '../hooks/usePageSEO'; import { InFlowSiteFooter } from '../components/SiteFooter'; interface FavItem { id: number; post_id: number; created_at: string; post?: PostItem; } export default function FavoritesPage() { const nav = useNavigate(); const { user, loading: authLoading } = useAuth(); const { limits } = useForumLimits(); useNoIndexSEO('我的收藏'); const { data: list = [], loading } = useSessionResource( user ? 'favorites' : null, () => api.favorites().then(d => (Array.isArray(d.favorites) ? d.favorites as FavItem[] : [])), { enabled: !!user && !authLoading, onError: (e) => notify.error(e instanceof Error ? e.message : '加载失败'), }, ); useEffect(() => { if (authLoading) return; if (!user) nav(loginPath('/favorites')); }, [user, authLoading, nav]); if (authLoading || loading) return
; if (!user) return null; return (

我的收藏

共 {list.length} 篇收藏帖子

{list.length === 0 ? (

还没有收藏任何帖子

) : (
{list.map(fav => ( fav.post ? ( openForumPost(nav, id, limits.open_posts_in_new_tab)} /> ) : ( ) ))}
)}
); }