import React, { useState, useEffect } from 'react'; import './Dashboard.css'; export default function Dashboard() { const [ dashboardData, setDashboardData ] = useState( { postsCount: 0, pagesCount: 0, commentsCount: 0, usersCount: 0, recentPosts: [], recentComments: [], loading: true, } ); useEffect( () => { // Fetch dashboard data from WordPress REST API const fetchDashboardData = async () => { try { const [ posts, pages, comments, users ] = await Promise.all( [ fetch( `${ window.helixData?.restUrl || '/wp-json/wp/v2/' }posts?per_page=5` ), fetch( `${ window.helixData?.restUrl || '/wp-json/wp/v2/' }pages?per_page=5` ), fetch( `${ window.helixData?.restUrl || '/wp-json/wp/v2/' }comments?per_page=5` ), fetch( `${ window.helixData?.restUrl || '/wp-json/wp/v2/' }users?per_page=5` ), ] ); const [ postsData, pagesData, commentsData, usersData ] = await Promise.all( [ posts.json(), pages.json(), comments.json(), users.json(), ] ); setDashboardData( { postsCount: posts.headers.get( 'X-WP-Total' ) || postsData.length, pagesCount: pages.headers.get( 'X-WP-Total' ) || pagesData.length, commentsCount: comments.headers.get( 'X-WP-Total' ) || commentsData.length, usersCount: users.headers.get( 'X-WP-Total' ) || usersData.length, recentPosts: postsData.slice( 0, 5 ), recentComments: commentsData.slice( 0, 5 ), loading: false, } ); } catch ( error ) { // Error fetching dashboard data setDashboardData( ( prev ) => ( { ...prev, loading: false } ) ); } }; fetchDashboardData(); }, [] ); const StatsCard = ( { title, count, icon, color } ) => (
{ title }
Loading dashboard...
Your modern WordPress admin experience
{ formatDate( post.date ) }
No posts yet.{ ' ' } Create your first post!
) }{ comment.content.rendered .replace( /<[^>]*>/g, '' ) .substring( 0, 100 ) } ...
{ formatDate( comment.date ) }
No comments yet.
) }Stay updated with the latest WordPress news and updates.
The latest version includes new features and improvements...
Read more