'use client'; import { useQuery } from '@tanstack/react-query'; import { DollarSign, LayoutGrid, TrendingUp, Users } from 'lucide-react'; import { apiFetch } from '@/lib/api/client'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { CardSkeleton } from '@/components/shared/loading-skeleton'; import { WidgetErrorBoundary } from './widget-error-boundary'; interface KpiData { totalClients: number; activeInterests: number; pipelineValueUsd: number; occupancyRate: number; } function formatCurrency(value: number): string { return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0, }).format(value); } function formatPercent(value: number): string { return `${value.toFixed(1)}%`; } export function KpiCards() { const { data, isLoading, isError } = useQuery({ queryKey: ['dashboard', 'kpis'], queryFn: () => apiFetch('/api/v1/dashboard/kpis'), staleTime: 60_000, retry: 2, }); if (isLoading) { return ( <> ); } const kpis = [ { label: 'Total Clients', value: isError ? '—' : String(data?.totalClients ?? 0), icon: Users, }, { label: 'Active Interests', value: isError ? '—' : String(data?.activeInterests ?? 0), icon: TrendingUp, }, { label: 'Pipeline Value', value: isError ? '—' : formatCurrency(data?.pipelineValueUsd ?? 0), icon: DollarSign, }, { label: 'Occupancy Rate', value: isError ? '—' : formatPercent(data?.occupancyRate ?? 0), icon: LayoutGrid, }, ]; return ( <> {kpis.map(({ label, value, icon: Icon }) => ( {label}
{value}
))} ); } export function KpiCardsWithBoundary() { return ( ); }