"use client" import type React from "react" import { useEffect, useState } from "react" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { UserCheck, HeartHandshake, DollarSign, Users } from "lucide-react" import { Skeleton } from "@/components/ui/skeleton" // Type for stats type PulseStatsData = { verifiedCHWs: number activeGuardians: number totalSupport: number livesImpacted: number totalUsers: number lastUpdated: string } export function PulseStats() { const [stats, setStats] = useState>({}) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { const fetchStats = async () => { try { const response = await fetch("/api/stats") if (!response.ok) { throw new Error("Failed to fetch stats") } const data = await response.json() setStats(data) } catch (err) { setError("Failed to load statistics") console.error(err) } finally { setLoading(false) } } fetchStats() }, []) // Stat card component const StatCard = ({ title, value, icon: Icon, isLoading, isError, colorClass, }: { title: string value: number | string icon: React.ElementType isLoading: boolean isError: boolean colorClass?: string }) => ( {colorClass && (
)} {title} {isError ? (
Data unavailable
) : isLoading ? ( ) : (
{value}
)}

{isLoading ? "Loading..." : error ? "Update failed" : stats.lastUpdated ? `Updated: ${new Date(stats.lastUpdated).toLocaleString()}` : "Live data"}

) return (
) }