import React, { useEffect, useState } from 'react' import { Header } from '../components/Header' import { Footer } from '../components/Footer' import { PostCard } from '../components/PostCard' import { PostCardSkeleton } from '../components/PostCardSkeleton' import { Posts } from '../clientsdk/sdk.gen' import { createClient } from '../clientsdk/client' import { customQuerySerializer } from '../clientsdk/querySerializer' import { TENANT_SLUG, TENANT_API_KEY, API_URL } from '../config' const client = createClient({ baseUrl: API_URL, querySerializer: customQuerySerializer, headers: { 'X-Tenant-Slug': TENANT_SLUG, 'X-API-Key': TENANT_API_KEY, }, }) export const Home: React.FC = () => { const [posts, setPosts] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { const fetchPosts = async () => { try { setLoading(true) setError(null) const response = await Posts.listPosts({ client, query: { limit: 10, sort: '-createdAt', }, }) setPosts((response as any)?.data?.docs || []) } catch (err) { setError(err instanceof Error ? err.message : '加载失败') console.error('获取文章失败:', err) } finally { setLoading(false) } } fetchPosts() }, []) const stripHtml = (html: string): string => { const tmp = document.createElement('div') tmp.innerHTML = html return tmp.textContent || tmp.innerText || '' } const formatDate = (dateString: string): string => { const date = new Date(dateString) return date.toLocaleDateString('zh-CN', { year: 'numeric', month: 'long', day: 'numeric', }) } const getCategoryTitle = (post: any): string | undefined => { // categories is an array, get the first one return post.categories?.[0]?.title } const handlePostClick = (slug: string) => { window.location.href = `/posts/${slug}` } return (

📚 最新文章

探索我们的最新内容

{error && (
错误: {error}
)}
{loading ? Array.from({ length: 6 }).map((_, i) => ) : posts.map((post) => ( handlePostClick(post.slug)} /> ))}
{!loading && posts.length === 0 && !error && (

暂无文章

)}
) }