import { useState, useEffect } from 'react'; import { AdminLayout } from '@/components/layout/AdminLayout'; import { Card, CardContent, // CardDescription, // Not used in StatsCard, can be removed if not used elsewhere CardHeader, CardTitle, } from '@/components/ui/card'; import { BarChart, Layers, FileCheck, GitBranch } from 'lucide-react'; import { supabase } from '@/lib/supabase'; // Import Supabase client import { speciesApi } from '@/services/adminApi'; // Import speciesApi interface StatsCardProps { title: string; value: number | string; description: string; icon: React.ReactNode; } export default function AdminDashboard() { const [stats, setStats] = useState({ totalSpecies: 0, totalTradeRecords: 0, totalIucnAssessments: 0, totalCitesListings: 0, }); const [loadingStats, setLoadingStats] = useState(true); useEffect(() => { const fetchStats = async () => { console.log('[Dashboard] Fetching stats...'); setLoadingStats(true); try { // Using Promise.all to fetch all counts concurrently const [ speciesRes, tradeRecordsRes, iucnAssessmentsRes, citesListingsRes, ] = await Promise.all([ speciesApi.getAll(1, 1), // Fetches total count for species from speciesApi supabase.from('cites_trade_records').select('*', { count: 'exact', head: true }), // Corrected table name supabase.from('iucn_assessments').select('*', { count: 'exact', head: true }), supabase.from('cites_listings').select('*', { count: 'exact', head: true }), ]); // Log errors if any, but still try to set counts for successful fetches // speciesApi.getAll() throws on error, so speciesRes won't have an .error property here if successful. // Errors from speciesApi.getAll() will be caught by the main try...catch block. if (tradeRecordsRes.error) console.error('[Dashboard] Error fetching CITES trade records count:', tradeRecordsRes.error); if (iucnAssessmentsRes.error) console.error('[Dashboard] Error fetching IUCN assessments count:', iucnAssessmentsRes.error); if (citesListingsRes.error) console.error('[Dashboard] Error fetching CITES listings count:', citesListingsRes.error); setStats({ totalSpecies: speciesRes.count || 0, // speciesRes.count is available if speciesApi.getAll() succeeded totalTradeRecords: tradeRecordsRes.count || 0, totalIucnAssessments: iucnAssessmentsRes.count || 0, totalCitesListings: citesListingsRes.count || 0, }); } catch (error) { console.error('[Dashboard] Error in fetchStats Promise.all:', error); // In case of a major error in Promise.all itself, stats will remain 0 } finally { setLoadingStats(false); console.log('[Dashboard] Finished fetching stats. Current stats:', stats); // Log current stats after fetch } }; fetchStats(); }, []); // Empty dependency array ensures this runs once on mount return (

Admin Dashboard

Manage your Arctic species database

} /> } /> } /> } />
{/* TODO: Add loading indicator for the whole stats section if loadingStats is true */} {/* Recent activity, quick links or other dashboard elements */}
); } function StatsCard({ title, value, description, icon }: StatsCardProps) { return ( {title} {icon}
{value}

{description}

); }