import React, { useState, useEffect } from 'react' export default function YichensBoard() { const [stats, setStats] = useState({ posts: null, categories: [], users: null }) const [posts, setPosts] = useState([]) const [loading, setLoading] = useState(false) const [expandedPosts, setExpandedPosts] = useState({}) const [postTypeFilter, setPostTypeFilter] = useState('all') const API_BASE = localStorage.getItem('API_BASE') || '' useEffect(() => { fetchStats(); fetchPosts() }, []) const fetchStats = async () => { try { const [p, c, u] = await Promise.all([ fetch(`${API_BASE}/api/yichens/stats/posts?days=30`), fetch(`${API_BASE}/api/yichens/stats/categories?days=30`), fetch(`${API_BASE}/api/yichens/stats/users`) ]) setStats({ posts: await p.json(), categories: await c.json(), users: await u.json() }) } catch(e) { console.error(e) } } const fetchPosts = async () => { setLoading(true) let url = `${API_BASE}/api/yichens/posts?limit=50` if (postTypeFilter !== 'all') url += `&post_type=${postTypeFilter}` try { const res = await fetch(url) setPosts((await res.json()) || []) } catch { setPosts([]) } setLoading(false) } const togglePost = id => setExpandedPosts(p => ({ ...p, [id]: !p[id] })) const filtered = postTypeFilter === 'all' ? posts : posts.filter(p => p.post_type === postTypeFilter) return (