"use client"; import { BlogCardGrid } from "@/components/blog-home/blog-home-card"; import { Button } from "@/components/ui/button"; import { TrendingUp, Clock, } from "lucide-react"; import Sidebar, { SidebarAbout, SidebarHotPosts, SidebarMisskeyIframe, SidebarTags } from "../blog/blog-sidebar-card"; import config from '@/config'; import type { Label } from "@/models/label"; import type { Post } from "@/models/post"; import { listPosts } from "@/api/post"; import { useEffect, useState } from "react"; import { useStoredState } from '@/hooks/use-storage-state'; import { listLabels } from "@/api/label"; import { POST_SORT_TYPE } from "@/localstore"; import { motion } from "framer-motion"; import { useDevice } from "@/hooks/use-device"; // 定义排序类型 type SortType = 'latest' | 'popular'; export default function BlogHome() { const [labels, setLabels] = useState([]); const [posts, setPosts] = useState([]); const [loading, setLoading] = useState(false); const { isMobile } = useDevice(); const [sortType, setSortType, sortTypeLoaded] = useStoredState(POST_SORT_TYPE, 'latest'); useEffect(() => { if (!sortTypeLoaded) return; const fetchPosts = async () => { try { setLoading(true); let orderBy: string; let desc: boolean; switch (sortType) { case 'latest': orderBy = 'updated_at'; desc = true; break; case 'popular': orderBy = 'heat'; desc = true; break; default: orderBy = 'updated_at'; desc = true; } // 处理关键词,空格分割转逗号 const keywords = ""?.trim() ? ""?.trim().split(/\s+/).join(",") : undefined; const data = await listPosts({ page: 1, size: 10, orderBy: orderBy, desc: desc, keywords }); setPosts(data.data); } catch (error) { console.error("Failed to fetch posts:", error); } finally { setLoading(false); } }; fetchPosts(); }, [sortType, sortTypeLoaded]); // 获取标签 useEffect(() => { listLabels().then(data => { setLabels(data.data || []); }).catch(error => { console.error("Failed to fetch labels:", error); }); }, []); // 处理排序切换 const handleSortChange = (type: SortType) => { if (sortType !== type) { setSortType(type); } }; return ( <> {/* 主内容区域 */}
{/* 容器 - 关键布局 */}
{/* 主要内容区域 */} {/* 文章列表标题 */}

{sortType === 'latest' ? '最新文章' : '热门文章'} {posts.length > 0 && ( ({posts.length} 篇) )}

{/* 排序按钮组 */}
{/* 博客卡片网格 */} {/* 加载更多按钮 */} {!loading && posts.length > 0 && (
)} {/* 加载状态指示器 */} {loading && (
正在加载{sortType === 'latest' ? '最新' : '热门'}文章...
)}
{/* 侧边栏 */} , posts.length > 0 ? : null, , , ].filter(Boolean)} />
); }